wp_verify_nonce doesn’t return true on server when it matches the nonce

$.ajax({
    type: "POST",
    dataType: "text",
    url: ajaxurl,
    data: {
            action: "more_news",
            nonce: nonce,
            offset: offset
    },
    success : function(data, textStatus, jqXHR){
        console.log( nonce );
        console.log( data );
        console.log( textStatus );
    }
});

Checking for this AJAX request nonce locally works perfectly. As seen below, my console prints my nonce, “Awesome”, and then “success”.

9a91a5fdca
Awesome
success

When on my server, my console prints my nonce TWICE (thus proving to me they’re exactly the same), then success.

e2ca4eca80
e2ca4eca80
success

I’m… confused… to say the least. How is this possible? and how do I solve it?

add_action( 'wp_ajax_more_news', 'more_news' );
add_action( 'wp_ajax_nopriv_more_news', 'more_news' );

function more_news(){
    if ( !wp_verify_nonce($_POST['nonce'], 'more_news_nonce') ){
        exit($_POST['nonce']);
    }
    echo 'Awesome';
    die();
}

3 Answers
3

The second parameter for wp_verify_nonce() is the action. That part is not in your question, but I guess it should be called like:

if ( !wp_verify_nonce($_POST['nonce'], $_POST['action']) ){

Leave a Comment