0

I'm developing some additional functionality with a jQuery autocomplete form, some custom database tables, and PHP to query the tables.

I have it working outside of the WordPress framework using mysqli for the database part.

I've added the form to a WordPress page (and added the jQuery references to the header). When I run the Firefox Web Developer Web Console, I can see that the jQuery is doing its job up to the point of calling the PHP script.

My PHP script is in the root directory of my Genesis child theme.

The PHP script is where it hangs, and it appears that I'm not getting access to the information in $wpdb. I executed this script in the same directory as a test:

<?php

echo "got here";
global $wpdb;
print_r($wpdb);

?>

I see "got here" but nothing else.

Is there something else I have to do to get access to $wpdb? Apparently me declaring it global isn't enough.

3

If you're not using WP's built in Ajax handlers, which includes things for you, you will need to include the WP core yourself. Try adding

define('WP_USE_THEMES', false);
global $wpdb;
require(BASE_PATH . 'wp-load.php');

to the top of your file. I should point out that the path to wp-load.php may not be the same and hard coding the path will make your code un-portable.

  • This solved my problem - Though I had to use ABSPATH instead og BASE_PATH – Stender May 4 '18 at 10:08

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