Reputation:
I have a WordPress site where users go to the WooCommerce login/signup page before using the site. I want to add something to my functions.php that checks if the user is signed in and if they are on the login page, and if they are not it redirects them to the login page. The only code I can find redirects logged out users, so I was hoping someone could help.
Upvotes: 1
Views: 118
Reputation: 1240
Please add following code in your functions.php
add_action( 'template_redirect', 'redirect_to_login' );
function redirect_to_login() {
if ( is_page('my-account') && ! is_user_logged_in() ) {
wp_redirect( '/login/', 301 );
exit;
}
elseif ( is_page('login') && is_user_logged_in() ) {
wp_redirect( '/my-account/', 301 );
exit;
}
}
Upvotes: 1