The short answer
错误是因为我在中放置了URL而不是文件路径。一行修复是设置
$p = \'../../../../wp-blog-header.php\';
The long answer
跟随凯撒的链接,我走了一条漫长的路,最终找到了我认为正确的方法。
在里面pluginname/plugin.php
// load your javascript, and setup the ajaxurl variable
// this is loading my js everywhere, we could load it only where needed
// 0.01 is the version of your js, increment this each time you change your js
// so that you don\'t keep using the same cached version
wp_enqueue_script( \'pluginname\', plugins_url( \'js/pluginname.js\', __FILE__ ), array( \'jquery\' ), 0.01 );
wp_localize_script( \'pluginname\', \'pluginname\', array( \'ajaxurl\' => admin_url( \'admin-ajax.php\' ) ) );
// wp_ajax_insert_pick is a combination of wp_ajax_ and the \'action\' : \'insert_pick\' you provide in your js
// ajax_insert_pick is the function below that is called to handle your ajax request
add_action( "wp_ajax_action_name", "ajax_insert_pick" );
// this basically replaces pluginname/submit/pick.php
function ajax_insert_pick() {
global $wpdb;
$var = $_POST[\'var\'];
// handle the ajax request here
echo $response;
die(); // needed otherwise wordpress may append a zero to your response.
}
在中
pluginname/js/pluginname.js
function submit_pick( var ) {
jQuery.post( pluginname.ajaxurl,
{ \'action\' : \'insert_pick\',
\'var\' : var },
function(response) {
alert(response); },
\'text\' );
}
因为我昨天晚上才学会了这一点,而且学习php/mysql/wp/js/ajax才一个月,所以上面很可能有一些错误。但昨晚它对我起了作用,所以在这个阶段我很高兴。