0

I am using bimber theme on latest wordpress. I wanted to force all frontpage views behind a login and added the following code in bimber's functions.php

function members_only() {
    // Check to see if user in not logged in and not on the login page
    if( !is_user_logged_in() && (is_home()||is_front_page())){
        auth_redirect();
    }
}
add_action( 'template_redirect', 'members_only' );

But this works for individual posts but not for my home page. How do i fix this?

1
  • 1
    If you want all the site behind a login, why do you check is_home() or is_front_page()? Why don't just if( !is_user_logged_in() ) auth_redirect();?
    – cybmeta
    Commented May 27, 2018 at 8:27

1 Answer 1

2

Be careful not to confuse the two query conditionals.

Whether is_home() or is_front_page() return true or false depends on the values of certain option values.

When using these query conditionals:

  • If 'posts' == get_option( 'show_on_front' ):
    • On the site front page:
      • is_front_page() will return true
      • is_home() will return true
    • If assigned, WordPress ignores the pages assigned to display the site front page or the blog posts index
  • If 'page' == get_option( 'show_on_front' ):
    • On the page assigned to display the site front page:
      • is_front_page() will return true
      • is_home() will return false
    • On the page assigned to display the blog posts index:
      • is_front_page() will return false
      • is_home() will return true

Reference:
https://developer.wordpress.org/reference/functions/is_home/#usage


To force visitors (not logged in) to log in when viewing your front page, try changing your conditional statement to this:

function members_only() {
    // Check if user in not logged in and on the front page
    if ( ! is_user_logged_in() && is_front_page() ) {
        auth_redirect();
    }
}
add_action( 'template_redirect', 'members_only' );

I wanted to force all frontpage views [...]

If by "all frontpage views" you meant the entire site, and not just your site's "front page," I recommend you install the Force Login plugin to easily and simply accomplish this.

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.