I want to use $WPDB class outside Wordpress context in an external Ajax handling script. For this in general is needed to load the core Worpdress files. After loading those core files I can make calls to the MySQL tables using the $wpdb class, but loading the core Wordpress files is a lot of overhead just to run a couple of queries. I was looking for a better solution and I find 'wp_ajax' and 'wp_ajax_nopriv' hooks, but on Wordpress site and on Google I couldn't find any good documentation or tutorial. I need to transfer, using ajax, back and forth data as Json. Could anyone provide me with more information or a good example about this? Thx
Maybe this will help you: $path = ''; if(!defined('WP_LOAD_PATH')){ $root = dirname(dirname(dirname(dirname(dirname(__FILE__))))).'/'; if(file_exists($root.'wp-load.php')){ define('WP_LOAD_PATH',$root); }else{ if(file_exists($path.'wp-load.php')){ define('WP_LOAD_PATH',$path); }else{ exit("Cannot find wp-load.php"); } } } require_once(WP_LOAD_PATH.'wp-load.php'); global $wpdb; Code (markup):
The script is working, but is using the same method that you presented, loading the core Wordpress files, but is a lot of overhead so I founded an wordpress hook wp_ajax to use instead, but is not documented.So I'm searching for a complete demo or tutorial. In wordpress functions.php file I have a function which control a form, which can have tens to hundreds of fields.This function is used to modify different fields in all the site(pages and posts). It is a mass editing function.On submit on the form, with jquery $post I send Json data to a script, that send me back json data, as array.But instead using jquery directly I want to use the wp_ajax hook, so I can have access to $wpdb.