我想要实现的是:
对于每个请求,如果用户没有登录,他/她将被重定向到登录页面。如果用户已登录,则他/她可以访问该站点。
class Auth{
protected static $instance = null;
public function checkAuth(){
auth_redirect();
}
public static function getInstance() {
if (null == self::$instance) {
self::$instance = new self;
}
return self::$instance;
}
}
有了这个选秀课,什么是这个检查最好的动作钩?wp
, init
, template_redirect
?用法示例:
add_action(\'wp\', array(Auth::getInstance(), \'checkAuth\'));
UPDATE
我玩了一会儿钩子,我注意到:当我将其挂接到“wp”或“template\\u redirect”时,我会得到登录页面,但每次登录时都会提示我再次登录,一次又一次。
当我把它挂到“init”时
请求URI过长
请求的URL长度超过了此服务器的容量限制。
问题似乎出在auth\\u redirect()wordpress函数上。
我在Auth类中更改checkAuth函数
if(!is_user_logged_in()) wp_safe_redirect(site_url(\'/wp-login.php\'));
我再次测试了这三个挂钩,现在wp和template\\u重定向工作正常,但init挂钩导致重定向循环错误。你知道为什么会这样吗?(参考文献:Using `auth_redirect` : keeps asking me to login even when I'm logged in)