0

I am using a jQuery tokenizing autocomplete text entry (http://loopj.com/jquery-tokeninput) in a Wordpress form. This type of input calls a PHP script on the server (not a function) to build a list of choices from which the user may choose after the user has entered some text in the input box (very similar to your Tags input in this page). My script is located in a plugin directory on my site. I declared

global $wpdb

but am getting a null result when I try an use its methods (like $wpdb->prefix). Clearly, Wordpress is not loaded for my PHP script. I tried adding

include('http://where2bwho2c.dev/wp-config.php');
include('http://where2bwho2c.dev/wp-load.php');
include('http://where2bwho2c.dev/wp-includes/wp-db.php');

to my script, but this had no effect. What do I need to do to make sure Wordpress is loaded in my PHP script so I can use $wpdb?

I looked at a previous question (https://stackoverflow.com/questions/42288109/wpdb-is-null-despite-being-global) but it was not clear what exactly I should do in this case where my script is not a function.

1 Answer 1

-2

I used

define( 'SHORTINIT', true );
$path = $_SERVER['DOCUMENT_ROOT'];
include_once $path . '/wp-load.php';

it seems to work fine.

4
  • 1
    you should never interact with wordpress functions by loading the stack by yourself if you are doing a front end functionality. The only time it might be ok to do it is when an external application on the same server needs to interact with wordpress. Us the wordpress AJAX end point, or better use the json routes for AJAX type of functionality Aug 5, 2017 at 6:53
  • ... part of the reason is that there is no defintive location for those files you include, and even if it is your own server it is better not to assume them if you do not have to. Aug 5, 2017 at 6:58
  • Thanks Mark. I was just using these scripts to move some custom post data over to some custom database tables. I had loaded a bunch of content into custom post types in Wordpress before I realized the Wordpress database design just would not scale for what I had in mind, so I redesigned the site using some custom database tables, but I didn't want to lose my valid data, so I wrote scripts to port it over to the new tables (learned a lot about the internals of the Wordpress database). Anyway, the scripts are not part of the site, just utilities that I used on my development server. Aug 6, 2017 at 18:33
  • that is a fair usage for what you describe in the answer, but it realy do not match the question ;) Aug 7, 2017 at 1:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.