今天缙哥哥在看公众号的时候发现,有人反映不能充VIP,但是缙哥哥自己登录查看一切正常,又解释不清楚,怎么办?于是问他要账号密码,登录上去看看。但是每次都找用户要账号密码,多少有点麻烦和不智能,还让人感觉麻烦和反感,So,将下面这段代码放入 functions.php 可以让管理员快速登录其他用户的账户。
WordPress大学之前已经发过一篇《WordPress 一键切换为其他用户身份登录》,今天,同样的功能,我们用纯代码来解决这个问题,并中文显示。
/**
* WordPress 后台管理员免密一键切换其他账号登录
* https://www.dujin.org/fenxiang/wp/10144.html
*/
function wpdx_user_switch_action($actions, $user){
$capability = (is_multisite())?'manage_site':'manage_options';
if(current_user_can($capability)){
$actions['login_as'] = '<a title="以此身份登录" href="'.wp_nonce_url("users.php?action=login_as&users=$user->ID", 'bulk-users').'">以此身份登录</a>';
}
return $actions;
}
add_filter('user_row_actions', 'wpdx_user_switch_action', 10, 2);
function wpdx_handle_user_switch_action($sendback, $action, $user_ids){
if($action == 'login_as'){
wp_set_auth_cookie($user_ids, true);
wp_set_current_user($user_ids);
}
return admin_url();
}
add_filter('handle_bulk_actions-users','wpdx_handle_user_switch_action', 10, 3); |
/**
* WordPress 后台管理员免密一键切换其他账号登录
* https://www.dujin.org/fenxiang/wp/10144.html
*/
function wpdx_user_switch_action($actions, $user){
$capability = (is_multisite())?'manage_site':'manage_options';
if(current_user_can($capability)){
$actions['login_as'] = '<a title="以此身份登录" href="'.wp_nonce_url("users.php?action=login_as&users=$user->ID", 'bulk-users').'">以此身份登录</a>';
}
return $actions;
}
add_filter('user_row_actions', 'wpdx_user_switch_action', 10, 2);
function wpdx_handle_user_switch_action($sendback, $action, $user_ids){
if($action == 'login_as'){
wp_set_auth_cookie($user_ids, true);
wp_set_current_user($user_ids);
}
return admin_url();
}
add_filter('handle_bulk_actions-users','wpdx_handle_user_switch_action', 10, 3);
刷新 WordPress 后台,进入用户管理,你就会发现多了一个“以此身份登录”的按钮,点一下就自动退出管理员账号,并免密码以该账号登录。

怎么样?是不是方便测试啦!
来源:
https://www.wpdaxue.com/wordpress-user-switch.html
微信公众号
手机浏览(小程序)
Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(): Failed to enable crypto in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_32361.jpg): failed to open stream: operation failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57