I’ve seen a couple of discussions about getting WordPress to regenerate a unique nonce for subsequent Ajax requests, but for the life of me I can’t actually get WordPress to do it– every time I request what I think should be a new nonce, I get the same nonce back from WordPress. I understand the concept of WP’s nonce_life and even setting it to something else, but that hasn’t helped me.
I don’t generate the nonce in the JS object in the header via localization- I do it on my display page. I can get my page to process the Ajax request, but when I request a new nonce from WP in the callback, I get the same nonce back, and I don’t know what I’m doing wrong… Ultimately I want to extend this so that there could be multiple items on the page, each with the ability to add/remove– so I need a solution that will allow multiple subsequent Ajax requests from one page.
(And I should say I’ve put all of this functionality into a plugin, so the front-end “display page” is actually a function included with the plugin…)
functions.php: localize, but I don’t create a nonce here
wp_localize_script('myjs', 'ajaxVars', array('ajaxurl' => 'admin-ajax.php')));
Calling JS:
$("#myelement").click(function(e) {
e.preventDefault();
post_id = $(this).data("data-post-id");
user_id = $(this).data("data-user-id");
nonce = $(this).data("data-nonce");
$.ajax({
type: "POST",
dataType: "json",
url: ajaxVars.ajaxurl,
data: {
action: "myfaves",
post_id: post_id,
user_id: user_id,
nonce: nonce
},
success: function(response) {
if(response.type == "success") {
nonce = response.newNonce;
... other stuff
}
}
});
});
Receiving PHP:
function myFaves() {
$ajaxNonce = 'myplugin_myaction_nonce_' . $postID;
if (!wp_verify_nonce($_POST['nonce'], $ajaxNonce))
exit('Sorry!');
// Get various POST vars and do some other stuff...
// Prep JSON response & generate new, unique nonce
$newNonce = wp_create_nonce('myplugin_myaction_nonce_' . $postID . '_'
. str_replace('.', '', gettimeofday(true)));
$response['newNonce'] = $newNonce;
// Also let the page process itself if there is no JS/Ajax capability
} else {
header("Location: " . $_SERVER["HTTP_REFERER"];
}
die();
}
Frontend PHP display function, amongst which is:
$nonce = wp_create_nonce('myplugin_myaction_nonce_' . $post->ID);
$link = admin_url('admin-ajax.php?action=myfaves&post_id=' . $post->ID
. '&user_id=' . $user_ID
. '&nonce=' . $nonce);
echo '<a id="myelement" data-post-id="' . $post->ID
. '" data-user-id="' . $user_ID
. '" data-nonce="' . $nonce
. '" href="' . $link . '">My Link</a>';
At this point I’d be really grateful for any clues or pointers in getting WP to regenerate a unique nonce for each new Ajax request…
UPDATE: I’ve solved my problem. The code snippets above are valid, however I changed the $newNonce creation in the PHP callback to append a microseconds string to ensure that it is unique on subsequent Ajax requests.
Here’s a very lengthy answer of my own question that goes beyond just addressing the question of generating unique nonces for subsequent Ajax requests. This is an “add to favorites” feature that was made generic for the purposes of the answer (my feature lets users add the post IDs of photo attachments to a list of favorites, but this could apply to a variety of other features that rely on Ajax). I coded this as a standalone plugin, and there are a few items missing– but this should be enough detail to provide the gist if you want to replicate the feature. It will work on an individual post/page, but it’ll also work in lists of posts (e.g. you can add/remove items to favorites inline via Ajax and each post will have its own unique nonce for each Ajax request). Keep in mind that there’s probably a more efficient and/or more elegant way to do this, and currently this works for Ajax only– I haven’t bothered yet to process non-Ajax $_POST data.
scripts.php
favorites.js (Lots of debug stuff that can be removed)
Functions (front-end display & Ajax action)
To output the Add/Remove Favorites link, simply call it on your page/post via:
Front-end display function:
Ajax action function:
I really have to question the reasoning behind getting a new nonce for each ajax request. The original nonce will expire, but it can be used more than once until it does. Having the javascript receive it through ajax defeats the purpose, especially providing it on an error case. (The purpose of nonces being a little security for associating an action with a user within a time frame.)
I’m not supposed to mention other answers, but I’m new and can’t comment above, so in regards to the posted “solution”, you are getting a new nonce every time but are not using it in the request. It would certainly be tricky to get the microseconds the same every time to match each new nonce created that way. The PHP code is verifying against the original nonce, and the javascript is supplying the original nonce…so it works (because it hasn’t expired yet).