In my WordPress v5.8.2, I have a custom login page http://www.....com/login
.
Site allows only logged in users to post comments and below redirect URL link is published on the comments section for user to login:
http://www......com/login/?redirect_to=http://www......com/post-name
I am trying to redirect back to the post after login with below function:
function redirect_to_posts_after_login($redirect_to, $requested_redirect_to) {
$redirect_to_post = $_GET['redirect_to'];
if (!$redirect_to_post === "") {
return $redirect_to_post;
} else {
return $requested_redirect_to;
}
}
add_filter('login_redirect', 'redirect_to_posts_after_login', 10, 3);
However, it still redirects to wp-admin
after the login. How can I make the redirect back to the post?
3
Answers
With below changes, now I am able to return the user to the post after login.
Login URL at each post remained same as below with
redirect_to
:In my custom login page template
login
I am capturing theredirect_to
value as a variable:And then in the same custom login page I have this hidden input field that take the
redirect_to
as input value:In the below function I am capturing the post referrer URL from the above input and checks if there is a referrer (
redirect_to
) it will redirect to the post else towp-admin
:Since you are trying to redirect the user, to your desired custom URL after login. Then you should insert your custom URL here:-
$redirect_to variable contains the URL to the wp-admin. But as a user apart from admin. You can insert the custom URL beside the home_url() function.(Concatinate your custom url in the form hard coded string)
!$redirect_to_post === ""
will always return false !can you try with this code?