I just simple edit below function with few lines of code and it works like charm.
public function validate_auth_cookie() {
global $json_api;
if (!$json_api->query->cookie) {
$json_api->error(“You must include a ‘cookie’ authentication cookie. Use the create_auth_cookie method.”);
}
$valid = wp_validate_auth_cookie($json_api->query->cookie, ‘logged_in’) ? true : false;
$userid = wp_validate_auth_cookie($json_api->query->cookie, ‘logged_in’);
// print_r($userid);
// print_r(get_userdata( $userid ));
$data = get_userdata( $userid );
$username = $data->user_login;
// print_r($username );
// require(‘../wp-blog-header.php’);
$user_login = $username;
$user = get_userdatabylogin($user_login);
// print_r($user);
$user_id = $user->ID;
$dsf=wp_set_current_user($user_id, $user_login);
// print_r($dsf);
$sdf=wp_set_auth_cookie($user_id);
// print_r($sdf);
$datas=do_action(‘wp_login’, $user_login);
// print_r($datas);
// exit;
wp_redirect( get_site_url(), 301 ); //exit;
// exit;
if($userid) {
$creds = array();
// $creds[‘user_login’] = ‘add’;// $json_api->query->username;
// $creds[‘user_password’] = ‘add’; // $json_api->query->password;
$creds[‘remember’] = true;
$user = wp_signon( $creds, false );
if ( is_wp_error($user) )
echo $user->get_error_message();
wp_redirect( get_site_url(), 301 ); exit;
}
else
{
return array(“valid” => $valid);
}
}
Thanks Ali Qureshi your plugin works great…
glad it worked for you. Thanks.
Hi,
Through this User API is it possible to validate the user who has logged in from some other sites database and when they come to wordpress (which has separate databse) it should show as logged in user. I dont want user to login again in wordpress to write the blogs. Please suggest.
It’s not designed for this task. It is basically REST API, authenticating user with the cookie value and using it for RESTful calls.
You need SSO (single Sign on) solution. See these plugins >> https://wordpress.org/plugins/tags/single-sign-on
hi
Thanks For the code..!!
but still one question..
how to call login api??? i added above code in my project
and whats the parameter to pass
please give to demo