How to redirect to login page in WordPress

This post may contain affiliate links for products discussed. As an Amazon Associate I earn from qualifying purchases. What this means is that by clicking one of these links, we may sometimes receive an affiliate fee.

WordPress’s default login redirect behavior is to pass a user into the WordPress dashboard once logged in. However, you may want to change this behavior if you’re running a multi-user WordPress site and want users to log in to see certain content.

In these instances, you’ll want to either redirect users to the WordPress login page or redirect users from the login page to some other content (the home page, the page they were just on, etc). Depending on what you’re looking to accomplish, there are different processes involved, and perhaps even multiple ways of performing that redirect to the login page.

Users sent to the WP Admin dashboard after login

This is the default WordPress login behavior. When a user logs in with the WordPress login page, they’ll automatically be sent to the WP admin dashboard. If you didn’t want this behavior for your users, you can do a variety of things to alter the behavior.

Redirect from the home page to the login page

If someone accesses your website without being logged in and you want them to immediately log in, you can redirect that user directly to the login page with the following added to your child theme’s WordPress functions.php file:

if(!is_user_logged_in()) {
    wp_redirect( wp_login_url() );
}

This will detect the user’s login state and send them to the login page if not logged in. Once they log in they will be sent to the WordPress dashboard. If you would like them to be sent to the home page, add this code to your child theme’s functions.php file:

/* redirect users to front page after login */
function redirect_to_front_page() {
    global $redirect_to;
    if (!isset($_GET['redirect_to'])) {
        $redirect_to = get_option('siteurl');
    }
}
add_action('login_form', 'redirect_to_front_page');

Redirect to the previous page after login

If you would rather the user be brought back to the previous page after logging in instead of the WordPress admin page, that code is a bit more complex but not terrible. There are two tasks that need to be performed to accomplish this:

  1. Get the page the user was viewing before clicking the Login link
  2. Redirect the user to that same page after a successful login submission

1 – Get last page URL visited in WordPress

First, let’s get the URL of the page the user is on before clicking the login link. Adding this code to functions.php will set that in a session variable on each page load:

add_action( 'wp', 'get_before_login_url' );
function get_before_login_url(){
    if( !is_user_logged_in() ):
    $_SESSION['referer_url'] = get_the_permalink();
    endif;
}

2 – Redirect user to last URL in variable after login

After a successful login submission, we can then redirect the user to the previously captured session variable URL with this code added to the fuctions.php:

/*@ After successful login redirect */
if( !function_exists('after_login_redirect') ):
    function after_login_redirect() {
 
    $redirect_url = home_url('/');
    if ( isset($_SESSION['referer_url']) ):
        $redirect_url = $_SESSION['referer_url'];
        unset( $_SESSION['referer_url'] );
    endif;
 
    return $redirect_url;
    exit;
   }
   add_filter('login_redirect', 'after_login_redirect');
endif;

With these added to your functions.php file a user will now be brought to the last page they were on when they clicked the login link. It should be noted, however, that this should not be used with another redirect method.

Redirect to WordPress login page with a plugin

If you don’t feel like dealing with custom code and editing your child theme’s functions.php file, then there’s another option with a plugin called Peter’s Login Redirect. This plugin will allow you to create various redirects either by specific username, role or even by profile capability. You can also create a redirect rule for all other users not specified in custom rules.

It doesn’t seem to work well with membership plugins, but for general WordPress login functionality, it gets the job done.

Creating login redirect by user role in WordPress

Perhaps the most applicable option with the plugin is creating login redirects for specific roles. This would allow you to create a redirect for general Subscribers to take them to the home page (or any other page).

Create login redirect by capability in WordPress

This will allow you to redirect users based on their role’s capabilities. Is the user allowed to edit published posts? Send them to the Posts dashboard page. Is the user a forum mod? Send them to their forum dashboard.

The use cases are somewhat specific, but they’re almost infinitely flexible and easy enough to set up.

Create login redirect for specific users in WordPress

If you have very specific redirection requirements for certain users you can define those here. While it’s not the most useful or common redirection requirement we’d imagine, it’s definitely possible if you need that type of control.

Create login redirect for all users in WordPress

If you just want all of your users to get redirected to the same place after logging in, you can specify that here. This will also serve as a catch-all for any users that don’t match any other redirect rules.

Create login redirect after successful registration in WordPress

You can also specify a custom welcome page for users to be redirected to after a successful site registration. This means you can build a landing page for new registrants to point them to exactly what you want them to see first.

Using a custom login form to redirect users after login

Another option is to use a custom login form to override the default WordPress login page behavior. Different form plugins offer different ways of doing this, but most form builders that also allow for custom login forms will let you set a custom login redirection.

Final thoughts

Redirecting WordPress login functions may differ based on your need, but it usually is pretty easy–whether you accomplish this with custom code or with a WordPress plugin. We hope that this has helped in your WordPress administration.

Newsletter Updates

Enter your email address below and subscribe to our newsletter

2 Comments

  1. Hello
    I tried the code to Redirect user to last url in variable after login
    but the latter does not seem to work well with me
    in my case on an example page the user not connected can only see part of the page if he is not connected
    there is therefore a button on this page which allows him to connect and after connection he can see the entire document
    can you help me thank you

    • Hey there,
      Just to make sure, did you use both part 1 and part 2 of that code? You’ll need both the variable set code and the redirect code to work together.

      Otherwise, it sounds like you’re looking for a content locker, correct? Something that will hide the contents of a page unless you’re logged in?

      There are a bunch of these floating around out there, such as MyThemeShop’s Content Locker Pro. Most of the full-featured content lockers are paid plugins, but they do offer a free version.

Leave a Reply to flexi2202Cancel Reply

Your email address will not be published. Required fields are marked *