Question: How can I check to see if the current logged-in user is an administrator or an editor?
Answer: In this example, we have used “OR” operator to check logic. Below is an example:
1 2 3 | <?php if( current_user_can('editor') || current_user_can('administrator') ) { ?> // Stuff here for administrators or editors <?php } ?> |
If you want to check more than two roles, you can check if the roles of the current user is inside an array of roles, something like:
1 2 3 4 5 | $user = wp_get_current_user(); $allowed_roles = array('editor', 'administrator', 'author'); <?php if( array_intersect($allowed_roles, $user->roles ) ) { ?> // Stuff here for allowed roles <?php } ?> |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.