Answer the question
In order to leave comments, you need to log in
Wordpress authorization for a user through the admin panel without a password, how?
I have a personal account website where each advertiser sets up their ads in our mini network, there is also a personal account for webmasters who add their resources to our network and comforts and joys, there are errors or difficulties with filling in, most often the user turns to the support for that figured it out. And now we need to implement such functionality so that our support agent can log in under the user who turns to us for help. Log in using the button from the admin panel without a user password. Please tell me a plugin in which this can already be implemented or tell me where to dig, because it seems to me that I have already shoveled everything that is possible on wp-kama and the code.
Thanks to everyone who responded, wrote such a clumsy plug =)
<?php
defined('ABSPATH') or die("Stop stop stop.. bro!");
/*
Plugin Name: loginME
Description: loginME
Version: 1.0
Author: dima
Author URI: #
Text Domain: loginME
*/
class loginME{
protected static $instance;
public static function init() {
is_null(self::$instance) AND self::$instance = new self;
return self::$instance;
}
public function __construct() {
add_filter('user_row_actions', array( $this, 'user_row_actions'),10,2);
add_action('init', array($this, 'loginME_init'));
add_action("wp_footer", function(){
if(isset($_COOKIE["isUSERCHANGER"])):
?>
<div style="position: fixed;
background-color: #232050;
padding: 5px;
border-radius: 5px;
bottom: 30px;
right: 30px;">
<a style="color: #fff;"
href="<?=home_url();?>/?switch_user=switch_user&user_id=<?=$_COOKIE["admin_id"];?>&clear=<?=$_COOKIE["isUSERCHANGER"];?>">
<div>Назад в админ панель ></div>
</a>
</div>
<?php
endif;
});
}
public function loginME_init() {
add_action('admin_action_switch_user', array( $this, 'switch_user'));
add_filter( 'query_vars', function( $query_vars ){
$query_vars[] = 'switch_user';
return $query_vars;
});
add_action( 'template_redirect', function(){
$switch_user = get_query_var('switch_user');
if($switch_user === 'switch_user'){
if(current_user_can('manage_loginME') || isset($_COOKIE["isUSERCHANGER"])){
$admin_id = 0;
$admin_secret = 0;
if(!isset($_COOKIE["isUSERCHANGER"])){
$admin_id = $GLOBALS['current_user']->data->ID;
$admin_secret = $GLOBALS['current_user']->data->ID.$GLOBALS['current_user']->data->user_pass;
}
$id = (isset($_GET['user_id']) ? $_GET['user_id'] : $_POST['user_id']);
$user = get_user_by( 'id', $id );
if(!isset($_GET['clear'])){
global $current_user;
unset($current_user);
nocache_headers();
wp_clear_auth_cookie();
wp_set_current_user($id, $user->user_login);
do_action( 'wp_login', $user->user_login );
wp_set_auth_cookie($id, true);
if(!isset($_COOKIE["isUSERCHANGER"])){
setcookie("isUSERCHANGER", md5($admin_secret), time()+3600);
setcookie("admin_id", $admin_id, time()+3600);
}
wp_safe_redirect(home_url());
}else{
unset($_COOKIE['isUSERCHANGER']);
setcookie('isUSERCHANGER', null, -1);
unset($_COOKIE['admin_id']);
setcookie('admin_id', null, -1);
global $current_user;
unset($current_user);
nocache_headers();
wp_clear_auth_cookie();
wp_set_current_user($id, $user->user_login);
do_action( 'wp_login', $user->user_login );
wp_set_auth_cookie($id, true);
wp_safe_redirect(admin_url( 'users.php'));
}
die();
}
}
});
}
public function switch_user()
{
$id = (isset($_GET['user_id']) ? $_GET['user_id'] : $_POST['user_id']);
if(isset($id)){
wp_safe_redirect( home_url()."/?switch_user=switch_user&user_id=".$id);
die();
}
}
public function user_row_actions($actions, $user)
{
if(current_user_can('manage_loginME')){
$action_name = "switch_user";
$action = '?action='.$action_name.'&user_id='.$user->ID;
$actions['loginME'] = '<a href="'.admin_url( "admin.php". $action ).'" aria-label="">Авторизация</a>';
}
return $actions;
}
}
register_activation_hook( __FILE__, function (){
$role = get_role( 'administrator' );
$role->add_cap( 'manage_loginME' );
});
register_deactivation_hook( __FILE__, function (){
$role = get_role( 'administrator' );
$role->remove_cap( 'manage_loginME' );
});
add_action('plugins_loaded', array('loginME', 'init'));
Answer the question
In order to leave comments, you need to log in
It is extremely incorrect to log in under the user.
If you need the functionality of filling in user fields, then nothing prevents you from uploading them to the manager's office and editing them there.
In general, authorization without a password is elementary
nocache_headers();
wp_clear_auth_cookie();
wp_set_auth_cookie( $user_id );
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question