admin管理员组文章数量:1335871
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?
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?
Share Improve this question asked May 27, 2018 at 7:09 Jagan VeeraraghavanJagan Veeraraghavan 1011 silver badge1 bronze badge 1 |1 Answer
Reset to default 2Be 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
- On the site front page:
- 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
- On the page assigned to display the site front page:
Reference:
https://developer.wordpress/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.
本文标签: functionsaddaction templateredirect not working for home page
版权声明:本文标题:functions - add_action template_redirect not working for home page 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742394300a2466610.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
is_home()
oris_front_page()
? Why don't justif( !is_user_logged_in() ) auth_redirect();
? – cybmeta Commented May 27, 2018 at 8:27