你可以在管理部分这样做-
add_action( \'admin_init\', \'redirect_non_logged_users_to_specific_page\' );
function redirect_non_logged_users_to_specific_page() {
if ( !is_user_logged_in() && is_page(\'add page slug or i.d here\') &&
$_SERVER[\'PHP_SELF\'] != \'/wp-admin/admin-ajax.php\' ) {
wp_redirect( \'http://www.example.dev/page/\' );
exit;
}
对于前端-
add_action( \'template_redirect\', \'redirect_to_specific_page\' );
function redirect_to_specific_page() {
if ( is_page(\'slug\') && ! is_user_logged_in() ) {
wp_redirect( \'http://www.example.dev/your-page/\', 301 );
exit;
}
}
还有这个
auth_redirect 函数用于后端。如果要在前端使用它,请添加以下过滤器-
add_filter( \'auth_redirect_scheme\', \'the_dramatistcheck_loggedin\' );
function the_dramatist_check_loggedin(){
return \'logged_in\';
}
那么
auth_redirect() 将按预期工作:如果用户未登录,请将其重定向到登录表单。