我正在尝试从www.my auth site.org中获取cookie的数据后,试图加载贡献者的用户配置文件。如果WordPress的WP_USERS表中尚不存在cookie的用户名,则我正在WP_USERS表中插入所需的用户数据,但是如果用户名已经存在,那么我想加载当前的用户仪表板,以便他可以在我的网站上发布新文章,与我的网站相同,当他登录http://www.mysiteurl.org/wp-login.php。
时,他可以做但是,当我将其重定向到http//www.mysiteurl.org/wp-admin/post-new.php时它再次重定向到wp-login.php,最终被重定向到http://myauthsite.org(即,else part)
以下是我写过的代码.php文件
function redirect_login_page(){
$page_viewed = basename( $_SERVER['REQUEST_URI'] );
global $user;
$temp = getLoggedIn(); // to check if cookie exist by using custom getLoggedIn()
if( $page_viewed== "wp-login.php" && isset( $temp ) ) { // if user wants to login and cookie is set
$myusername=getLoggedIn(); //get username details from cookie
// to check if user already exist in wp_users table
$response = wp_remote_get( "http://www.mysiteurl.org/demo.php?user=$myusername" );
$ext_auth = json_decode( $response['body'], true );
if( $ext_auth['result'] == 0 ) { //if cookie user does not already exist
/*
some of my required code
......
*/
$userdata = array(
'user_email'=> $myuseremail,
'user_login' => $myusername,
'user_nicename' => $myusername,
'user_pass'=> $myuserpassword
);
$new_user_id = wp_insert_user( $userdata ); // A new user has been created with details from cookie
// Load the new user info
$user = new WP_User ($new_user_id);
} else if( $ext_auth['result'] == 1 ) {
// External user already exist, try to load the user info from the WordPress wp_users table
$user = new WP_User ($ext_auth['id']);
}
$temppassword="password from wp_users table"; //
$creds = array();
$creds['user_login'] = $myusername;
$creds['user_password'] = $temppassword;
$creds['remember'] = true;
$user = wp_signon( $creds, false );
wp_set_current_user($user->ID);
if ( is_wp_error($user) )
echo $user->get_error_message();
//wp_redirect('http//www.mysiteurl.org/wp-admin/profile.php');
//NOW $user has all details of current user
// But if I am redirecting it to dashboard, then instead of loading user profile it is getting redirected to wp-login.php
exit();
} //eoif
else{ //if cookie is not set redirect to auth to login and set cookie
wp_redirect('http://www.myAuthsite.org')
}
}
add_action( 'init','redirect_login_page' );
看起来您正在直接使用外部API的用户ID。您可以验证那些用户ID是否相同?看起来您似乎并没有将新创建的用户ID传递给外部验证源以使其同步,也不是可行的,因为每个系统都应该能够设置自己的ID。