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.
is_home()
oris_front_page()
? Why don't justif( !is_user_logged_in() ) auth_redirect();
?