我不想让用户看到没有登录的任何页面。目前可以看到首页。但我想把他们重定向到没有登录的登录页面。他们没有登录就不能访问网站的任何页面。有人知道如何在wordpress中做到这一点吗??
header.php包含在每个页面中,你可以用这个get_current_user_id();这个函数返回0如果没有任何用户登录。
在header。php<?php
if( !get_current_user_id() && !preg_match('/login/', $_SERVER['REQUEST_URI'])){ // checks if the user is not logged in and the page is not login page,
// redirect to some other page,
header("Location: ". site_url().'/wp-login/' ); // redirecting to login page
exit()
}
?>
WordPress有一个函数is_user_logged_in()来检查用户是否登录。
所以在header。php顶部的逻辑应该是:
<?php
if ( !is_user_logged_in() ) {
header("Location: http://www.LoginPageUrl.com/");
}
?>