//the homepage
$(document).ready(function(){
myplugin_cast_vote(20);
});
//the plugin
<?php
add_action('wp_head', 'myplugin_js_header' );
function myplugin_js_header() // this is a PHP function
{
// use JavaScript SACK library for Ajax
wp_print_scripts( array( 'sack' ));
// Define custom JavaScript function
?>
<script type="text/javascript">
//<![CDATA[
function myplugin_cast_vote(posts)
{
$.post("<?php bloginfo( 'wpurl' ); ?>/wp-content/themes/fullscreen/function.php",
{
action : "process_thumbs" ,
numposts : posts,
results_div_id : output
});
} // end of JavaScript function myplugin_cast_vote
//]]>
</script>
<?php
add_action('wp_ajax_process_thumbs', 'my_action_callback');
add_action('wp_ajax_nopriv_process_thumbs', 'my_action_callback');
} // end of PHP function myplugin_js_header
?>
//functions.php
function my_action_callback(){
alert('sdfsdf');
$numposts = $_POST['numposts'];
$results_id = $_POST['results_div_id'];
die( "document.getElementById('$results_id').innerHTML = '$numposts'" );
}
我可能甚至不确定我的代码是否在正确的位置,因为我并不真正理解wordpress的例子。
主页执行进行ajax调用的插件函数。我添加了操作并将其指向functions.php中的回调函数。当然,在那时回调没有被运行。
有什么想法吗?
答案 0 :(得分:0)
您应该将数据发布到site_url('/ wp-admin / admin-ajax.php')而不是您的function.php