WordPress How To Check If User Already Logged In
If you are looking for wordpress function to check weather user already logged in or not then below code might be useful for you. WordPress provide one single function called is_user_logged_in() that return boolean true for logged in user and false if none of the user logged in. Â You can execute different kind of code or display message using this function for logged in user and non logged in users.
Here is an example:
[php]<?php
if ( is_user_logged_in() ) {
// Write your code for logged in user.
} else {
// Write your code for non logged in user.
};
?>
Moreover, you can check level of logged in user and display content based on logged in user roles and capabilities. You can use following code to achieve the same.
[php] <?phpglobal $current_user;
get_currentuserinfo();
if($current_user->user_level==10) {
// Put your code here
} else if($current_user->user_level==9) {
// Put your code here
} else if($current_user->user_level==8) {
// Put your code here
} else if($current_user->user_level==7) {
// Put your code here
} else if($current_user->user_level==6) {
// Put your code here
} else if($current_user->user_level==5) {
// Put your code here
} else if($current_user->user_level==3) {
// Put your code here
} else if($current_user->user_level==2) {
// Put your code here
} else if($current_user->user_level==1) {
// Put your code here
} else if($current_user->user_level==0) {
// Put your code here
}
?>
[/php]
User Level to Role Conversion Graph
Hi
Great code, just trying to get it to work for me 🙂
I am putting the following code in functions.php
—————————————————-
user_level==3) {
// then inserting various functions to remove dashboard items and menus
}
?>
———————————————-
what am i doing incorrectly? Appreciate your help
Sorry the entire code is:
// If current user is 3 which is author then run this code
user_level==3) {
//insert functions to remove dashboard items here
}
?>
You need to use current user object $current_user-> as specified in post. user_level variable not directly available to use.