Skip to content

Instantly share code, notes, and snippets.

@imvarunkmr
Last active April 2, 2017 15:45
Show Gist options
  • Save imvarunkmr/501e11bcd317c10ed75f23f9c07b09c2 to your computer and use it in GitHub Desktop.
Save imvarunkmr/501e11bcd317c10ed75f23f9c07b09c2 to your computer and use it in GitHub Desktop.
Ajax handlers for WordPress
<?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