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

  • marine-marketplace-thumb
    $39

    Marine

     (1)
    A clean, modernĀ and fully responsive marine-based goods WooCommerce Theme.
  • AppThemes Coupon Plugin
    $29

    AppThemes Coupons

     (8)
    An easy way to start offering coupons and promotions to your customers.
  • stripe-plugin
    $39

    Stripe

     (4)
    Process credit cards safely and securely on your AppThemes website.
  • 2Checkout Payment Gateway Plugin
    $39

    2Checkout

     (4)
    Accept up to eight payment methods, fifteen languages, & twenty six currencies.
  • featured
    $9

    QR Codes Widget

     (3)
    Add a widget with QR codes to your AppThemes site.
  • Authorize.Net Thumbnail
    $39

    Authorize.Net

     (3)
    Easily start accepting online payments via credit card and e-check.
  • GeoReg thumbnail
    $29

    GeoReg

     (10)
    Captures detailed geographic info with new user registrations.
  • critic-icon-2
    $39

    Critic

     (7)
    A professional review and rating system for WordPress.
  • pcp-featured
    $39

    Price Comparer

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

    Convert

     (2)
    A conversion focused responsive Clipper child theme with premium features.
  • StarStruck WordPress Plugin Thumbnail
    $19

    StarStruck

     (16)
    A fast, lightweight, and elegant star rating system for comments, pages, & posts.
  • autoauto-thumbnail
    $39

    AutoResponder AutoRegistration

     (4)
    Automatically integrates WordPress registration with major autoresponders.
  • balanced-payments-plugin-sm
    $39

    Balanced Payments

     (3)
    Accept credit cards and setup escrow payments.
  • adposter-blocker-thumbnail
    $9

    AdPoster Blocker

     (2)
    A simple but powerful plugin that blocks spammy new user regs and/or ads/jobs/posts.
  • daddy-likes-190x130
    $19

    Daddy Like

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