WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Surprised I haven't found this Q.

I want to force users to login to the site before accessing any of the content.

I have this function:

function av_force_user_login() {
    global $post;
    global $pagenow;
    if ( in_array( $GLOBALS['pagenow'], array( 'wp-login.php', 'wp-register.php' ) ) ) {
add_action( 'get_header', 'av_force_user_login' );

This doesn't seem to work though and after I try and login I get redirected back to the login page.

One caveats to watch out for (in my case): I'm using BuddyPress' registration system which doesn't apply to the login page but it would apply to the wp-login.php?action=register page which redirects to Buddypress' template for registration.

Nevertheless, is there anything wrong with the above code that would cause a redirect back to the login page?

share|improve this question
up vote 1 down vote accepted

Very Basic Script. This should work.

add_action('template_redirect', 'redirect_to_login');

function redirect_to_login(){
   // user not logged in so redirect them to login page
   $redirect_url = '' // you can determine where to redirect user after they login
   wp_redirect( wp_login_url($redirect_url), 301 ); exit;
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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