admin管理员组文章数量:1323335
I am trying to list registered users on the frontend which have a role of 'member'. I have a lot of advanced custom fields for the users though and don't know how to get those fields. I installed the 'AFC to rest' plugin which gave me an endpoint which had all the advanced custom fields for the user but no the standard ones like first name, last name etc.
So, I don't know if it would be better to forget the ACF to rest plugin and do it differently or use the plugin but somehow get the standard user fields. Currently I am creating my own route:
add_action( 'rest_api_init', 'rest_members_endpoint' );
function rest_members_endpoint() {
$namespace = 'rest/v1/';
register_rest_route( $namespace, 'members/', array(
'methods' => 'GET',
'callback' => 'list_members',
) );
}
function list_members( $request ) {
$params = $request->get_params();
$args = array(
'role' => 'member',
);
$wp_user_query = new WP_User_Query( $args );
$members = $wp_user_query->get_results();
return new WP_REST_Response(array('members' => $members), 200);
}
The ajax:
$.ajax({
method: 'GET',
url: mySiteData.api_url + 'members/',
beforeSend: function ( xhr ) {
xhr.setRequestHeader( 'X-WP-Nonce', mySiteData.nonce );
},
})
This gives me back ID, display name, user_email, user_nicename but there is not first name and last name, and no Advanced custom fields.
I am trying to list registered users on the frontend which have a role of 'member'. I have a lot of advanced custom fields for the users though and don't know how to get those fields. I installed the 'AFC to rest' plugin which gave me an endpoint which had all the advanced custom fields for the user but no the standard ones like first name, last name etc.
So, I don't know if it would be better to forget the ACF to rest plugin and do it differently or use the plugin but somehow get the standard user fields. Currently I am creating my own route:
add_action( 'rest_api_init', 'rest_members_endpoint' );
function rest_members_endpoint() {
$namespace = 'rest/v1/';
register_rest_route( $namespace, 'members/', array(
'methods' => 'GET',
'callback' => 'list_members',
) );
}
function list_members( $request ) {
$params = $request->get_params();
$args = array(
'role' => 'member',
);
$wp_user_query = new WP_User_Query( $args );
$members = $wp_user_query->get_results();
return new WP_REST_Response(array('members' => $members), 200);
}
The ajax:
$.ajax({
method: 'GET',
url: mySiteData.api_url + 'members/',
beforeSend: function ( xhr ) {
xhr.setRequestHeader( 'X-WP-Nonce', mySiteData.nonce );
},
})
This gives me back ID, display name, user_email, user_nicename but there is not first name and last name, and no Advanced custom fields.
Share Improve this question asked Sep 5, 2020 at 11:01 user8463989user8463989 5931 gold badge8 silver badges24 bronze badges1 Answer
Reset to default 0Once you get the id of the user in members variable. You then have to use get_post_meta
to get the custom data of members because ACF uses meta boxes. After that merge the data with the members variable and then pass the variable to WP_REST_Response class. I would have commented but that reputation, so posted answer, Hope you don't mind and hope it helps.
本文标签: advanced custom fieldsGetting user data via ajax
版权声明:本文标题:advanced custom fields - Getting user data via ajax 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742136218a2422382.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论