I have a question.
I made a simply ajax function.
When I’m logged in, it works perfectly.
When I’m logged out, it returns me -1 (since WordPress 3.1)
Why ? I don’t understand.
Precisely, it returns -1 and my entire HTML code. (lol)
I’m gonna be crazy again.
PHP (in functions.php)
function say_coucou(){
check_ajax_referer( 'hello', 'nonce' );
echo "Hello";
die(); // this is required to return a proper result
exit;
}
add_action('wp_ajax_hello_hello', 'say_coucou');
add_action('wp_ajax_nopriv_hello_hello', 'say_coucou');
*JAVASCRIPT / JQUERY / AJAX (in footer.php) *
function blabla(){
var toSend = {
action:"hello_hello",
post_id: "<?php echo $wp_query->post->ID; ?>",
nonce: "<?php echo js_escape( wp_create_nonce('hello')); ?>"
};
url_action = 'http://www.***********/wp-admin/admin-ajax.php';
$.ajaxSetup({cache:true});
$.ajax({
url: url_action,
type:'POST',
data: toSend,
cache: false,
success:function(results)
{
alert(results)
}
});
}
Any help, please ?
Thanks.
Rarst said it worked for him both logged in and logged out, i can also confirm the same, here’s my ugly test code that works, very much just a hacked together version of your code(for testing).
Worked both logged out and logged in(obviously not in admin logged out, because there is no admin logged out).
I’d not realistically use the hooks so poorly in real code form, so don’t look to my hacky version of your code as a good example of how to do things, because it’s not.. (for illustration of working test code only).
Found It!
I had done a redirect, so my ajax function status was 302…
I deleted the redirect and it worked !
Thanks guys.