Shortcode Reference
Shortcode Reference
The primary way to interact with this utility is through the [user_role] shortcode. This shortcode allows you to dynamically display the current visitor's WordPress role anywhere that shortcodes are processed (Posts, Pages, Widgets, or Template files).
[user_role]
The shortcode identifies the logged-in user and outputs their assigned role string.
Usage
Insert the shortcode directly into the WordPress Gutenberg editor, Classic editor, or a Text widget:
[user_role]
To use the shortcode directly within a PHP theme template, use the do_shortcode function:
<?php echo do_shortcode('[user_role]'); ?>
Output Behavior
The behavior of the shortcode depends on the authentication status of the visitor:
| User Status | Output |
| :--- | :--- |
| Logged In | Returns the slug of the user's primary role (e.g., administrator, editor, subscriber). |
| Logged Out | Returns an empty string (no output). |
Practical Examples
1. Displaying a Welcome Message You can combine the shortcode with standard text to create a personalized experience for your users.
- Input:
You are currently logged in as: [user_role] - Output (for an Editor):
You are currently logged in as: editor
2. Conditional Visibility (CSS-based)
While this shortcode is primarily for display, you can wrap it in a div or span to use the output as a class for CSS targeting:
<div class="user-status-[user_role]">
Welcome to the dashboard.
</div>
Notes
- Role Format: The shortcode outputs the role slug (e.g.,
author) rather than the display name (e.g.,Author). - Multi-role Users: For users with multiple roles, the shortcode will typically return the primary role associated with the user account.