admin管理员组文章数量:1122846
Im looking to get user information by email address using the latest version of the wordpress restapi.
I have registered the user email field and it returns on both the "/wp-json/wp/v2/users/" and the "/wp-json/wp/v2/users/" calls but im looking for a way to basically get user by email. I cant seem to find a way to filter or search by email field though, any thoughts?
Im looking to get user information by email address using the latest version of the wordpress restapi.
I have registered the user email field and it returns on both the "/wp-json/wp/v2/users/" and the "/wp-json/wp/v2/users/" calls but im looking for a way to basically get user by email. I cant seem to find a way to filter or search by email field though, any thoughts?
- if the core endpoints don't help you, you can create a new one : developer.wordpress.org/rest-api/extending-the-rest-api/… – mmm Commented Apr 26, 2018 at 17:04
- Maybe you take a look here (found by using google) wp-api sample and grab the code (and correct deprecated stuff) you need?! – Charles Commented Apr 26, 2018 at 17:54
3 Answers
Reset to default 6You can use the search argument
https://example.com/wp-json/wp/v2/users/[email protected]
A tad late to the party, but the trick is to make sure your search query is url-escaped (i.e. "%40" instead of "@").
The following fetch call worked on my site:
https://example.com/wp-json/wp/v2/users/?search=myemail%40domain.com
Or for a more friendly input experience (that doesn't require escaping special characters) I created this function:
function get_user_by_email(email) {
return fetch( `https://example.com/wp-json/wp/v2/users/?${new URLSearchParams({ search: email })}` )
.then(response => response.json());
}
get_user_by_email("[email protected]")
.then(console.log);
You need to add the database field user_email
to the search columns for user REST API query. Therefore you can use the filter rest_user_query
.
add_filter( 'rest_user_query', function ( array $prepared_args ) {
$prepared_args['search_columns'][] = 'user_email';
return $prepared_args;
} );
Now it works, if you search the email like:
https://example.com/wp-json/wp/v2/users/[email protected]
本文标签: WP Rest API Get User by Email
版权声明:本文标题:WP Rest API: Get User by Email 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736293406a1929140.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论