Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Last active October 5, 2016 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommcfarlin/8cff4e80351ce357d34ded738a03f4c0 to your computer and use it in GitHub Desktop.
Save tommcfarlin/8cff4e80351ce357d34ded738a03f4c0 to your computer and use it in GitHub Desktop.
[WordPress] WordPress Ajax Responses in JSON
<?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.' )
);
}
<?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