user1411837
user1411837

Reputation: 1564

WordPress - Check if user is logged in

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.

In my header.php the function is_logged_in doesn't seem to work.

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

Any advice would be helpful.

Upvotes: 51

Views: 171738

Answers (8)

Bud Damyanov
Bud Damyanov

Reputation: 31839

A neat way to check if particular user is online is to check the session_tokens meta key from usermeta table. Place this function in your functions.php and check the returned value:

<?php
if(!function_exists('check_last_login_time')) {
    function check_last_login_time($user_id) {
        $login_time = NULL;
        //Get only 1 record from Database: parameter `true`
        $user_session_tokens = get_user_meta($user_id, "session_tokens", true); 
        if(is_array($user_session_tokens) && !empty($user_session_tokens)) {
            foreach($user_session_tokens as $key=>$val) {
                $login_time = $val["login"];
            }
        } 
        return $login_time;
    }
}

Note: if there is no session_tokens key, that means that user is not online (i.e. not logged-in), otherwise the variable returned by the function will contain UNIX timestamp of last successful login time.

Upvotes: 0

Chris
Chris

Reputation: 453

I had the exact same issue and found that the login check function needs to fire within the 'template_redirect' hook, e.g:

function add_login_redirect()
{

    if (!is_user_logged_in()) {
        wp_redirect(home_url('/wp-admin'));
        exit;
    }
}

add_action('template_redirect', 'add_login_redirect');

Upvotes: 0

Abdo-Host
Abdo-Host

Reputation: 4103

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!';
}

?>

Upvotes: 6

Đỗ Khắc Phong
Đỗ Khắc Phong

Reputation: 11

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.

add at dashboard page

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

Upvotes: 1

suspectus
suspectus

Reputation: 17268

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

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

More details here get_current_user_id().

Upvotes: 4

Bhumi Shah
Bhumi Shah

Reputation: 9476

Use the is_user_logged_in function:

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

Upvotes: 97

Yulia T.
Yulia T.

Reputation: 151

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);
});

Upvotes: -1

Nikhil
Nikhil

Reputation: 77

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
}

Upvotes: 3

Related Questions