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

2 Answers 2


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:


add_action('init', function () {

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




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

  • 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? Commented Feb 27, 2021 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
    Commented Feb 27, 2021 at 21:10
  • I also did that myself. we both came up with same sollution i guess. I am going to accept your answer. Commented Feb 27, 2021 at 21:12
  • I am hoping this approach doesnot ahve any downsides Commented Feb 27, 2021 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
    Commented Feb 27, 2021 at 21:18

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(
        $loginPageAction = isset( $_REQUEST['action'] ) ? $_REQUEST['action'] : '';

        if(!in_array( $loginPageAction, $defLoginPageActions)){  


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.