1
//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的例子中,正確的地方。無法獲得wp_ajax鉤在WordPress
主頁執行ajax調用的插件函數。我添加了這些動作並將其指向functions.php中的回調函數。當然在那個時候回調沒有被執行。
任何想法?