I am working on WordPress startup from YII2. Below is my code.
function.php (WP)
function autologin()
{
$strCookie = 'PHPSESSID=' . $_COOKIE['PHPSESSID'] . '; path=/';
session_write_close();
$ch = curl_init("http://example.com/testregister/wplogin");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt( $ch, CURLOPT_COOKIE, $strCookie );
$response = curl_exec($ch);
curl_close($ch);
$newres = json_decode($response);
$email = $newres->email;
$password = $newres->password;
$result = $newres->result;
if($result == 1)
{
$creds = array();
$creds['user_login'] = $email;
$creds['user_password'] = $password;
$creds['remember'] = false;
$user = wp_signon( $creds );
if ( is_wp_error( $user ) )
{
echo $user->get_error_message();
}
}
else
{
wp_destroy_current_session();
wp_clear_auth_cookie();
do_action( 'wp_logout' );
}
}
add_action( 'init', 'autologin' );
testregister / wplogin: (YII2)
public function actionWplogin()
{
$userEmail = Yii::$app->user->identity->Email;
$userpw = Yii::$app->user->identity->Password;
$result = 1;
if($userEmail == "")
{
$result = 0;
}
return '{"email":"'.$userEmail.'", "password":"'.$userpw.'" ,"result":"'.$result.'"}';
}
This function is called every time the wordpress site loads. but the first time he didnβt enter the site.
If I use wp_redirect( esc_url( home_url() ) );, the login function works fine, but wp-admin does not work after using the redirect.
source
share