How to Redirect Users after login with Ultimate Member Plugin

Get access to all plugins for $120 $60 with our All Access Lifetime Suite!

The Ultimate Member plugin for WordPress provides various options for redirecting users after login, including redirecting to their profile, a specific URL, refreshing the active page, or redirecting to the WordPress Admin area. These actions can be customized in the “Redirection after Login” option for each login form. Additionally, each user role can have a specific “Action to be taken after login” setting​​.

However, the plugin does not include a default action to redirect users to the previous page they were viewing before login. To achieve this, customization is needed. One way to customize the redirect after login is to add the redirect_to parameter with the desired URL (the previous page URL). There are two methods for adding this parameter:

  1. Use the um_browser_url_redirect_to__filter hook to change the hidden “redirect_to” input in the login form.
  2. Add the “redirect_to” parameter to the login page URL, which the plugin then copies to the hidden “redirect_to” input in the login form.

It’s also recommended to use the Referer header to obtain the previous page URL, though this header might sometimes be empty or incorrect. In such cases, a custom solution is required to save and retrieve the URL of the previous page. If the page contains restricted or role-specific content that is cached, adding a random parameter to the redirect URL can help bypass caching​​.

A specific code example demonstrates how to implement this feature. The code involves adding a filter to um_browser_url_redirect_to__filter in the functions.php file of the active theme. This filter checks for the HTTP_REFERER and sets the redirect URL accordingly, adding a unique identifier to bypass caching if needed​​.

Here are some code examples using the filter

/**
 * Redirect users to different pages after login based on their role.
 */
add_filter( 'um_browser_url_redirect_to__filter', function( $url ) {
    $user = wp_get_current_user();
    if ( in_array( 'subscriber', (array) $user->roles ) ) {
        return home_url( '/subscriber-page/' ); // Redirect subscribers to a specific page
    } elseif ( in_array( 'editor', (array) $user->roles ) ) {
        return home_url( '/editor-page/' ); // Redirect editors to a different page
    }
    // Default redirect for other roles
    return $url;
});

This code checks the role of the currently logged-in user and redirects them to a specific page based on their role. For example, subscribers are redirected to /subscriber-page/ and editors to /editor-page/. You can modify this code to suit your needs by changing the role checks and corresponding URLs.

/**
 * Redirect users to a welcome page on their first login.
 */
add_filter( 'um_browser_url_redirect_to__filter', function( $url ) {
    $user_id = get_current_user_id();
    $first_login = get_user_meta( $user_id, 'first_login', true );

    if ( empty( $first_login ) ) {
        update_user_meta( $user_id, 'first_login', 'done' );
        return home_url( '/welcome/' ); // Redirect to a welcome page on first login
    }

    // Default redirect for subsequent logins
    return $url;
});

This code checks if it’s the user’s first login by looking for a custom meta field first_login. If the field is empty, it means it’s their first login, and they are redirected to a custom welcome page. After the first login, this meta field is updated, so subsequent logins will follow the default redirect behavior.

This customization enhances the user experience by ensuring that users return to the content they were engaged with prior to logging in, providing a seamless browsing experience.

Do you need help with your WordPress site?

Check out our WordPress Services and get the help fast.