admin管理员组文章数量:1426901
Looking for some advice on the below function. I have a web app type site built in WP. There is nothing on the site relevant to non-users, apart from the login form on the homepage. I've got some membership plugins that lock content, but as an added layer of security I was thinking of adding the following:
function lock_non_users_to_front() {
if( !is_user_logged_in() && !is_front_page() ) {
wp_redirect( home_url() );
}
}
add_action( 'template_redirect', 'lock_non_users_to_front' );
Essentially, if anyone tries to access a url that isn't the homepage, and they aren't logged in, it'll just kick them back to the homepage.
Any thoughts/improvements?
Looking for some advice on the below function. I have a web app type site built in WP. There is nothing on the site relevant to non-users, apart from the login form on the homepage. I've got some membership plugins that lock content, but as an added layer of security I was thinking of adding the following:
function lock_non_users_to_front() {
if( !is_user_logged_in() && !is_front_page() ) {
wp_redirect( home_url() );
}
}
add_action( 'template_redirect', 'lock_non_users_to_front' );
Essentially, if anyone tries to access a url that isn't the homepage, and they aren't logged in, it'll just kick them back to the homepage.
Any thoughts/improvements?
Share Improve this question edited May 16, 2019 at 18:31 warm__tape asked May 16, 2019 at 10:26 warm__tapewarm__tape 611 silver badge11 bronze badges 1- 1 Why not use "home_url()" instead of hard coding url? Also die() after wp_redirect(). – gmatta Commented May 16, 2019 at 11:10
1 Answer
Reset to default 1The comment by 'warm_tape' is correct. Use home_url(), then put a die() after the wp_redirect to ensure that the accessed page will 'die' and not interfere with your main page.
本文标签: loginAdvice on redirect to lock site from unauthorized users
版权声明:本文标题:login - Advice on redirect to lock site from unauthorized users 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745487171a2660433.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论