简体   繁体   中英

Redirect Logged Out Users Wordpress

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.

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;
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM