Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[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
You can’t perform that action at this time.