Ajax and multiple nonces
-
I have a challenge that may be more complex than this board can help with…
I’m writing a plugin that allows people to add or remove photos to a “Lightbox” that gets attached to their user metadata – sort of like a “favorite posts” function that stores the post IDs of photos in an array. The Lightbox is binary: if the photo isn’t in the Lightbox it gets added, if the photo is in the Lightbox, it gets removed. The user metadata is updated each time accordingly.
I’ve built a graceful degrade that processes the add and remove functions with or without Ajax. The huge problem I’m having is that when the actions are performed and processed via Ajax, the Javascript has to be updated dynamically to regenerate the nonces that go along with the add and remove actions. To make it even more challenging, sometimes photos are displayed individually, and other times thumbnails of photos are displayed in a grid, and I’d like users to be able to add/remove photos to their Lightbox from the grid as well as on the individual photo pages. In this sense there could be multiple add/remove actions at any time on one page.
Currently I can’t figure out how to dynamically update and regenerate the nonces for the add and remove functions for the Ajax calls.
This is where I’m hoping to get some tips– how does one request a new nonce in the receiving php, assign the Ajax js object that variable, and then send it back to the requesting page for the “next” ajax sequence(s)?
I’ve read these suggestions and tips – the first link has been the most helpful in terms of the concept of requesting a new nonce in the receiving PHP function(s), but I still can’t figure out how to dynamically regenerate the Javascript object variables accordingly:
1) https://wordpress.stackexchange.com/questions/19826/multiple-ajax-nonce-requests
2) https://www.garyc40.com/2010/03/5-tips-for-using-ajax-in-wordpress/
3) https://wp.tutsplus.com/tutorials/creative-coding/capabilities-and-nonces/
- The topic ‘Ajax and multiple nonces’ is closed to new replies.