Hi @pedrorivera,
Please try the following code snippet to redirect to the previous page after login.
add_action( 'user_registration_before_customer_login_form', 'ur_set_redirect_url' );
function ur_set_redirect_url() {
if ( isset( $_SERVER['HTTP_REFERER'] ) ) {
set_transient( 'originalLoginRefererURL', $_SERVER['HTTP_REFERER'], 60 * 60 * 24 );
}
}
add_filter( 'user_registration_login_redirect', 'ur_redirect_back', 10, 2 );
function ur_redirect_back( $redirect, $user ) {
if ( true === ( $redirect_url = get_transient( 'originalLoginRefererURL' ) ) ) {
delete_transient( 'originalLoginRefererURL' );
return $redirect_url;
}
return $redirect_url;
}
If you do not know how to add code snippet on your site, please check this documentation https://docs.wpeverest.com/user-registration/docs/how-to-add-the-custom-code-snippet-on-your-site.
Regards!
Hello Amrit, thanks for the reply.
The function works pretty well, but I think I wasn’t very clear. Currently the code redirects to the page before login.
Is it possible to make the user redirect to the page where the login form is? The login form is placed on the article page, so user should be redirected back to this page.
Making it clear:
Asume the login form is placed on page example.com/page/sub-page.
When user get to /sub-page, they should login. After login, they should be back to /sub-page. With the current code, user goes to /page.
Thank you in advance!
Hi @pedrorivera ,
It seems you are using my account shortcode, so your users are redirecting to the account page. Could you please try this [user_registration_login] login shortcode on pages?
Regards!
@shresthauzwal thanks, that’s exactly what I need.
Hi @pedrorivera,
Glad to hear that your issue has been solved. If you have a moment to spare, then we would really appreciate your review. Please click on this link https://wordpress.org/support/plugin/user-registration/reviews/#add-new and share your thoughts about our support and the theme. We would love to hear from you.
It will motivate us to provide free support and release a regular plugin update.
Regards!