WordPress Check User Role Function

WordPress usually has a built in function for just about everything—it’s just a matter of finding it. In our case, we needed a function to check if a logged in user had a certain role. If not, show them different content. As far as we could find, one didn’t already exist (as of WordPress 3.5).

So we decided to quickly whip one up and share with the WordPress community. We made it flexible enough to pass in a user id instead of assuming the user was already logged in. It’s also setup for WordPress 3.6 which will allow multiple roles per user (Trac ticket #17924).

/**
 * Checks if a particular user has a role. 
 * Returns true if a match was found.
 *
 * @param string $role Role name.
 * @param int $user_id (Optional) The ID of a user. Defaults to the current user.
 * @return bool
 */
function appthemes_check_user_role( $role, $user_id = null ) {
 
    if ( is_numeric( $user_id ) )
	$user = get_userdata( $user_id );
    else
        $user = wp_get_current_user();
 
    if ( empty( $user ) )
	return false;
 
    return in_array( $role, (array) $user->roles );
}
 
 
// example use for the current user
if ( appthemes_check_user_role( 'customer' )
    _e( "You've got access dude!", 'appthemes' );
else
    _e( "Sorry man, no luck.", 'appthemes' );
 
 
 
// example use for a specific user
$user_id = 23;
 
if ( appthemes_check_user_role( 'customer', $user_id )
    _e( "You've got access dude!", 'appthemes' );
else
    _e( "Sorry man, no luck.", 'appthemes' );

Like this tutorial? Subscribe and get the latest tutorials delivered straight to your inbox or feed reader.

Your rating: none
Rating: 4.7 - 52 votes

Written by: on January 9, 2013. Last modified: January 9, 2013

Popular Marketplace Items

  • thumbnail-vantage-image-slider
    $19

    Image Slider for Vantage

     (2)
    Replace the Vantage listing and events gallery with an image slider.
  • Ad Stuffer for Jobroller by Appthemes
    $19

    Ad Stuffer JobRoller

     (1)
    Stuff your JobRoller Theme with 14 extra Ads & Links!
  • GeoReg thumbnail
    $29

    GeoReg

     (10)
    Captures detailed geographic info with new user registrations.
  • autoauto-thumbnail
    $39

    AutoResponder AutoRegistration

     (4)
    Automatically integrates WordPress registration with major autoresponders.
  • thumbnail-vantage-user-locator
    $19

    User Locator for Vantage

     (7)
    Auto-detect & add your visitors location using geo coordinates.
  • adposter-blocker-thumbnail
    $9

    AdPoster Blocker

     (2)
    A simple but powerful plugin that blocks spammy new user regs and/or ads/jobs/posts.
  • dear-seven-responsive-multipurpose-theme-thumbnail
    $39

    Creative

     (2)
    A responsive multi-purpose WordPress theme for business or personal portfolio.
  • StarStruck WordPress Plugin Thumbnail
    $19

    StarStruck

     (14)
    A fast, lightweight, and elegant star rating system for comments, pages, & posts.
  • critic-icon-2
    $39

    Critic

     (6)
    A professional review and rating system for WordPress.
  • daddy-likes-190x130
    $19

    Daddy Like

     (10)
    A fast, lightweight, and elegant "like" system for comments, pages, posts, & activities.
  • featured
    $9

    QR Codes Widget

     (3)
    Add a widget with QR codes to your AppThemes site.
  • thumbnail
    $29

    Ad Injector

     (3)
    Inject adverts directly into listings and search results. Works with multiple themes.
  • dear-seven-responsive-multipurpose-theme-thumbnail
    $39

    Envelope

     (1)
    A responsive multi-purpose WordPress theme for business or personal portfolio.
  • pcp-featured
    $39

    Price Comparer

     (3)
    Jump start your Price Comparision site with products from Amazon, Commission Junction, and LinkShare.