有时候我们可能需要登入到其他用户的账号里去,但即使我们是管理员,我们也不知道其他用户的密码,无法登入,所以这里提供一个一键登入其他用户账户的方法,主要是将如下代码添加到主题的functions.php文件中:
/**
* 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);
即可实现后台管理员一键登入其余用户账号,在后台用户列表里就可以看到: