你的任何答案都不起作用,只是增加了一点,它起作用了!这里是我的代码:
function login_redirect( $redirect_to, $request, $user ){
if(isset($_REQUEST[\'redirect_to\'])){
return $_REQUEST[\'redirect_to\'];
}
return admin_url();
}
add_filter( \'login_redirect\', \'login_redirect\', 10, 3 );
function restrict_access_if_logged_out(){
if (!is_user_logged_in() && !is_home()){
$redirect = home_url() . \'/wp-login.php?redirect_to=\' . esc_url($_SERVER["HTTP_HOST"] . urlencode($_SERVER["REQUEST_URI"]));
wp_redirect( $redirect );
exit;
}
}
add_action( \'wp\', \'restrict_access_if_logged_out\', 3 );
只有我添加了
/wp-login.php
与@Matt的回答相比,但对我来说是关键。希望有帮助!:)
**编辑:
当您强制wordpress在HTTPS中导航时,我检测到一个错误。此方法无效,因为重定向位于HTTP中。为了解决这个问题,我更改了函数。结果如下:
function restrict_access_if_logged_out(){
global $wp;
$protocol=\'http\';
if (isset($_SERVER[\'HTTPS\']))
if (strtoupper($_SERVER[\'HTTPS\'])==\'ON\')
$protocol=\'https\';
if (!is_user_logged_in() && !is_home() && ($wp->query_vars[\'pagename\'] != \'downloads\') ){
$redirect = home_url() . "/wp-login.php?redirect_to= $protocol://" . $_SERVER["HTTP_HOST"] . urlencode($_SERVER["REQUEST_URI"]);
wp_redirect( $redirect );
exit;
}
}
add_action( \'wp\', \'restrict_access_if_logged_out\', 3 );
我检查了协议,然后删除了\'
esc_url
\' 并添加了正确的协议:
$protocol://
. 我还改变了
""
.
我以this page.