我正在尝试删除一些cookie,这些cookie设置用于在用户特别想要注销网站时控制持久登录。
我以为这很简单:
function remove_user_cookie() {
unset($_COOKIE[\'woai\']);
unset($_COOKIE[\'woak\']);
}
add_action(\'wp_logout\', \'remove_user_cookie\');
我也尝试过:
function remove_user_cookie() {
if( $_GET[\'action\'] == \'logout\' ) :
unset($_COOKIE[\'woai\']);
unset($_COOKIE[\'woak\']);
endif;
}
add_action(\'wp_logout\', \'remove_user_cookie\');
但是,用户的cookie保持不变,因此他们会自动重新登录。
我不确定wp\\u注销操作是否不起作用,或者它是否与我自动登录用户的代码冲突。
现在,我在标题中有这个。php:
<?php if( !is_user_logged_in() ) :
// check if user has cookies
if ( isset($_COOKIE[\'woai\']) && isset($_COOKIE[\'woak\']) ) :
$args = array(
\'meta_key\' => \'login_key\',
\'meta_value\' => $_COOKIE[\'woak\'],
\'meta_compare\' => \'=\',
\'fields\' => \'ID\',
\'include\' => $_COOKIE[\'woai\']
);
$user_query = new WP_User_Query($args);
if( $user_query->results[0] != \'\' || $user_query->results[0] != NULL ) :
// get user id and login name
$user_id = $user_query->results[0];
$user_login = get_user_by( \'id\', $user_id );
// log the user in
wp_set_current_user( $user_id, $user_login->user_login );
wp_set_auth_cookie( $user_id );
do_action( \'wp_login\', $user_login->user_login );
endif;
endif; // end if cookies
endif; ?>
当用户登录时,他们会得到一个新的密钥集:
function set_user_cookie($user_login) {
// if they have cookies, delete them
if ( isset($_COOKIE[\'woai\']) && isset($_COOKIE[\'woak\']) ) :
unset($_COOKIE[\'woai\']);
unset($_COOKIE[\'woak\']);
endif;
$user_id = get_user_by( \'login\', $user_login );
// generate new key for user
$salt = wp_generate_password(20); // 20 character "random" string
$key = sha1($salt . $email . uniqid(time(), true));
// set new cookies
setcookie("woai", $user_id->ID, time()+31536000); /* expire in 1 year */
setcookie("woak", $key, time()+31536000); /* expire in 1 year */
// update the db
update_field(\'field_53c3de9cd031e\', $key, \'user_\'.$user_id->ID.\'\');
}
add_action(\'wp_login\', \'set_user_cookie\', 10, 1);
SO网友:De Paragon
(我在prestashop和3个定制php应用程序上为我的客户端创建了一个持久登录,一个用于两个基于wordpress的站点)。我也遇到过同样的挑战,但我就是这样解决的。
第一步
add_action(\'wp_logout\', \'doMyLogoutCall\');
add_action(\'init\', \'runInitLogoutProcess\');
第二步
function doMyLogoutCall()
{?>
<img style="display:none" src="<?php echo home_url();
?>?loadwordpressbyimage=1">
<?php
}
function runInitLogoutProcess()
{
// check if wordpress action is set to logout and clear cookie
if (isset($_GET)) {
if (isset($_GET[\'action\'])) {
if ($_GET[\'action\'] == \'logout\') {
setcookie(\'woai\', \'\', time() - 3600, COOKIEPATH, COOKIE_DOMAIN, false);
setcookie(\'woak\', \'\', time() - 3600, COOKIEPATH, COOKIE_DOMAIN, false);
}
}
// check if your wp_logout hook is called and do the same.
if (isset($_GET[\'loadwordpressbyimage\'])) {
if ($_GET[\'loadwordpressbyimage\'] == 1) {
setcookie(\'woak\', \'\', time() - 3600, COOKIEPATH, COOKIE_DOMAIN, false);
setcookie(\'woai\', \'\', time() - 3600, COOKIEPATH, COOKIE_DOMAIN, false);
}
}
}
//就是这样。