http://localhost/finsafe/calculator/sip.html
如果我键入上面的url,我希望它重定向到我的登录页面。
以下是我的代码
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;
}但是没有人能帮上忙
发布于 2017-03-22 10:44:29
这是因为您的页面是一个.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;
}
?>发布于 2017-03-22 10:43:14
试试这个:
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;
}
}发布于 2017-03-22 10:46:36
您也可以这样检查:
if ( is_user_logged_in() ) {
echo "user logged in";
} else {
header('Location: ' . wp_login_url());
}https://stackoverflow.com/questions/42949212
复制相似问题