I'm developing a website by wordpress, customer want I build a custom login page and login by email, not username. And admin page with custom url '/manage/'

Below my code for login:

$email = $_POST['email'];
$pass = $_POST['password'];
$user = get_user_by('email', $email);
if (wp_check_password($pass, $user->data->user_pass, $user->ID)) {
   wp_set_current_user($user->data->ID, $user->data->user_login);
   do_action( 'wp_login', $user->data->user_login );
   $_SESSION['success'] = 'Hello ' . $user->data->display_name . '!';

My problem is:

When I submit, website return success and current user on Frontend changed. But when I try go to admin dashboard, it redirect me to wp-login.php and require me login again. I don't what is wrong or how to fix that.

So, please, help me!

  • You should avoid using $_SESSION, WP doesn't use it and it doesn't work on a lot of hosts
    – Tom J Nowell
    Oct 14, 2019 at 17:55

1 Answer 1


What you have should work - although you should not use $_SESSION (as Tom already noted). Also ID, user_pass, and user_login are all top level in the user object.

Here's what I would try:

$email = sanitize_email( $_POST['email'] );
$pass  = $_POST['password'];
$user  = get_user_by( 'email', $email );

if ( wp_check_password( $pass, $user->user_pass, $user->ID ) ) {
   wp_set_current_user( $user->ID );
   wp_set_auth_cookie( $user->ID );
   do_action( 'wp_login', $user->user_login );
   wp_safe_redirect( wp_get_referer() );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.