Skip to content

Instantly share code, notes, and snippets.

@garyc40
Created October 3, 2012 15:27
Show Gist options
  • Save garyc40/3827562 to your computer and use it in GitHub Desktop.
Save garyc40/3827562 to your computer and use it in GitHub Desktop.
Hooking into WordPress AJAX actions
// if both logged in and not logged in users can send this AJAX request,
// add both of these actions, otherwise add only the appropriate one
add_action( 'wp_ajax_nopriv_myajax-submit', 'myajax_submit' );
add_action( 'wp_ajax_myajax-submit', 'myajax_submit' );
function myajax_submit() {
// get the submitted parameters
$postID = $_POST['postID'];
// generate the response
$response = json_encode( array( 'success' => true ) );
// response output
header( "Content-Type: application/json" );
echo $response;
// IMPORTANT: don't forget to "exit"
exit;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment