skip to Main Content

I need help with this case.

How to if a user is logged in are not allowed to access Login page and will be redirected to their Account Page?

I have a WordPress customized login page, and user is logged in still can access by typing /login slug

Here is my code below:

add_action( 'template_redirect', 'redirect_to_specific_page' );

function redirect_to_specific_page() {

 if ( is_page('login') && is_user_logged_in() ) {

wp_redirect( is_page('account'), 302 ); 
  exit;
     }
}

2

Answers


  1. Chosen as BEST ANSWER

    Idk I have wrong type or something. I have change the wp_redirect( is_page('account'), 302 ); with wp_redirect( 'account', 302 ); and its fixed. Can anyone explain this?

    My revision code below:

    add_action( 'template_redirect', 'redirect_to_specific_page' );
    
    function redirect_to_specific_page() {
    
     if ( is_page('login') && is_user_logged_in() ) {
    
    wp_redirect( 'account', 302 ); 
      exit;
         }
    }
    

  2. you can use it on wp action to redirect.

    add_action( 'template_redirect', 'redirect_to_specific_page' );
    
    function redirect_to_specific_page() {
    
     if ( is_page('login') && is_user_logged_in() ) {
    
    wp_redirect( 'account', 302 );
      exit;
         }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search