1

I am using Theme My Login plugin to change the login page from "/wp-login.php" to "/login".

My question is how do I redirect a user that has already logged in so that visiting "/login" instead takes them to my homepage?

Note: I am not talking about the initial redirect after logging in, but rather if someone was to visit the login page again afterwards.

I saw a similar question was asked here: wp-login.php — redirect logged in users to custom URL

I tried using the code posted there, and changed it to:

function redirect_logged_in_user() 
{
    if( is_user_logged_in ) {
      wp_redirect('http://my_homepage_url');
    }
}

global $pagenow;
if( $pagenow == '/login')     
redirect_logged_in_user();

but it still does not work.

any help would be much appreciated.

cheers.

0

2 Answers 2

1

I advice you to try this, better to the action login_init:

add_action('login_init', 'redirect_logged_in_user');
function redirect_logged_in_user()
{
    global $action;

    /* if the user call logout and is not logged in we do nothing*/
    if ('logout' === $action || !is_user_logged_in()) {
        return;
    }

    /* we redirect logged in people*/
    wp_redirect('http://my_homepage_url');
    exit;
}
10
  • thanks for the reply. this seems to work with the "my_homepage_url/login" and "my_homepage_url/register" pages but it's also preventing me from logging out with "my_homepage_url/logout". how can it be changed to allow for logout?
    – Tom
    Commented Oct 3, 2017 at 17:59
  • you mean you cannot logout now ? Commented Oct 3, 2017 at 18:02
  • yes. after adding the code you posted above it is making "my_homepage_url/logout" re-direct to homepage also, and keeps me logged in
    – Tom
    Commented Oct 3, 2017 at 18:11
  • updated my anwser, have a try please Commented Oct 3, 2017 at 18:16
  • i have just tried the new code but it's still doing the same thing
    – Tom
    Commented Oct 3, 2017 at 18:22
0
    add_action('wp', 'add_login_check');
function add_login_check()
{
    if ( is_user_logged_in() && is_page( [6070, 6072] ) ) {
        wp_redirect('http://my_homepage_url');
        exit;
    }
}

i took the code from here: https://stackoverflow.com/a/25992092

i then moved the "add_action('wp', 'add_login_check');" to the top and added another "is_page" (6070=login page ID, 6072=register page ID).

it seems to be holding up so far and allows me to log out.

is this code written well? would it be necessary to change anything?

4
  • the only issue with this code is that it's not generic. as you need to specify the id of the page. You can also use is_page with my solution in order to check if we are in the logout page or not Commented Oct 3, 2017 at 20:26
  • yeh i noticed your code is not reliant on manually inputting the page IDs. i have carried out some further testing & it seems the above code is holding out, and since i don't need to add many page IDs (and don't want to take up any more of your time) i'll keep it the way it is. many, many thanks for your efforts mate, they are much appreciated. i voted your comment up and it said "Thanks for the feedback! Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score." so i will revisit this page when i have 15 rep and make sure it worked :)
    – Tom
    Commented Oct 3, 2017 at 20:42
  • yes no problem ;) and you may consider manually input, it's not bad for a specfic solution, but since we are in site like this, it's better to highlight generic solution that may work for everyone, and maybe in one year someone will face the same issue and will find this page ;) Commented Oct 3, 2017 at 20:48
  • well i'm happy to try more attempts if you are ;)
    – Tom
    Commented Oct 4, 2017 at 7:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.