无法使wp_ajax钩子在wordpress中工作 //主页 $(文档).ready(函数(){ 我的投票(20); }); //插件 // //functions.php 函数my_action_callback(){ 警报(“sdfsdf”); $numposts=$_POST['numposts']; $results\u id=$\u POST['results\u div\u id']; die(“document.getElementById('$results_id')。innerHTML='$numposts'); }

无法使wp_ajax钩子在wordpress中工作 //主页 $(文档).ready(函数(){ 我的投票(20); }); //插件 // //functions.php 函数my_action_callback(){ 警报(“sdfsdf”); $numposts=$_POST['numposts']; $results\u id=$\u POST['results\u div\u id']; die(“document.getElementById('$results_id')。innerHTML='$numposts'); },wordpress,Wordpress,我甚至可能不确定我的代码是否在正确的位置,因为我没有真正理解wordpress的示例 主页执行进行ajax调用的插件函数。我添加这些操作并将其定向到functions.php中的回调函数。当然,此时回调没有运行 有什么想法吗?您应该将数据发布到站点的url('/wp admin/admin ajax.php'),而不是function.php //the homepage $(document).ready(function(){ myplugin_cast_vote(20); });

我甚至可能不确定我的代码是否在正确的位置,因为我没有真正理解wordpress的示例

主页执行进行ajax调用的插件函数。我添加这些操作并将其定向到functions.php中的回调函数。当然,此时回调没有运行


有什么想法吗?

您应该将数据发布到站点的url('/wp admin/admin ajax.php'),而不是function.php

//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'" );
}