Archived topic
Redirect Logged In Users Away from Home Page (login page)
9 replies · Started by Gerson on February 9, 2021
I've created a login form with Elementor on my homepage and I want to redirect all logged in users away from the homepage to a specific page if they attempt to visit the homepage again. I'm using this code and adding it to the hook element in wp_head but I'm having no luck..is this possible?
<?php
add_action( 'template_redirect', function() {
if ( is_page( 'home' ) && is_user_logged_in() ) {
wp_redirect( home_url( '/presentation' ) );
die();
}
} );
?>
Hi there,
Do you need it on home/front page only? If so, try this:
add_action( 'template_redirect', function() {
if ( is_front_page() && !is_user_logged_in() ) {
wp_redirect( home_url( '/presentation' ) );
die();
}
} );
I’m using this code and adding it to the hook element in wp_head but I’m having no luck..is this possible?
Hook element module is for presentation purposes only. For this kind of function you should use a child theme's functions.php or Code snippets plugin. https://docs.generatepress.com/article/adding-php/
But if you want it to apply to ALL pages, I think this is the one you're looking for.
https://developer.wordpress.org/reference/functions/auth_redirect/
Example: If you want ALL pages to WordPress's default login page
add_action( 'template_redirect', function() {
if ( !is_user_logged_in() ) {
auth_redirect();
}
} );
Thanks for that. I'm using the following code in functions.php file but its not working:
//Redirect logged users to presenation page//
add_action( 'template_redirect', function() {
if ( is_front_page() && !is_user_logged_in() ) {
wp_redirect( home_url( '/presentation/' ) );
die();
}
} );
My use case is...when a user logs in and accidentally closes their browser, or navigates away to another website, when they revisit the homepage URL (since they are already logged in) I want it to redirect them to a specific page.
That code will redirect you to /presentation/ if you're on the front page and not logged in.
If you want it to happen when the user is logged in, you need to remove the ! before is_user_logged_in().
Thanks Tom but every time I do that, I get this error message: "Unable to communicate back with site to check for fatal errors, so the PHP change was reverted. You will need to upload your PHP file change by some other means, such as by using SFTP." Is it just a matter of removing the '!'?
Removing the ! shouldn't cause that. However, if you check your error_log file, you should find a specific error that should help us know the actual cause.
Hi Tom, I'm not sure which error log it was...but it could be this one:
generatepress_child/functions.php on line 87" while reading response header from upstream
My use case is…when a user logs in and accidentally closes their browser, or navigates away to another website, when they revisit the homepage URL (since they are already logged in) I want it to redirect them to a specific page.
If this is the use case then I'm not sure its a good idea to use this code at all.
Using this code means a logged in user will be redirected to /presentation page every time it tries to visit the frontpage which will literally mean logged in users can't visit the homepage anymore regardless if it's a revisit or a first time visitor. I'm not sure if this is the one you should use with your use case, unless, that's what you actually want.
In this case, I believe what you can do is implement cookies to your site and use that as basis for redirection.
But this will most likely require scripting or a third-party plugin which is, unfortunately, out of our scope of support.
Using this code means a logged in user will be redirected to /presentation page every time it tries to visit the frontpage which will literally mean logged in users can’t visit the homepage anymore regardless if it’s a revisit or a first time visitor. I’m not sure if this is the one you should use with your use case, unless, that’s what you actually want.
Actually Elvin, that is exactly what I want to achieve :D - is it possible?
Then this should work:
add_action( 'template_redirect', function() {
if ( is_front_page() && is_user_logged_in() ) {
wp_redirect( home_url( '/presentation/' ) );
die();
}
} );
I basically just removed the !. It's strange that it caused error on yours.
I've tested the same code on my sandbox site to make sure it doesn't cause any errors and it actually works as I've mentioned.
I've read the error on your site and its to be because the site already loads the header. Ideally we do the redirect before get_header() to avoid this issue.
Perhaps these articles could help you out.
https://tommcfarlin.com/wp_redirect-headers-already-sent/
https://stackoverflow.com/a/8028987