Redirect to the previous page after login

How to select a predefined action after login

The plugin may do one of these actions after login via the Ultimate Member login form:

  • Redirect to profile
  • Redirect to URL
  • Refresh active page
  • Redirect to WordPress Admin

You can select needed action in the "Redirection after Login" option for the desired login form to use this action for all members or select the "Default" to use the role-based action. Each user role has the "Action to be taken after login" setting in the Login Options.

Screen wp-admin > Ultimate Member > Forms > Edit Form (login).

Screen wp-admin > Ultimate Member > User Roles > Edit Role.

How to customize redirect after login

There is no action to redirect to the previous page after login. You have to customize the redirect after login if you need this feature. Add the redirect_to parameter with the URL you need (the previous page URL). There are two ways to add 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. The plugin copies this parameter to the hidden "redirect_to" input in the login form. An URL example: http://site.domain/login/?redirect_to=%2Fmembers%2F

Try to use the Referer header to get the previous page URL. This header may be empty or incorrect. In this case you have to use your own solution to save and get the URL of the previous page. 

Add a random parameter to the redirect URL if you want to bypass caching. This may be needed if the page that contains restricted or role-specific content is cached.

Code example

Try a solution with the Referer header shown below. Just add this code to the functions.php file in the active theme directory.

/**
 * Redirect to the previous page after login via the Ultimate Member login form.
 */
add_filter( 'um_browser_url_redirect_to__filter', function( $url ) {
	if ( empty( $url ) && isset( $_SERVER['HTTP_REFERER'] ) ) {
		$url = esc_url( wp_unslash( $_SERVER['HTTP_REFERER'] ) );
	}
	return add_query_arg( 'umuid', uniqid(), $url );
} );

NOTE: We have created this code example to provide guidance and to make it easier for you to implement this code into your website. However, we are not able to provide any support when it comes to customizing the plugin. If you need help implementing this code, please hire a developer.