Wordpress ajax and $wpdb
Anyone who does any tinkering with Wordpress templates or plugins is likely familiar with the Wordpress class for interfacing with the Wordpress MySQL database, $wpdb. In the previous article here in The Garage, Viewer-facing Ajax in Wordpress Plugin, I described a problem I was having with Ajax requests from the public side of a website from a user who is not logged in. Pesky little problem it has been.
In a nutshell, if the user is not logged in they do not have access to any of the built-in Wordpress Ajax functionality so you have to write your own Ajax handler, which is not a problem until you need to access the database using $wpdb since the $wpdb class is not normally available to a page or script that is loaded outside the Wordpress context. My research led me to a couple tricks around this limitation such as a template redirect or loading the core Worpdress files in the external Ajax handling script, eg,
include_once(‘wp-config.php’); include_once(‘wp-load.php’); include_once(‘wp-includes/wp-db.php’);
In theory, after loading those core files I can now make calls to the MySQL tables using the $wpdb class just as if I was logged in to the admin panel. In the back of my mind I am thinking that maintaining extra scripts for anonymous db access is a lot of work and loading the core Wordpress files is a lot of overhead just to run a couple of simple queries. Really not optimal.
So I was still looking for a better solution even as I was working on making the above work when I stumbled upon the better solution. Somewhere around March Wordpress released an update (v2.8 I believe) which has support for Ajax actions initiated by a user who is not logged in. Along with the ‘wp_ajax’ hook there is now also a ‘wp_ajax_nopriv’ hook that provides the same functionality but for users that are not logged in. So when your main plugin code is loading all you need to do is check to see if the user is logged in or not and then initialize the appropriate action hook.
//load nopriv ajax handlers for front-end ajax users not logged in
if ( is_user_logged_in() ) {
add_action('wp_ajax_getVectorMarkers', 'getVectorXML');
add_action('wp_ajax_getVectorMarkersText', 'getVectorXMLText');
} else {
add_action('wp_ajax_nopriv_getVectorMarkers', 'getVectorXML');
add_action('wp_ajax_nopriv_getVectorMarkersText', 'getVectorXMLText');
};
Using the ‘wp_ajax_nopriv’ hook allowed me get rid of a lot of code that was written specifically for displaying custom maps to users who are not logged in. It makes me very happy!
Evidently not too many people know about ‘nopriv’ as the info was difficult for me to Google down. (Click here to see the thread where I found the information.) The alternative methods are also discussed in the thread if you are not in the position to upgrade to the latest Wordpress version with the ‘nopriv’ option. (Here is the Wordpress Trac nopriv change documentation.)
I can verify that wp_ajax_nopriv also works with Wordpress Mu version 2.8.2.

September 27th, 2009 at 7:55 am
[...] TheGarage Tech Blog Tech projects from the corner of a suburban garage « Wordpress ajax and $wpdb [...]
February 1st, 2010 at 7:16 pm
Is there any way to do ajax in wp without building a plugin (which seems kinda redundant)?
February 2nd, 2010 at 6:14 am
I’m not sure I understand the question. To implement new ajax functionality you will have to write some code and hook it to Wordpres. The plugin mechanism is the way to do that.
Ajax is simply a technique of getting or sending data from or to the server without reloading the page. Wordpress itself is chock full of ajax functionality.
The only way I am aware to add custom functionality to Wordpress, ajax or otherwise, is by modifying a theme or writing a plugin.
February 8th, 2010 at 6:12 am
I am in the similar situation as you. I want to use $WPDB class outside Wordpress context in an external Ajax handling script. I need to transfer, using ajax, back and forth data as Json.Can you make your post more detailed, maybe give an example.Thx.
February 8th, 2010 at 12:25 pm
@Green C – all you have to do is include the core Wordpres files
include_once(‘wp-config.php’);
include_once(‘wp-load.php’);
include_once(‘wp-includes/wp-db.php’);
at the top of your external script and then declare Global $wpdb and off you go. I didn’t ultimately use this technique, as mentioned in the aticle, so I can’t vouch for it.
To see if it will work set up a php script, include the Wordpress core files, and then see if you can access $wpdb by echoing a property to the page. Eg.
echo $wpdb->prefix
May 23rd, 2010 at 9:55 am
I am currently running a website for my athletic club and I have created a separate database which will hold results. I wiuld like to go to a webpage and from a drop down box select the race and the results will be populated below on the same page. You dont need to be logged on as i want this available to everyone..
Its driving me crazy at the minute so I would appreciate any help
thanks
May 23rd, 2010 at 11:05 am
Instead of creating a whole new database just create a table in the existing Wordpress database. Then all you will need is a customized page template with the drop down and the technique discussed in the article will work like a charm.
June 22nd, 2010 at 8:57 am
You are the best! This has been bugging me for hours.