WordPress - Check if user is authorized

I am new to WordPress. My homepage has a navigation bar that I want to show only to those who are logged in as a user.

In my header.php , the is_logged_in function does not seem to work.

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

Any advice would be helpful.

+22
php wordpress
source share
5 answers

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 } 
+44
source share

Try using the following code which works great for me

 global $current_user; get_currentuserinfo(); 

Then use the following code to check if the user is logged in.

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

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

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

Read more here get_current_user_id () .

+2
source share

This issue is due to a lazy Chrome data update request. The first time you go to the home page. Chrome request with empty data. Then you go to the login page and log in. When you return to the Chrome homepage, you are too lazy to update your cookie request because this domain is the same as your first access. Solution : Add a parameter for the home URL. This helps Chrome to understand that in this request, you need to update the cookie to call the server.

add on toolbar page

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

I think so. When the guest launches the page, but Admin does not register, we do not show anything, for example 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); }); 
-one
source share

All Articles