I am performing an jquery ajax request inside my wordpress. This calls an internal php script. This php script needs to be able to access certain wordpress features like... functions.php which is simple for me to include. What i cant do is access info like the current wordpress user, the $wpdb object. My question is... is there some wordpress file which i can include which gives me access to all that data (and functions.php). I hope you understand what i am accessing as i am aware that was probably THE crappest explaination in the world :D
4条回答 默认 最新
dsf487787 2013-01-06 19:01关注THE BAD WAY (as pointed out by others)
When I created some custom PHP to use with wordpress I included the
wp-load.phpfile. Which then loads everything required, including$wpdb.require_once('wp-load.php'); // relative path from your PHP file global $wpdb; $wpdb->show_errors = TRUE; // useful for when you first startI found it was a decent starting point for a quick fix. However you have to remember this will load in a lot more functionality than you may actually require. Thus resulting in slower performance times.
THE GOOD WAY
Once functionality became more complex the 'bad' implementation wasn't proving to be all that great. So I moved onto writing plugins instead. The WordPress codex contains good information on working with AJAX and plugins: http://codex.wordpress.org/AJAX_in_Plugins
In the most basic form you will need to register your AJAX hook:
// 'wp_ajax_foo' is the hook, 'foo' is the function that handles the request add_action( 'wp_ajax_foo', 'foo');You will also need the corresponding function (in this case
foo):function foo() { // handle the AJAX request $bar = $_POST['bar']; }Then in your JavaScript you identify which hook to use with the
actionattribute but leave out thewp_ajaxpart:$.post(ajaxurl, { action: 'foo', bar: true }, function(response) { // do something with response });本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报