If the current user is an administrator or editor

How can I check to see if the current logged-in user is an administrator or an editor?

I know how to do each individually:

<?php if(current_user_can('editor')) { ?> 
    <!-- Stuff here for editors -->
<?php } ?>

<?php if(current_user_can('administrator')) { ?>
    <!-- Stuff here for administrators -->
<?php } ?>

But how do I work those in together? I.e., the user is an administrator or editor?

First answer, not WordPress-related because it is just only PHP: Use the logic “OR” operator:

<?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:

$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 } ?>

However, current_user_can can be used not only with users’ role name, but also with capabilities.

So, once both editors and administrators can edit pages, your life can be easier checking for those capabilities:

<?php if( current_user_can('edit_others_pages') ) {  ?>
    // Stuff here for user roles that can edit pages: editors and administrators
<?php } ?>

Have a look here for more information on capabilities.

Leave a Comment