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: 5
Rating: 4.7 - 52 votes

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

Popular Marketplace Items

  • screenshot
    $39

    Convert

     (5)
    A conversion focused responsive Clipper child theme with premium features.
  • classipress-fortumosms-gateway
    $39

    Fortumo SMS payment gateway

     (1)
    Fortumo sms payment gateway for ClassiPress
  • at_autosuggest_taskerr_thumb
    $19

    AutoSuggest – Taskerr

     (1)
    Automatically suggests services as the user types.
  • stripe-plugin
    $39

    Stripe

     (5)
    Process credit cards safely and securely on your AppThemes website.
  • balanced-payments-plugin-sm
    $39

    Balanced Payments

     (3)
    Accept credit cards and setup escrow payments.
  • pcp-featured
    $39

    Price Comparer

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

    Custom Greeting

     (1)
    Easily replace the default WordPress greeting.
  • autoauto-thumbnail
    $39

    AutoResponder AutoRegistration

     (4)
    Automatically integrates WordPress registration with major autoresponders.
  • GeoReg thumbnail
    $29

    GeoReg

     (10)
    Captures detailed geographic info with new user registrations.
  • daddy-likes-190x130
    $19

    Daddy Like

     (10)
    A fast, lightweight, and elegant "like" system for comments, pages, posts, & activities.
  • AppThemes Coupon Plugin
    $29

    AppThemes Coupons

     (10)
    An easy way to start offering coupons and promotions to your customers.
  • StarStruck WordPress Plugin Thumbnail
    $19

    StarStruck

     (18)
    A fast, lightweight, and elegant star rating system for comments, pages, & posts.
  • thumnail
    $19

    Category Icons

     (2)
    Add icons to your ClassiPress categories
  • Authorize.Net Thumbnail
    $39

    Authorize.Net

     (4)
    Easily start accepting online payments via credit card and e-check.
  • adposter-blocker-thumbnail
    $9

    AdPoster Blocker

     (4)
    A simple but powerful plugin that blocks spammy new user regs and/or ads/jobs/posts.
  • critic-icon-2
    $39

    Critic

     (7)
    A professional review and rating system for WordPress.
  • featured
    $9

    QR Codes Widget

     (4)
    Add a widget with QR codes to your AppThemes site.
  • 2Checkout Payment Gateway Plugin
    $39

    2Checkout

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

    Marine

     (1)
    A clean, modernĀ and fully responsive marine-based goods WooCommerce Theme.
  • woo_goodstore_thumb
    $39

    GoodStore

     (1)
    A clean, modernĀ and fully responsive WooCommerce Fashion Theme.
  • classpress tabber
    $12

    CP-Tabber

     (1)
    Show specific custom fields as tabs on an ad detail page.
  • wp-smart-export-final
    $25

    WP Smart Export

     (6)
    A highly customizable WordPress data exporter plugin.