Last active
May 29, 2018 17:10
-
-
Save alisonmonteiro/13338b093210cc2f6793736efe5ca866 to your computer and use it in GitHub Desktop.
Wordpress Ajax Example
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
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) // ... |
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
<form data-example="..."> | |
<input type="hidden" name="token" value="<?php echo wp_create_nonce('action-name') ?>"> | |
<input type="text" name="name" required /> | |
</form> |
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
<?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