Multiple ajax nonce requests

I have a feature in my current project that will be allow multiple ajax requests – I’ve got a single nonce request worked out (via this tute), but am somewhat unsure how to figure in multiple nonce requests.

functions.php: localize, create a single nonce

Read More
 wp_localize_script( 
      'B99-Portfolio', 
      'B99ajax', 
      array( 
           'ajaxurl'        => admin_url( 'admin-ajax.php' ),
           'requestNonce'   => wp_create_nonce('b99-request-nonce') 
      )
 );

calling js:

$.ajax({
        type    : 'POST',
        url     : B99ajax.ajaxurl,
        data    : {
            action          : 'b99-ajax-submit',
            requestNonce    : B99ajax.requestNonce
        },...

receiving php

public function b99_ajax_submit() {
        $nonce = $_POST['requestNonce'];
        if ( ! wp_verify_nonce( $nonce, 'b99-request-nonce' ) )

... sql, response

At this point, I can reuse this nonce repeatedly, but I think that defeats the purpose of the nonce check. My solution will be to request a new nonce in the receiving php, assign B99ajax js object that variable, and send it back to the requesting page for the “next” ajax sequence.

Is there a better way?

Related posts

Leave a Reply

1 comment

  1. That is basically the way WordPress does it and pretty much the only way to do it, simply have your receiving PHP function create a new nonce add send it back with your response, then just update the value on your JS before the next round.