admin管理员组文章数量:1128017
I am using the API currently to search for a user by email and then getting the ID if the user and then executing a change to the account based on the ID. The only issue is, it seems that when I search, the search doesn't require it to be exact. I am wanting it to be exact.
Example is, I search for a user using:
"GET /wp-json/wp/v2/[email protected] HTTP/1.1" 200
It comes back with the correct user, but if I also tried:
"GET /wp-json/wp/v2/users?search=username@gmai HTTP/1.1" 200
That also came back with the user. I am developing just using a test site so I only have a few users, but I foresee that this could be a problem. I am needing to search for one specific user by email at a time and I was hoping that when I got a result back I could validate it by checking if the email address shown in the json search result matched the email that was passed to it, but it doesn't look like it displays the email address in the results either.
Does anyone have any suggestions on what I can do to get things a bit closer to what I am after?
I am using the API currently to search for a user by email and then getting the ID if the user and then executing a change to the account based on the ID. The only issue is, it seems that when I search, the search doesn't require it to be exact. I am wanting it to be exact.
Example is, I search for a user using:
"GET /wp-json/wp/v2/[email protected] HTTP/1.1" 200
It comes back with the correct user, but if I also tried:
"GET /wp-json/wp/v2/users?search=username@gmai HTTP/1.1" 200
That also came back with the user. I am developing just using a test site so I only have a few users, but I foresee that this could be a problem. I am needing to search for one specific user by email at a time and I was hoping that when I got a result back I could validate it by checking if the email address shown in the json search result matched the email that was passed to it, but it doesn't look like it displays the email address in the results either.
Does anyone have any suggestions on what I can do to get things a bit closer to what I am after?
Share Improve this question asked Oct 15, 2018 at 8:58 MostHatedMostHated 113 bronze badges 5- I'd suggest looping over the results and comparing the searched-for email address and the result's email address and check for an exact match. – Jacob Peattie Commented Oct 15, 2018 at 9:04
- That would be no problem, the only thing is, when I search I am not seeing the email address show up in the results. Is there something else I have to pass along with it to show the email as well? – MostHated Commented Oct 15, 2018 at 9:47
- Oh, you're right, the email address isn't included in the response. At least for an unauthenticated request. – Jacob Peattie Commented Oct 15, 2018 at 10:01
- I am making an authenticated request using Oauth2, I am still not seeing an email address though? – MostHated Commented Oct 15, 2018 at 10:02
- By that all I meant is that I only tried an unauthenticated request tried to double check what you were saying. It could also be true for authenticated requests. – Jacob Peattie Commented Oct 15, 2018 at 10:03
1 Answer
Reset to default 1I was able to accomplish this by installing a plugin called "PHP Snippits" that lets you add code as if you were adding it to functions.php, but through the admin panel. Then I did some reasearch on how to add a custom endpoint and then how to query a user and came up with this. It seems to be working well, and is better than what I had originally hoped for. I just pass the endpoint the email and I get back the specific userID I needed.
add_action( 'rest_api_init', function () {
register_rest_route( 'endpoint/v1', 'email/(?P<stringvar>[^/]+)', array(
'methods' => 'GET',
'callback' => 'user_email',
'permission_callback' => function () {
return current_user_can('edit_others_posts');
},
) );
});
function user_email($data) {
// Get user by their email address
$user = get_user_by( 'email', $data['stringvar']);
$userId = $user->ID;
$user_data = [$userId, $data['stringvar']];
wp_reset_postdata();
return rest_ensure_response($user_data);
}
本文标签:
版权声明:本文标题:rest api - How might I go about using the API to search for a user by email, but ensuring that it is an exact match? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736703223a1948537.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论