Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

 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?

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.