Getting user data via ajax


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:

  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.

, , , , user8463989 2 years 2020-09-05T06:10:21-05:00 0 Answers 69 views 0

Leave an answer