How to Redirect to the Requested URL After a Successful Login in WordPress

WPTurbo » Snippets » How to Redirect to the Requested URL After a Successful Login in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Navigating the nuances of user login can be a tricky business. In particular, have you ever struggled with redirecting users back to the page they were originally trying to access after they’ve logged in? Fret no more. This article is a comprehensive guide to help you successfully implement a redirect to the requested URL post-login on your WordPress site. So read on to learn how to enhance your user experience by eliminating unnecessary navigation challenges, and keep your visitors engaged and happy.

					function wpturbo_redirect_after_login( $redirect_to, $request, $user ) {
    if (isset($request) && $request != '') {
        return $request;
    } else {
        return home_url();
    }
}
add_filter('login_redirect', 'wpturbo_redirect_after_login', 10, 3);
				

The above code helps to redirect the users to the previously requested URL after successful login with WordPress.

The body of the PHP script starts with defining a new function named wpturbo_redirect_after_login(). This function takes three parameters: $redirect_to, $request, and $user. These parameters represent the URL to redirect to, the requested redirect URL, and the WP_User object, respectively.

Inside the function, we write an if statement:

if (isset($request) && $request != '') {
    return $request;
} else {
    return home_url();
}

The isset($request) checks if the $request variable is set and is not null, and $request != '' checks if the $request is not an empty string. If these two conditions are both true, meaning there is a requested URL, the function will return $request, which effectively redirects the user to their requested URL after successfully logging in.

However, if the conditions are not met, meaning there isn’t a requested URL, the script will execute the else clause where home_url() function is returned. The home_url() function is a built-in WordPress function that retrieves the homepage URL of the current site, meaning the user would be redirected to the homepage after login.

Finally, we are adding our function to the login_redirect filter hook:

add_filter('login_redirect', 'wpturbo_redirect_after_login', 10, 3);

The login_redirect filter is used to modify the location redirected to after a user logs in. The add_filter() function is used to hook a function wpturbo_redirect_after_login to a specified filter action, login_redirect.

The parameters 10 and 3 in the add_filter() function specifies the priority and the number of arguments the function accepts, respectively. In this case, the priority is set to 10, and the function accepts three arguments, which matches with $redirect_to, $request, and $user in the wpturbo_redirect_after_login function.

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