http://localhost/finsafe/calculator/sip.html
如果我输入上面的网址,我希望它重定向到我的登录页面
以下是我的代码
if(!is_user_logged_in() && curPageURL() = 'http://localhost/finsafe/calculator/sip.html') {
wp_redirect( 'http://localhost/finsafe/wp-login.php?redirect_to=http%3A%2F%2Flocal%3A8095%2Ffinsafe%2Fcontact%2F/', 302 );
exit;
}
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
但任何人都无法帮助
答案 0 :(得分:2)
这是因为您的页面是.html文件,并且您正在尝试在其上运行PHP代码。您需要将.html
文件另存为.php
文件,然后重试。然后将PHP代码包装在php标签中,如此...
<?php
if(!is_user_logged_in() && curPageURL() = 'http://localhost/finsafe/calculator/sip.html') {
wp_redirect( 'http://localhost/finsafe/wp-login.php?redirect_to=http%3A%2F%2F183.82.33.232%3A8095%2Ffinsafe%2Fcontact%2F/', 302 );
exit;
}
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
?>
答案 1 :(得分:0)
试试这个:
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;
}
}
答案 2 :(得分:0)
答案 3 :(得分:0)
您也可以这样检查:
if ( is_user_logged_in() ) {
echo "user logged in";
} else {
header('Location: ' . wp_login_url());
}