-
-
Save tommcfarlin/8cff4e80351ce357d34ded738a03f4c0 to your computer and use it in GitHub Desktop.
[WordPress] WordPress Ajax Responses in JSON
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 | |
// We're going to try to look up a user by his/her email address. | |
$user = get_user_by( 'email', 'tom@mattreport.com'); | |
/* Next, if the $user object exists, we'll respond with a JSON-based | |
* response that includes the user's ID. Since this email address | |
* does *not* exist, we're going to response with an error. | |
*/ | |
if ( false !== $user ) { | |
// Removed since it's already been covered. | |
} else { | |
wp_send_json_error( | |
new WP_Error( '-1', 'The specified user does not have an account.' ) | |
); | |
} |
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 | |
// We're going to try to look up a user by his/her email address. | |
$user = get_user_by( 'email', 'tom@tommcfarlin.com'); | |
/* Next, if the $user object exists, we'll respond with a JSON-based | |
* response that includes the user's ID. | |
*/ | |
if ( false !== $user ) { | |
wp_send_json_success( | |
array( 'User ID' => get_user_by_id( 'id', $user->ID ) ) | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment