admin管理员组文章数量:1417050
I've seen this question posted before but not exactly trying to achieve what I want.
Basically what I want is: If user is not logged in AND is on -this page- OR -this page- OR -this page, redirect him to -this page- (which is a custom registration page)
I'm tweaking this piece of code, but it's not working. I'll appreciate any type of guidance.
<?php
function redirect_non_logged_in(){
// if user is not logged and is on this pages
if( !is_user_logged_in() && is_page( array( 250, 253 ) ) {
//This redirects to the custom login page.
wp_redirect(site_url('/user-registration'));
exit();
}
}
add_filter('get_header','redirect_non_logged_in');
?>
I've seen this question posted before but not exactly trying to achieve what I want.
Basically what I want is: If user is not logged in AND is on -this page- OR -this page- OR -this page, redirect him to -this page- (which is a custom registration page)
I'm tweaking this piece of code, but it's not working. I'll appreciate any type of guidance.
<?php
function redirect_non_logged_in(){
// if user is not logged and is on this pages
if( !is_user_logged_in() && is_page( array( 250, 253 ) ) {
//This redirects to the custom login page.
wp_redirect(site_url('/user-registration'));
exit();
}
}
add_filter('get_header','redirect_non_logged_in');
?>
Share
Improve this question
edited Mar 20, 2014 at 13:29
gmazzap
46.3k6 gold badges95 silver badges147 bronze badges
asked Mar 19, 2014 at 0:54
rohnrohn
131 gold badge1 silver badge3 bronze badges
0
2 Answers
Reset to default 6Your function is fine, but 'get_header'
it's too late.
Use template_redirect
instead:
add_action( 'template_redirect', function() {
if ( is_user_logged_in() || ! is_page() ) return;
$restricted = array( 250, 253 ); // all your restricted pages
if ( in_array( get_queried_object_id(), $restricted ) ) {
wp_redirect( site_url( '/user-registration' ) );
exit();
}
});
Be sure to not include 'user-registration' page id in $restricted
array or you'll experience an endless redirect...
Install page restrict https://da.wordpress/plugins/pagerestrict/
In settings choose login form show no
insert link in restriction message: <a href="https://your login page/">Login</a>
to view this page/post
本文标签: Redirect not logged in users if they are on a specific page
版权声明:本文标题:Redirect not logged in users if they are on a specific page 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745262346a2650412.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论