Ajax function returns -1

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).

function say_coucou(){
    check_ajax_referer( 'hello', 'nonce' );
    echo "Hello";
    die;
}
add_action('wp_ajax_hello_hello', 'say_coucou');
add_action('wp_ajax_nopriv_hello_hello', 'say_coucou');
add_action('admin_print_footer_scripts','blabla',20000);
add_action('wp_head','enj',20000);
add_action('wp_footer','blabla',20000);
function enj() {
    wp_enqueue_script('jquery');
}

function blabla(){
?>
<script type="text/javascript">

    jQuery(document).ready(function($){

        $('a#blabla').click(function(){

            var toSend = {
                action:"hello_hello",
                post_id: "1",
                nonce: "<?php echo esc_js( wp_create_nonce('hello') ); ?>"
            };
            url_action = "<?php echo admin_url('/admin-ajax.php'); ?>";
            $.ajaxSetup({cache:true});
            $.ajax({
                url: url_action,
                type:'POST',
                data: toSend,
                cache: false,
                success:function(results) {
                    alert(results)
                }
            });
        });
    });

</script>
    <a href="#" id="blabla">aaa</a>
    <?php
}

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).

Leave a Comment