0

My theme allows me to create a front-end login form in a normal blog page. Now, I wish to redirect all the users to that page and not to wp-login.php. I have used the below code snippet.

function redirect_login_page(){
   $page_viewed = basename($_SERVER['REQUEST_URI']);
   // Where we want them to go
   $login_page  = 'http://localhost.com/login/';
   if( $page_viewed == "wp-login.php" && $_SERVER['REQUEST_METHOD'] == 'GET') {
     wp_redirect($login_page);
     exit();
   }
}
add_action('init','redirect_login_page');

But the problem is, it only redirects wp-login.php page. What if we place wp-admin, (e.g. http://localhost.com/wp-admin/) it does not redirect. Instead it takes to that wp-login.php page (which I do not want)

Any suggestions?

1

I do this the same, but a little differently, and handle the case of logging in regular users or admins. I will assume you know to put these sorts of things in your "functions.php" or equivalent.

add_filter('login_url', 'your_login_url', 10, 2 );
add_action('login_init', 'your_login_page');
add_action('wp_login', 'your_login_redirect', 10, 2);



// assuming that your new front end login url is "/login", use these:
function your_login_url($login_url, $redirect) {
    return home_url('/login/');
}

function your_login_page() {
    wp_redirect(home_url('/login/'), 302);
}

// if admin send them to the dashboard, otherwise leave them on the frontend
function your_login_redirect($user_login, $user) {

    if  (current_user_can('activate_plugins')) {
            wp_redirect('/wp-admin/index.php', 302);
        }
    }
    return; 
}

Finally, if you're feeling like you want to handle password resets on the front end (the actual code for handling this is a bit more involved) here is the filter you can use:

add_filter('lostpassword_url', 'your_lost_password_page', 10, 2);

You would have to send a reset email with a one time use link after collecting the users email address via a form, and then provide for verification of that link, and another form to allow the user to reset their password. (and probably verify the text). Each of those pages will need some sort of response messaging also.

0

A good solution for this is to hook into login_init, which is fired before anything is outputted in wp-login.php.

add_action( 'login_init', 'wpse149411_login_redirect' );

function wpse149411_login_redirect() {
    wp_redirect( site_url( '/login/' ) );
    exit;
}
  • Thanks man. This really works, now the problem is, even the forgot password link does not go anywhere else. any solution to this? – kalyan Jun 12 '14 at 14:22
  • You can add a check for the action parameter to catch that: if ( ! empty( $_REQUEST['action'] && $_REQUEST['action'] == 'lostpassword' ). – engelen Jun 12 '14 at 14:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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