0
votes

Alright, I've been pretty frustrated with accessing $wpdb in a new PHP file in WordPress. Now, I've gone through and read all the answers I could find on the situation, and they're all universal in saying that all I need to do is add wp-load.php at the top of my php file which uses $wpdb and it should work. I've done that... However, it still isn't working. To make sure I'm not doing anything stupid, I've included both files in their entirety. Here's my code:

main-admin.php:

<?php    
function my_plugin_menu(){
            add_menu_page('my System', 'my System','administrator', 'my-plugin.php', 'my_plugin_action', plugins_url('my.png', __FILE__));
       }

    function my_plugin_action(){
      ?>
    <form id="createItemForm" method="post">
      <br/><br/>Create New Item
    <table><tr>
    <td>Enter Item Name:</td><td><input type="text" name="itemName" id="itemName"></td></tr><br><tr>
    <td>Enter Item Value: </td><td><input type="text" name="itemValue" id="itemValue"></td></tr><br><tr>
     <td><input type="submit" class="button button-primary" id="specialSubmit2" value="Create Item"></td></tr></table>
    </form>
    <script>
    jQuery(function() {
        jQuery('#createItemForm').submit(function(e){
            e.preventDefault();
            var itemName = jQuery( "input[id='itemName']" ).val();
            var itemValue = jQuery("input[id='itemValue']").val();
            alert(itemName + " " + itemValue);//Test that it works, it does
            jQuery.ajax({
                type: "POST",
                url: "../path/to/submit.php",
                data:{ name: itemName, value: itemValue }, 
                success: function(data){
                    alert(data);
                },
                error: function (req, textStatus, thrownError) {
                        console.log(req.responseText);
                    }
                    });
                });
            });
        </script>
        <?php 
    } 
    ?>

submit.php (within same folder):

<?php
require_once( 'path/to/wp-load.php' );
if(isset($_POST["name"]) && isset($_POST["value"])){
  global $wpdb;
  $varCheckCon = $wpdb->check_connection();
  echo json_encode($varCheckCon);
}
?>

However, all it does is return me a 500 error, not even a "false" for not connected. It should be noted that the main-admin.php file itself is not the core WordPress plugin PHP file (that main file can access $wpdb without any problems). Rather, both main-admin.php and submit.php are new PHP files that I've made within a /admin subfolder within my WordPress plugin. The main-admin.php form appears as part of my dashboard menu in Wordpress; it is called from the main file.

I'm still something of a Wordpress beginner, so maybe it's something incredibly stupid that I'm missing?

Thanks for any help!

3

3 Answers

1
votes

If you want to add your own custom functionality to Wordpress, either frontend or backend, look into creating your own custom plugin.

No matter how simple the plugin (cf Hello Dolly), it is the best way as it keeps things portable and enables you to access $wpdb et al in a standardised way.

From what I understand, you are looking to add a form to the Wordpress admin. You can do this by creating your own admin page using add_menu_page()

1
votes

First you should make sure you are debugging everything properly in wp-config.php

define('WP_DEBUG', true);
define( 'WP_DEBUG_DISPLAY', true );
define('WP_DEBUG_LOG', true);

The debug info ends up in wp-content/debug.log

If you are doing direct ajax calls to php you should make sure the directory is correct and also prevent access to non-ajax calls.

require_once("../../../wp-load.php");

/* AJAX check  */
if(empty($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
    die("Invalid request - please press your back key");
}

If you can't figure out what is going wrong after this, post the errors from the debug log and if that fails check your php error.logs

0
votes

Sorry, answered it! You have to be very careful with the wp-load.php include route. I made a mistake and thus it couldn't find it :) Stupidity on my part, as I thought!