rest api - How to login via wordpress api and get user details?

I want to login via wordpresss api. I tried with "wp-jsonwpv2users" . but it just returns user 1 details.M

I want to login via wordpresss api. I tried with "/wp-json/wp/v2/users" . but it just returns user 1 details.

My goal is login via api and fetch all profile information.

I want to login via wordpresss api. I tried with "/wp-json/wp/v2/users" . but it just returns user 1 details.

My goal is login via api and fetch all profile information.

Share Improve this question asked Aug 19, 2018 at 2:33 Mohammad Shahnewaz SarkerMohammad Shahnewaz Sarker 1531 gold badge2 silver badges7 bronze badges 1
  • developer.wordpress/rest-api/using-the-rest-api/… – Jacob Peattie Commented Aug 19, 2018 at 3:34
Add a comment  | 

3 Answers 3

Reset to default 3

1. Install and activate JWT Authentication for WP REST API plugin, also install WP REST API plugin
2. Now you can run any wordpress default api from mobile app or any other source or by postman. for example hit this url from your app or by postman. https://example/wp-json/wp/v2/posts
3. By app or by postman, When you will login with valid details (using rest api) you will get back a token. To login and get token, run the following url by postman or by app https://example/wp-json/jwt-auth/v1/token
4. By this way you will get a token as shown in picture
Now use this token to get logged in user details, for example
5. make function in function.php

function checkloggedinuser()
{
$currentuserid_fromjwt = get_current_user_id();
print_r($currentuserid_fromjwt);
exit;
}

 add_action('rest_api_init', function ()
{
  register_rest_route( 'testone', 'loggedinuser',array(
  'methods' => 'POST',
  'callback' => 'checkloggedinuser'
  ));
});


6. Now again run this new url in postman or in app to get logged in user details. https://example/wp-json/testone/loggedinuser (replace example with your url)

Hello I have a faster and better solution to get the ID and more like the user avatar.

Just add this to your functions.php file:

/*
 * Insert some additional data to the JWT Auth plugin
 */
function jwt_auth_function($data, $user) { 
    $data['user_role'] = $user->roles; 
    $data['user_id'] = $user->ID; 
    $data['avatar']= get_avatar_url($user->ID);
    return $data; 
} 
add_filter( 'jwt_auth_token_before_dispatch', 'jwt_auth_function', 10, 2 );

Reference: How to ger ID user via API

I hope that's helped

Solved:

add_action( 'rest_api_init', 'register_api_hooks' );
function register_api_hooks() {
  register_rest_route(
    'custom-plugin', '/login/',
    array(
      'methods'  => 'POST',
      'callback' => 'login',
    )
  );
}
function login($request){
    $creds = array();
    $creds['user_login'] = $request["username"];
    $creds['user_password'] =  $request["password"];
    $creds['remember'] = true;
    $user = wp_signon( $creds, true );


    if ( is_wp_error($user) )
      echo $user->get_error_message();

    $id = $user->ID;
    $meta = get_user_meta($id);

    return $meta;
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742322366a4422069.html

相关推荐

  • rest api - How to login via wordpress api and get user details?

    I want to login via wordpresss api. I tried with "wp-jsonwpv2users" . but it just returns user 1 details.M

    3天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信