• Skip to primary navigation
  • Skip to main content
Matthew Dunbar

Matthew Dunbar

Hire Me
  • About
  • Portfolio
  • Blog
    • General
    • HTML
    • jQuery
    • PHP
    • Optimization
  • Hire Me
You are here: Home / PHP / Redirect to WP Login on Every Page If User Is Not Logged In

Redirect to WP Login on Every Page If User Is Not Logged In

March 2, 2017

Copy and paste this code into your functions.php file to dynamically redirect users who are not logged in to the default wp-login.php page when they try to visit any page on your site. Once they log it, they will be redirected back to the page they tried to access.

// function that will redirect users to the wp-login.php page if they are not logged in
function md_redirect() {

    // Current Page
    global $pagenow;

    // Check to see if user in not logged in and not on the login page
    if(!is_user_logged_in() && $pagenow != 'wp-login.php')
          // If user is, Redirect to Login form.
          auth_redirect();
}
// adds your function to the WordPress build
add_action( 'wp', 'md_redirect' );

Filed Under: PHP

Go Back: How to Add a Preloader to Your Genesis Website
Next Up: How to Replace $0.00 to Free in Woocommerce 3.0 Update – 2017
© 2025 Matthew Dunbar - All Rights Reserved Back to Top