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.
};
?>

[/php]

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] <?php
global $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

Written by Bala Krishna

Bala Krishna is web developer and occasional blogger from Bhopal, MP, India. He like to share idea, issue he face while working with the code.

This article has 3 comments

  1. Clearly Ice

    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

  2. Clearly Ice

    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

    }
    ?>

    • Bala Krishna

      You need to use current user object $current_user-> as specified in post. user_level variable not directly available to use.