Can’t get username using current_user with WP-API inside functions.php

I can’t get WP-API to show the current logged-in username for some reason it just shows an id : 0 and some other data.

functions.php

Read More
add_action( 'plugins_loaded', 'get_user_info' );

function get_user_info(){
    global $current_user;
    get_currentuserinfo();

    return $current_user;
}

add_action( 'rest_api_init', function () {
    register_rest_route( 'cala', '/get', array(
        'methods' => 'GET',
        'callback' => 'get_user_info',
    ) );
} );

JSON

{
  "data": {

  },
  "ID": 0,
  "caps": [

  ],
  "cap_key": null,
  "roles": [

  ],
  "allcaps": [

  ],
  "filter": null
}

I am not sure if this is the best way either. Please help thanks.

Related posts

1 comment

  1. Id “0” means the current user is logged out (visitor). Check you session status when you are firing the request.

    Try wp_get_current_user() or get_current_user_id() maybe it will work better for you.

Comments are closed.