I have a voting system where a list of posts are printed out using an archive template. Each post's voting button has html data attributes containing the vote-id and the vote-nonce.
When the voting button is clicked an AJAX call is made containing the vote-id, and the vote-nonce. These should be verified server side but they appear to be failing wp-verify-nonce. Debugging and echoing the posted data confirms that it is identical to the original data.
AJAX PHP Function:
function submit_vote() { $vote_id = intval($_POST['vote_id']); $vote_nonce = sanitize_text_field($_POST['vote_nonce']); $action = sanitize_text_field($_POST['vote_nonce']); if ( !wp_verify_nonce($vote_nonce,'vote-nonce-' . $vote_id)) die(-1); $response = json_encode( array( 'success' => true ) ); die($response); } add_action( 'wp_ajax_submit_vote', 'submit_vote' ); NONCE Generation:
wp_create_nonce( 'vote-nonce-' . get_the_ID() ); AJAX JavaScript call:
jQuery(document).ready(function(){ jQuery(".post-voting").click(function(){ vote_nonce = jQuery(this).data('vote-nonce'); vote_id = jQuery(this).data('vote-id'); jQuery.post( vote_ajax.url, { action: 'submit_vote', vote_id: vote_id, vote_nonce: vote_nonce }, function( data ) { alert( jQuery.parseJSON(data) ); } ); }); }); The ajax request appears to be just dying with an empty response (not -1)
die(-1)which should bedie('-1')for your voting system. Did youvar_dump( $_REQUEST )to make sure all data are there?wp_create_nonce( 'vote-nonce' )and thenwp_verify_nonce($vote_nonce,'vote-nonce'), to see if it has to do with the identifierwp_get_session_token()tokens which are based on cookies e.gLOGGED_IN_COOKIE