[英]wp is_user_logged_in not working on home page
I'm having an issue with is_user_logged_in()
and was hoping to get some help.... the main site URL is http://www.thecuriosityworkshop.com I'm using is_user_logged_in()
to echo certain content to signed in users, and it all works fine within the inner pages, but doesn't work on the home page.我在使用is_user_logged_in()
遇到问题,希望能得到一些帮助....主站点 URL 是http://www.thecuriosityworkshop.com我正在使用is_user_logged_in()
向登录用户回显某些内容, 并且在内页中一切正常,但在主页上不起作用。
Even when I log in as an admin and visit the home page is_user_logged_in()
shows false
, but when I go to any of the inner pages, is_user_logged_in()
shows true
.即使我以管理员身份登录并访问主页is_user_logged_in()
显示false
,但当我转到任何内页时, is_user_logged_in()
显示true
。 Any idea what is happening?知道发生了什么吗?
For the sake of testing I'm just doing the following:为了测试,我只是在做以下事情:
if ( is_user_logged_in() ) {
echo 'true';
} else {
echo 'false';
}
Use this code:使用此代码:
//variable for check user logged in
$isUserLogged = false;
//Checks if the current visitor is a logged in user.
if ( !function_exists('is_user_logged_in') ) {
$user = wp_get_current_user();
if ( !empty($user->ID) ) {
$isUserLogged = true;
}
} else {
if ( is_user_logged_in() ) {
$isUserLogged = true;
}
}
I had this same issue and when I changed my .htaccess file to disable all caching it solved the problem.我遇到了同样的问题,当我更改 .htaccess 文件以禁用所有缓存时,它解决了问题。 This is the best code I have found for this:这是我为此找到的最好的代码:
#prevent caching
<filesMatch "\.(html|htm|js|css)$">
FileETag None
<ifModule mod_headers.c>
Header unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
</ifModule>
</filesMatch>
whether the above code works in your .htacess file or not, try and go to browser tools -> clear browsing data and delete all cache before logging in, that should do it.无论上面的代码是否在您的 .htacess 文件中工作,请尝试转到浏览器工具 -> 在登录前清除浏览数据并删除所有缓存,应该这样做。
I browsed around a long time, stuck on this.我浏览了很长时间,坚持这个。
I had made an extra page, where I used WordPress-authentication.我做了一个额外的页面,在那里我使用了 WordPress 身份验证。
This was basically it:基本上就是这样:
https://example.org/vips-only/login.php https://example.org/vips-only/login.php
require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php' );
defined( 'ABSPATH' ) or die( 'This page cannot be accessed directly.' );
if( is_user_logged_in() ){
echo 'You are logged in, mate';
} else {
$redirect_to = get_site_url() . '/vips-only/welcome.php';
wp_login_form([
'redirect' => $redirect_to
]);
}
For some reason I couldn't get that is_user_logged_in()
to work.由于某种原因,我无法让is_user_logged_in()
工作。 Even if I first logged in via the 'real' WordPress-login form (and not this custom one I had setup).即使我第一次通过“真正的”WordPress 登录表单(而不是我设置的自定义表单)登录。
What solved it for me was to create a new file and copy everything over in that, like: https://example.org/vips-only/new-login.php .为我解决的是创建一个新文件并复制其中的所有内容,例如: https://example.org/vips-only/new-login.php 。 I have no idea why that worked, but it did.我不知道为什么这有效,但确实如此。
It might be cookie-related.它可能与 cookie 相关。 It might be related to a plugin (I was using WPS Hide Login ).它可能与插件有关(我使用的是WPS Hide Login )。 I thought the problem would return, but it hasn't yet.我以为问题会回来,但还没有。
Try to put this in header.php :尝试将其放在header.php 中:
if ( is_user_logged_in() ) {
$logged = TRUE
} else {
echo 'false';
$logged = FALSE
}
Then just add this condition on any content :然后只需在任何内容上添加此条件:
if($logged) {
echo 'Logged in';
} else {
echo 'not logged in';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.