Skip to content

Instantly share code, notes, and snippets.

@lkoudal
Last active April 17, 2020 22:09
Show Gist options
  • Select an option

  • Save lkoudal/5ca59dbd9168bd6d956a336b0ddc6c7c to your computer and use it in GitHub Desktop.

Select an option

Save lkoudal/5ca59dbd9168bd6d956a336b0ddc6c7c to your computer and use it in GitHub Desktop.
Using nonce from WordPress plugin in JavaScript file
jQuery(document).ready(function($) {
$.ajax({
url: ajaxurl, // WP variable, contains URL pointing to admin-ajax.php
type: 'POST',
data: {
'action':'get_custom_data',
'_ajax_nonce':plugin_ajax_object.nonce,
'user_data':'Some input from a user'
},
success: function( response ) {
// Here we do what happens if all is good - update the interface with a success message!?
},
error: function( response ) {
// Whooops, something went wrong!
// console.log(response);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment