• Skip to primary navigation
  • Skip to main content

RocketGeek

Home of WP-Members, The Original WordPress Membership Plugin

  • WordPress Plugins
    • WP-Members
      • FAQs
      • Quick Start
      • Documentation
      • Extensions
    • Advanced Options
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Download Protect
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Invite Codes
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • MailChimp Integration
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • PayPal Subscriptions
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Salesforce Web-to-Lead
    • Security
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Text Editor
      • Purchase the Plugin
      • Get the Pro Bundle
    • User List
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • User Tracking
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Memberships for WooCommerce
    • WordPass
  • Blog
    • Basics
    • Tips and Tricks
    • Filters
    • Actions
    • Code Snippets
    • Shortcodes
    • Design
    • Release Announcements
  • Store
    • Cart
    • Checkout
  • Contact
  • Log In
  • Show Search
Hide Search
Home » Code Snippets » Custom shortcode: Display user role

Custom shortcode: Display user role

Chad Butler · Jun 1, 2021 ·

/**
 * Adding the following to your functions.php file will give you a custom
 * shortcode to display a user's role.  The shortcode can be used as follows:
 *
 * [display_user_role] -> this would display the current user's role.
 *
 * [display_user_role id=123] -> displays the role for user ID 123.
 */
add_shortcode( 'display_user_role', function( $atts, $content, $tag ) {
    global $wp_roles;
    $user_id = ( isset( $atts['id'] ) ) ? $atts['id'] : get_current_user_id();
    $roles = wpmem_get_user_role( $user_id );
    if ( is_array( $roles ) ) {
        foreach ( $roles as $role ) {
            $display[] = translate_user_role( $wp_roles->roles[ $role ]['name'] );
        }
        $content = implode( ", ", $display );
    } else {
        $content = translate_user_role( $wp_roles->roles[ $roles ]['name'] );
    }
    return $content;
}, 10, 3);

Not sure what to do with this code?

You're not a "coder" and don't know what to do? Don't worry! Code Snippets are the basic building blocks of WordPress customization, and once you know the basics, they are simple to use.

Here are some free articles to get you started:

  • Using Code Snippets from the Site
  • Using a code snippets plugin
  • The functions.php File
  • Create a plugin file for custom functions
  • Create a child theme
  • Do not modify plugin files!

For "hands on" help, consider a plugin support subscription or the Pro Bundle.

Ready to get started?

Join Today!

© 2025 · butlerblog.com · RocketGeek is built using WordPress, WP-Members, and the Genesis Framework

  • butlerblog.com
  • WP-Members Support Subscription
  • Terms of Service
  • Privacy Policy
  • Refund Policy