Wordpress nonce check always false Wordpress nonce check always false wordpress wordpress

Wordpress nonce check always false


I've just ran into a similar issue and it turned out that all I had to do is to re-log as admin. I think it should also work in your case, because everything else in the provided code seems to be fine.

I guess it has something to do with how sessions are handled in Wordpress.


I do an almost identical interchange without problems.This is in a plugin (class), but that shouldn't matter.

PHP - initialize the javascript :

add_action( 'wp_print_scripts', array( &$this, 'enqueue_script') );

PHP - function enqueue_script:

wp_localize_script( 'B99-Portfolio', 'ajax', array(  'ajaxurl'       => admin_url( 'admin-ajax.php' ),                                                     'imgurl'        => content_url().'/uploads/portfolio-content/',                                                     'requestNonce'  => wp_create_nonce('b99-request-nonce')) );

JS - initiate the ajax request:

$.ajax({        type    : 'POST',        cache   : false,        url     : ajax.ajaxurl,        data    : {            action          : 'b99_ajax_request_items',            requestNonce    : ajax.requestNonce                     },        dataType: 'json',        error   : function(jqXHR, textStatus, errorThrown) {alert(jqXHR+" "+textStatus+" "+errorThrown);},        success : function( response ) {recieveAjax( response );}        });

PHP - recieve and handle the request ( function b99_ajax_request_items):

$nonce = $_POST['requestNonce'];        if ( ! wp_verify_nonce( $nonce, 'b99-request-nonce' ) ){            die ( 'security fail'.$nonce);        }

Make sure that you have enqueued the script prior to localizing it.

I'm using current versions of both jquery and wordpress and this works seamlessly on a local install of XAMPP. It looks pretty similar to your interchange, but maybe this is something to compare against.