1

I have just installed WordPress locally in my computer and run the website successfully. I want to achieve following: when logged-in user tries to access wp-login.php or register page, it should be automatically redirected to home page.

I have spent several hours on web, but could no come up with solution neither was I able to find appropriate WordPress plugin.

My question is: why does WordPress function this way? how can we change this behavior as natively as simply as possible?

Thank you

1

You can check the current page being loaded through the $pagenow global variable. Both login and registration is handled through wp-login.php. Adding this to the functions.php of your theme should work:

<?php

add_action('init', function () {

    if ($GLOBALS['pagenow'] === 'wp-login.php' && is_user_logged_in() && (!isset($_GET['action']) || $_GET['action'] !== 'logout')) {

        wp_redirect(home_url());
        exit;

    }

});

EDIT: Added a check to allow logging out and clarified where to put this code.

7
  • Thanks. Also, could you please, tell in which file should i place above script? Also, i have previously tried this, but when i use the above code and press logout button, it does not logout anymore, instead it redirects to home page. is it possible to correct this? Feb 27 at 20:25
  • @TornikeShavishvili You're right, this would make logging out impossible so I've added a check to see if the user is trying to log out. I also should've mentioned this should be placed in the functions.php of the (child-)theme you are using. I've edited the answer.
    – Patriot
    Feb 27 at 21:10
  • I also did that myself. we both came up with same sollution i guess. I am going to accept your answer. Feb 27 at 21:12
  • I am hoping this approach doesnot ahve any downsides Feb 27 at 21:16
  • 1
    Thanks, be mindful of the place you've put it in your answer: That file can be overridden by WordPress updates (best to assume it will every time you update), so putting it in the theme or a child theme is less hassle.
    – Patriot
    Feb 27 at 21:18
1

i modified @Patriot code.

I was able to achieve desired functionality with following code: I added this code to \wp-includes\functions.php at the and of file.

It not as clean is i wished, but it works.

If someone sees any possible issues with this approach, please inform us

add_action('init', function() {

    if ($GLOBALS['pagenow'] === 'wp-login.php' && is_user_logged_in()) {
        
        $defLoginPageActions = array(
            //'confirm_admin_email',
            //'postpass',
            'logout',
            //'lostpassword',
            //'retrievepassword',
            //'resetpass',
            //'rp',
            //'register',
            //'checkemail',
            //'confirmaction',
            //'login',
            WP_Recovery_Mode_Link_Service::LOGIN_ACTION_ENTERED,
        );
        $loginPageAction = isset( $_REQUEST['action'] ) ? $_REQUEST['action'] : '';

         
        if(!in_array( $loginPageAction, $defLoginPageActions)){  
            wp_redirect(home_url());
            exit;
        } 

    }
 
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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