Last updated on September 24, 2023

Add a Custom Login/Logout Redirect

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Customize login/logout redirects.

A custom login/logout redirect can be useful when you want to redirect users to a specific page after they log in or log out of your WordPress website. This can be handy for creating a personalized user experience or directing users to a specific landing page.

To add a custom login/logout redirect, you can use the login_redirect and logout_redirect filters provided by WordPress. These filters allow you to modify the default redirect behavior and specify your own custom URLs.

Here’s an example code snippet that demonstrates how to add a custom login/logout redirect:

// Custom login redirect
function wpsnippets_custom_login_redirect($redirect, $request, $user) {
    // Change the URL to your desired login redirect page
    return home_url('/my-custom-login-redirect');
}
add_filter('login_redirect', 'wpsnippets_custom_login_redirect', 10, 3);

// Custom logout redirect
function wpsnippets_custom_logout_redirect($redirect) {
    // Change the URL to your desired logout redirect page
    return home_url('/my-custom-logout-redirect');
}
add_filter('logout_redirect', 'wpsnippets_custom_logout_redirect');

In the above code snippet, we define two functions: wpsnippets_custom_login_redirect and wpsnippets_custom_logout_redirect. These functions handle the custom login and logout redirects, respectively.

Inside the wpsnippets_custom_login_redirect function, we modify the $redirect parameter to specify the desired login redirect URL. You can change the URL to your own custom login redirect page.

Similarly, in the wpsnippets_custom_logout_redirect function, we modify the return value to specify the desired logout redirect URL. Again, you can change the URL to your own custom logout redirect page.

By adding these filters to your theme’s functions.php file or a custom plugin, you can easily redirect users to your desired pages after they log in or log out.

Examples

Example 1: Redirect users to a custom page after login

This use case demonstrates how to redirect users to a custom page after they successfully log in to your WordPress site.

function wpsnippets_custom_login_redirect( $redirect, $user ) {
    // Change the URL to your custom page
    $redirect = home_url( '/custom-page' );
    return $redirect;
}
add_filter( 'login_redirect', 'wpsnippets_custom_login_redirect', 10, 2 );

The code example above uses the login_redirect filter to modify the default redirect URL after login. By defining a custom function wpsnippets_custom_login_redirect, we can change the redirect URL to any page on our site. In this example, we redirect users to a custom page with the slug /custom-page.

Example 2: Redirect users to a specific URL based on user role after login

This use case demonstrates how to redirect users to different URLs based on their user roles after they log in to your WordPress site.

function wpsnippets_custom_login_redirect( $redirect, $user ) {
    if ( in_array( 'administrator', $user->roles ) ) {
        // Redirect administrators to the dashboard
        $redirect = admin_url();
    } elseif ( in_array( 'subscriber', $user->roles ) ) {
        // Redirect subscribers to a specific page
        $redirect = home_url( '/subscriber-page' );
    } else {
        // Redirect other users to the homepage
        $redirect = home_url();
    }
    return $redirect;
}
add_filter( 'login_redirect', 'wpsnippets_custom_login_redirect', 10, 2 );

In this code example, we use the login_redirect filter to redirect users to different URLs based on their user roles. By checking the user’s role using the in_array() function, we can conditionally set the redirect URL. Administrators are redirected to the dashboard, subscribers are redirected to a specific page, and all other users are redirected to the homepage.

Example 3: Redirect users to a custom page after logout

This use case demonstrates how to redirect users to a custom page after they log out of your WordPress site.

function wpsnippets_custom_logout_redirect() {
    // Change the URL to your custom page
    $redirect = home_url( '/custom-logout-page' );
    wp_redirect( $redirect );
    exit;
}
add_action( 'wp_logout', 'wpsnippets_custom_logout_redirect' );

In this code example, we use the wp_logout action hook to trigger a custom function wpsnippets_custom_logout_redirect when a user logs out. Inside the function, we set the redirect URL to a custom page and use wp_redirect() to perform the redirection. Finally, we exit the script to prevent any further execution.

Last updated on September 24, 2023. Originally posted on September 22, 2023.

Leave a Reply

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