我以前看到过这个问题,但并不完全是为了达到我的目的。
基本上,我想要的是:如果用户未登录并且位于-此页面-或-此页面-或-此页面,请将其重定向到-此页面-(这是一个自定义注册页面)
我正在调整这段代码,但它不起作用。我将感谢任何类型的指导。
<?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\');
?>
最合适的回答,由SO网友:gmazzap 整理而成
你的功能很好,但是\'get_header\'
太晚了。
使用template_redirect
而是:
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();
}
});
确保不在中包含“用户注册”页面id
$restricted
否则您将经历无休止的重定向。。。