Archived topic
Redirect Logged In Users
9 replies · Started by Maria on May 23, 2020
I'm trying to redirect logged-in users to a Members Only Home page when they log in. I tried the code below per David's response on 2/7/20 #1157775 and changed '/logged-in-home' to the page slug of the page I want to redirect them to but it doesn't work. I'm currently using Peter's Login Direct plugin but it's causing me other issues and I'd like to get rid of that plugin. Am I missing anything from the code below?
add_action( 'wp', function() {
if ( is_front_page() && is_user_logged_in() ) {
wp_redirect( home_url( '/logged-in-home' ) );
die();
}
} );
Doesn't help me. I have no idea what the $page = 2516 or update_option means. David's code makes sense to me but seems like something is missing. Any other options?
Hi there,
where and how are you adding the code?
For reference this is the working code i just tested:
add_action( 'wp', function() {
if ( is_front_page() && is_user_logged_in() ) {
wp_redirect( home_url( '/logged-in-home' ) );
die();
}
} );
Where and how should I be adding the code?
It's PHP - this document explains:
I realize it's PHP. I was hoping for a bit more explanation. I put the following snippet in the functions.php of my child theme.
add_action( 'wp', function() {
if ( is_front_page() && is_user_logged_in() ) {
wp_redirect( home_url( '/members-home-page' ) );
die();
}
} );
After logging in, I was directed to the Dashboard, not the Members Home Page I intended. I don't know what I'm doing wrong.
Hi there,
How are you logging in?
That code will redirect a logged-in user if they go to the front page. It won't necessarily redirect them as soon as they log in.
In order to redirect someone from the login form, you would need them to go to the login form using a specific link.
If you search for "WordPress redirect after login", you should find a lot of answers. This one looks promising: https://stackoverflow.com/a/8127629/2391422
Thanks, Tom for the explanation. I totally understand now. I wanted to redirect them as soon as they log in.
No problem! :)