Created
April 11, 2016 19:59
-
-
Save JayWood/61d98ac12fb3fa0bf4b224ea4aca0f49 to your computer and use it in GitHub Desktop.
adding in nonce to post data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* SerializeOnDemand | |
* | |
* In theory you could do this inline, but sometimes its easier to just | |
* do it within a method and re-use it where you need to. | |
* | |
* @return {object} Serialized Object | |
*/ | |
var searializeOnDemand = function() { | |
// Go ahead and serialize the form data. | |
var form_data = jQuery( '#theForm' ).seraizlize(); | |
if ( window.MyAjax ) { | |
// At this point you know you have access to the localized object, | |
// so now you can append to the serialized data object. | |
form_data.postCommentNonce = window.MyAjax.postCommentNonce; | |
} | |
return form_data; | |
} | |
jQuery.post( ajaxurl, searializeOnDemand, function( response ) { | |
/** | |
* At this point, response should equal a json encoded response | |
* SO if you're doing something like this: | |
* | |
* wp_send_json_success( array( 'some_key' => 'some value' ) ) | |
* you can access it like so: | |
*/ | |
if ( response.success ) { | |
// Success data is stored within response.data | |
alert( response.data.some_key ); | |
} | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment