I've implemented some AJAX functionality for my plugin and it works fine as long as I'm not logged in as admin - then wp_verify_nonce
fails. It works for unauthorized users and authorized regular users too.
Here's my PHP class (I removed everything that is not relevant to the issue):
class My_Ajax {
function __construct() {
add_action( 'wp_ajax_geoip_citylist', array($this, 'geoip_citylist') );
add_action( 'wp_ajax_nopriv_geoip_citylist', array($this, 'geoip_citylist') );
add_action( 'wp_enqueue_scripts', array($this, 'geoip_localize_js'), 11 );
}
function geoip_citylist() {
if ( ! wp_verify_nonce($_POST['geoipNonce'], 'my_geoip_nonce') ) {
exit('Wrong nonce: ' . $_POST['geoipNonce']);
}
}
function geoip_localize_js() {
wp_localize_script(
'my_custom_js', 'myGeoipAjaxObject',
array(
'myGeoipNonce' => wp_create_nonce('my_geoip_nonce')
)
);
}
}
And here's my Javascript:
$.ajax({
url: myMainAjaxObject.ajaxUrl,
type: 'POST',
dataType: 'json',
data: {
geoipNonce: myGeoipAjaxObject.myGeoipNonce,
action: "geoip_citylist"
},
success: function(data) {
replacePhones(data.user_city, data.current_phone, data.city_list);
},
error: function(error) {
console.log(error);
}
});
The nonce in PHP error response seems to be correct, but it still doesn't validate for some reason.
I've spent days trying to figure out what's wrong and trying to google this in every possible way, but so far I couldn't find a solution that would work. There are unanswered questions with similar issues on stackoverflow, like this one which was created in 2011... I could just assume that nonces don't work with admin accounts in Wordpress, but I would like to at least get a confirmation before giving up.
UPDATE: I've tried to use just the code that I provided here and it still resulted with the same error (I also double checked the cache and it was refreshed).
UPDATE 2:
myMainAjaxObject
and its ajaxUrl
are inserted in another script. Since AJAX URL is same for every AJAX request, I just use the global one instead of redefining it every time. So this is not what's causing the error. Also if it wouldn't be defined it would never work. Once again, it doesn't work only for the admin, for other users and unauthorized users it works fine. And I do get "Wrong nonce: 73dd02f662" in the server response when I'm logged as admin. So I'm 100% sure that URL is correct.