WordPress重定向功能



我正在构建一个私人WooCommerce门户网站,供客户订购。

我正在努力避免臃肿的会员插件,在网上找到了这个代码,效果很好,但我希望它重定向到url,如果"任何"页面被注销的用户访问,而不仅仅是一个单一的页面ID

add_action( 'template_redirect', 'redirect_if_user_not_logged_in' );
function redirect_if_user_not_logged_in() {
if ( is_page('slug || ID') && ! is_user_logged_in() ) { //example can be is_page(23) where 23 is page ID
wp_redirect( 'http://your-redirect-page-here '); 

exit;// never forget this exit since its very important for the wp_redirect() to have the exit / die

}

}

我已经尝试删除页面检查以及wordpress文档中提到的is_page((

// When any single Page is being displayed.
is_page();

// When Page 42 (ID) is being displayed.
is_page( 42 );

// When the Page with a post_title of "Contact" is being displayed.
is_page( 'Contact' );

// When the Page with a post_name (slug) of "about-me" is being displayed.
is_page( 'about-me' );

/*
* Returns true when the Pages displayed is either post ID 42,
* or post_name "about-me", or post_title "Contact".
* Note: the array ability was added in version 2.5.
*/
is_page( array( 42, 'about-me', 'Contact' ) );

是否取消页面检查?未测试。

if ( !is_user_logged_in() ) {

最新更新