login_redirect

Home » Hooks » login_redirect

The login_redirect hook is a powerful hook in WordPress that allows developers to customize the redirection behavior after a user logs in to their WordPress website. By default, after logging in, WordPress redirects the user to the WordPress admin dashboard. However, with the login_redirect hook, you can easily modify this behavior and redirect the user to a different page or URL of your choosing.

The login_redirect hook is particularly useful in scenarios where you want to enhance the user experience by redirecting users to a specific page after they log in. For example, you might want to redirect users to a custom dashboard, a personalized welcome page, or even a specific landing page tailored to their user role or membership level.

To leverage the power of the login_redirect hook, you need to add a custom function to your theme’s or plugin’s functions.php file or create a custom plugin. This function should define the desired redirection logic and return the URL or path where you want to redirect the user.

Here’s an example usage code that redirects the user to a custom dashboard page after they log in:

function custom_login_redirect($redirect, $request, $user) {
    // Check if the user has a specific role
    if (isset($user->roles) && is_array($user->roles) && in_array('subscriber', $user->roles)) {
        // Redirect subscribers to the custom dashboard page
        $redirect = home_url('/dashboard');
    }
    return $redirect;
}
add_filter('login_redirect', 'custom_login_redirect', 10, 3);

In this example, the custom_login_redirect function checks if the user has the "subscriber" role. If they do, it updates the redirect URL to the custom dashboard page using the home_url() function. Finally, the function returns the updated redirect URL.

By adding this code to your WordPress theme or plugin, users with the subscriber role will be redirected to the "/dashboard" page after they log in, instead of the default WordPress admin dashboard.

With the login_redirect hook, you have the flexibility to tailor the login experience to your specific needs and provide a more personalized user journey on your WordPress website.

Learn More on WordPress.org

Register an account to save your snippets or go Pro to get more features.