Last active
April 2, 2017 15:45
-
-
Save imvarunkmr/501e11bcd317c10ed75f23f9c07b09c2 to your computer and use it in GitHub Desktop.
Ajax handlers for WordPress
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_action_slug', 'action_slug' ); | |
add_action( 'wp_ajax_nopriv_action_slug', 'action_slug' ); | |
function action_slug() { | |
check_ajax_referer('form_nonce'); | |
$errors = array(); // Array to hold validation errors | |
$data = array(); // Array to pass back data | |
// Validation logic if any... | |
// return a response | |
if( ! empty($errors) ) { | |
// if there are errors, return those errors | |
$data['success'] = false; | |
$data['errors'] = $errors; | |
} | |
else { | |
// if there are no errors, process the form and return a message | |
// form processing logic | |
$data['success'] = true; | |
$data['message'] = 'Success'; | |
} | |
echo json_encode($data); | |
wp_die(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment