php WordPress - 检查用户是否登录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19946972/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:15:49  来源:igfitidea点击:

WordPress - Check if user is logged in

phpwordpress

提问by user1411837

I am fairly new to WordPress. On my homepage I have a navigation bar which I only want to show to people who are logged in as users.

我对 WordPress 还很陌生。在我的主页上,我有一个导航栏,我只想向以用户身份登录的人显示。

In my header.phpthe function is_logged_indoesn't seem to work.

在我header.php的功能is_logged_in中似乎不起作用。

I want to place a condition in my header.phpfile to check if the user has logged in (and then display the navigation).

我想在我的header.php文件中放置一个条件来检查用户是否已登录(然后显示导航)。

Any advice would be helpful.

任何意见将是有益的。

回答by Bhumi Shah

Use the is_user_logged_infunction:

使用is_user_logged_in函数:

if ( is_user_logged_in() ) {
   // your code for logged in user 
} else {
   // your code for logged out user 
}

回答by Nikhil

Try following code that worked fine for me

尝试以下对我有用的代码

global $current_user;
get_currentuserinfo();

Then, use following code to check whether user has logged in or not.

然后,使用以下代码检查用户是否已登录。

if ($current_user->ID == '') { 
    //show nothing to user
}
else { 
    //write code to show menu here
}

回答by suspectus

get_current_user_id()will return the current user id (an integer), or will return 0 if the user is not logged in.

get_current_user_id()将返回当前用户 ID(整数),如果用户未登录,则返回 0。

if (get_current_user_id()) {
   // display navbar here
}

More details here get_current_user_id().

更多细节在这里get_current_user_id()

回答by ?? Kh?c Phong

This problem is from the lazy update data request of Chrome. At the first time you go to homepage. Chrome request with empty data. Then you go to the login page and logged in. When you back home page Chrome lazy to update the cookie data request because this domain is the same with the first time you access. Solution: Add parameter for home url. That helps Chrome realizes that this request need to update cookie to call to the server.

这个问题来自于 Chrome 的惰性更新数据请求。第一次进入主页。带有空数据的 Chrome 请求。然后你进入登录页面并登录。当你回到主页时Chrome懒得更新cookie数据请求,因为这个域和你第一次访问时是一样的。 解决方案:为主页 url 添加参数。这有助于 Chrome 意识到此请求需要更新 cookie 以调用服务器。

add at dashboard page

在仪表板页面添加

<?php 
$track = '?track='.uniqid();
?>
<a href="<?= get_home_url(). $track ?>"> <img src="/img/logo.svg"></a>

回答by Abdo-Host

Example: Display different output depending on whether the user is logged in or not.

示例:根据用户是否登录显示不同的输出。

<?php

if ( is_user_logged_in() ) {
    echo 'Welcome, registered user!';
} else {
    echo 'Welcome, visitor!';
}

?>

回答by Yulia T.

I think that. When guest is launching page, but Admin is not logged in we don`t show something, for example the Chat.

我觉得。当来宾启动页面但管理员未登录时,我们不会显示某些内容,例如聊天。

add_action('init', 'chat_status');

function chat_status(){

    if( get_option('admin_logged') === 1) { echo "<style>.chat{display:block;}</style>";}
        else { echo "<style>.chat{display:none;}</style>";}

}



add_action('wp_login', function(){

    if( wp_get_current_user()->roles[0] == 'administrator' ) update_option('admin_logged', 1);
});


add_action('wp_logout', function(){
    if( wp_get_current_user()->roles[0] == 'administrator' ) update_option('admin_logged', 0);
});