php 如果未登录,Wordpress 会重定向用户
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11238375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Wordpress redirect user if not logged in
提问by Ryso
I'm using the following code in functions.php to redirect users who are not logged in, excluding if you are in wp-login.php.
我在functions.php 中使用以下代码来重定向未登录的用户,不包括您是否在wp-login.php 中。
if(!is_user_logged_in() && $pagenow != 'wp-login.php') {
wp_redirect( 'http://signup.mysite.com', 302 );
}
However, I've installed a shopping cart plugin that uses a different login page and basically redirects mysite.com/wp-login.php to mysite.com/account/login.
但是,我安装了一个使用不同登录页面的购物车插件,并且基本上将 mysite.com/wp-login.php 重定向到 mysite.com/account/login。
Is there some code I can use to exclude url paths from redirecting, perhaps something to replace $pagenow != 'wp-login.php'?
是否有一些代码可以用来从重定向中排除 url 路径,也许可以替换一些东西$pagenow != 'wp-login.php'?
回答by madhavaji
How about this..
这个怎么样..
//
// Re-direct not-logged-in users to holding page
//
if(!is_user_logged_in() && curPageURL() != 'http://mysite.com/wp-login.php') {
wp_redirect( 'http://signup.mysite.com', 302 );
exit;
}
//
// Get current page URL
//
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;
}
回答by Jacob King
Open Functions.php...
打开 Functions.php...
Paste:
粘贴:
// Redirect users who arent logged in...
function login_redirect() {
// Current Page
global $pagenow;
// Check to see if user in not logged in and not on the login page
if(!is_user_logged_in() && $pagenow != 'wp-login.php')
// If user is, Redirect to Login form.
auth_redirect();
}
// add the block of code above to the WordPress template
add_action( 'wp', 'login_redirect' );
hope this helps :)
希望这可以帮助 :)
回答by Dominic P
You could experiment with the WordPress is_page( 'login' )or maybe is_page( 'account/login' )conditionals.
您可以尝试使用 WordPressis_page( 'login' )或is_page( 'account/login' )条件语句。
回答by krunal sojitra
<?php
if (!is_user_logged_in()) {
?>
<style>
.lor1{ display:none;}
.lor1.kun{ display: block;}
.lor1.user_not_login{ display: block;}
</style>
<?php } ?>
- main div(.lor1) means comman div start after headerand finesh before footer
- Add new class (.lor1.kun) in main div when user loging this main class display: block;
- make new html Ex. display mess loging frist one (.lor1.user_not_login)
- main div(.lor1) 表示 comman div 在页眉之后开始,在页脚之前精细
- 用户登录此主类时在主div中添加新类(.lor1.kun)显示:block;
- 制作新的 html 示例。显示混乱日志第一个 (.lor1.user_not_login)
ADD this code in header.php
在 header.php 中添加此代码

