The [user_role] Shortcode
Using the [user_role] Shortcode
The [user_role] shortcode is a simple, non-configurable tag used to display the WordPress role assigned to the currently logged-in user. This is particularly useful for personalizing dashboard pages, creating member-only areas, or debugging user permissions on the frontend.
How to Use
Once you have added the code snippet to your functions.php file, you can place the shortcode anywhere within your WordPress content.
In the Block Editor (Gutenberg)
- Open the Page or Post editor.
- Click the (+) icon to add a new block.
- Search for the Shortcode block and select it.
- Enter the following tag into the block:
[user_role] - Update or Publish your page.
In the Classic Editor
- Open the Page or Post editor.
- Ensure you are in the Visual or Text tab.
- Paste
[user_role]directly into the editor where you want the role to appear.
Examples
The shortcode can be used as a standalone element or integrated into your existing sentences.
Simple Display: To show the user's role on its own:
[user_role]
Output: Administrator
Within Text: To create a personalized greeting or notification:
Hello! Your current access level is: [user_role]
Output: Hello! Your current access level is: subscriber
Expected Output & Behavior
- Logged-in Users: The shortcode will return the primary role slug (e.g.,
administrator,editor,author,contributor,subscriber) assigned to the user. - Logged-out Users: If a visitor is not logged in, the shortcode will return an empty string or nothing at all, as there is no active session to retrieve a role from.
- Styling: The output is returned as plain text. To style the output, wrap the shortcode in HTML tags within your editor:
<strong>Role: [user_role]</strong>
Developer Note
This shortcode fetches the role from the global $current_user object. It is intended for frontend display and does not require any additional parameters or attributes.