Skip to content

Instantly share code, notes, and snippets.

@alisonmonteiro
Last active May 29, 2018 17:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alisonmonteiro/13338b093210cc2f6793736efe5ca866 to your computer and use it in GitHub Desktop.
Save alisonmonteiro/13338b093210cc2f6793736efe5ca866 to your computer and use it in GitHub Desktop.
Wordpress Ajax Example
const url = '/wp-admin/admin-ajax.php'
const data = {
action: 'custom_action',
token: document.querySelector('[name="token"]').value,
name: document.querySelector('[name="name"]').value
}
customRequest(url, data) // ...
<form data-example="...">
<input type="hidden" name="token" value="<?php echo wp_create_nonce('action-name') ?>">
<input type="text" name="name" required />
</form>
<?php
add_action('wp_ajax_custom_action', 'custom_handle_function');
add_action('wp_ajax_nopriv_custom_action', 'custom_handle_function');
function custom_handle_function() {
check_ajax_referer('action-name', 'token');
// do stuff
wp_die();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment