Wednesday, 15 February 2012

I need some help about ajax in wordpress always return 0 -



I need some help about ajax in wordpress always return 0 -

this first time seek ajax in wordpress. need help.

in theme's functions.php, have:

function returnrandomposts(){ echo '123'; die(); } add_action('wp_ajax_returnrandomposts', 'returnrandomposts'); add_action('wp_ajax_nopriv_returnrandomposts', 'returnrandomposts');

and in single.php file, have

<script type="text/javascript"> jquery(document).ready(function($){ $.post("<?php echo admin_url("admin-ajax.php"); ?>", {"action": "returnrandomposts"}, function(response) { alert('response: ' + response); }); }); </script>

when run web page, alert "response: 0",

i hope can give me hints or tips, give thanks you.

update:

i checked has_filter function, new wp_ajax_xxxxx has add together $wp-filters, when phone call ajax wp-admin/admin-ajax.php, has_filter function homecoming false wp_ajax_xxxxx

anyone can give me hint?

finally, solved problem.

i have plugin, function is, preview theme adding "theme=name_of_theme" argument url.

( example, preview homepage "xxx.xx.xx/?theme=name_of_theme" )

the ajax failed in preview.

while add together same ajax function code online page (ie. activated theme), successes.

i hope experience may help somebody.

ajax wordpress

No comments:

Post a Comment