Created
October 3, 2012 15:27
-
-
Save garyc40/3827562 to your computer and use it in GitHub Desktop.
Hooking into WordPress AJAX actions
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
// 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