Wordpress ajax and $wpdb
September 20, 2009Anyone 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.
Posted by admin
