Hi @ytfeldrawkcab,
Thank you for contacting us.
You are correct, a redirect_to should be able to handle that kind of redirection after the user logs in when doing custom coding.
Nevertheless, you don’t need custom coding for this case since what you are looking for is already implemented in the base plugin under Users > Settings > Redirects > After login. Image for reference: https://share.zight.com/v1uGOXn2
Configuring this setting will redirect the user to your desire page after they logs in, without diving to custom codes.
I hope this helps, and please let me know if you have additional questions.
Hi @robfrtlz, thanks for your reply.
Unfortunately, your suggestion does not solve our problem. I am aware of the built-in redirect-after-login feature (I mentioned it in my 2nd paragraph), but it only allows you to pick a single page for users to be redirected to after they log in.
What I’m looking for is the user to be redirected to whatever page they attempted to access prior to being asked to log in.
This very support form we’re writing on does what I’m looking for:
For anyone who finds this thread, I found my own solution, hacky though it is. The following two functions added to your theme / child theme functions.php should do the trick:
// Set a 10 minute redirect cookie if WP User Manager is preventing access and sending the user to the login screen
function set_redirect_cookie($prevent_access) {
if ($prevent_access) {
setcookie('redirect_after_login',
esc_url_raw($_SERVER['REQUEST_URI']),
time() + 600,
COOKIEPATH,
COOKIE_DOMAIN
);
}
return $prevent_access;
}
add_filter('wpum_prevent_entire_site_access', 'set_redirect_cookie');
// If a redirect cookie exists, override the default login redirection with the cookie value and delete the cookie
function custom_login_redirect($redirect_url) {
if (isset($_COOKIE['redirect_after_login'])) {
$redirect_url = $_COOKIE['redirect_after_login'];
setcookie('redirect_after_login', '', time() - 3600, COOKIEPATH, COOKIE_DOMAIN);
}
return $redirect_url;
}
add_filter('wpum_redirect_after_login', 'custom_login_redirect', 10, 1);
This method uses a browser cookie to save the original pre-login URL, which is then used to redirect the user back to that URL after login. This is not an ideal solution – a URL parameter is more reliable and fits better with the way WordPress typically handles login redirects. However, line 394 in the plugin’s actions.php prevents URL parameters from working at all for this purpose.
I strongly recommend that the plugin developers add a native redirect solution to send the user back to the page they originally tried to access prior to be redirected to the login page using a URL parameter as I described in my previous comment on this thread. This is a base level functionality for this type of application.