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.

Read More
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();
}

Related posts

Leave a Reply

3 comments

  1. 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']) ){
    
  2. Isn’t action be the same as you sent?

        data: {
            action: "more_news",
            ...
    
       function more_news(){
          if ( !wp_verify_nonce($_POST['nonce'], 'more_news_nonce') ){
          ....
    

    I assume it should be

       function more_news(){
          if ( !wp_verify_nonce($_POST['nonce'], 'more_news') ){
          ....
    
  3. it should be,

     data: {
            action: "more_news",
            nonce: <?php echo wp_create_nonce( 'saiful_create_nonce' ); ?>,
            offset: offset
    },
    

    and wp function

    function more_news(){
      if ( ! check_ajax_referer( 'saiful_create_nonce', 'nonce' ) ){
        return false;
      }
     echo 'Awesome';
        die();
    }