add_action('rest_api_init', function () { register_rest_route('mps/v1', '/user-by-email', [ 'methods' => ['GET','POST'], 'permission_callback' => function () { return current_user_can('edit_users'); }, 'args' => [ 'email' => ['type' => 'string', 'required' => true], 'roles' => ['type' => 'array', 'required' => false], ], 'callback' => function (WP_REST_Request $req) { $email = sanitize_email($req->get_param('email')); $user = get_user_by('email', $email); if (!$user) { return new WP_Error('not_found', 'No user with that email', ['status'=>404]); } // If roles provided, update them if ($roles = $req->get_param('roles')) { wp_update_user([ 'ID' => $user->ID, 'roles' => array_map('sanitize_text_field', (array) $roles), ]); $user = get_userdata($user->ID); } return [ 'id' => $user->ID, 'username' => $user->user_login, 'email' => $user->user_email, 'roles' => $user->roles, ]; } ]); });