Attributes and Parameters
Shortcode Attributes
The [user_role] shortcode can be customized using attributes to modify how the role is displayed on the frontend. This allows for dynamic text manipulation and fallback handling without modifying the underlying PHP code.
Available Parameters
| Attribute | Type | Default | Description |
| :--- | :--- | :--- | :--- |
| prefix | string | '' | Text or HTML to display immediately before the user role. |
| suffix | string | '' | Text or HTML to display immediately after the user role. |
| case | string | none | Controls the text transformation. Options: lowercase, uppercase, capitalize. |
| guest_text | string | '' | The text to display if the visitor is not logged in. |
Usage Examples
Basic Display
The default usage displays the primary role of the logged-in user in lowercase (as stored in the WordPress database).
[user_role]
<!-- Output: administrator -->
Custom Labeling
Use the prefix attribute to provide context to the role display.
[user_role prefix="Account Type: "]
<!-- Output: Account Type: editor -->
Text Transformation
To ensure the role matches your site's typography, use the case attribute.
[user_role case="uppercase"]
<!-- Output: AUTHOR -->
Handling Logged-Out Users
By default, the shortcode returns an empty string for guests. Use guest_text to provide a fallback value for non-authenticated users.
[user_role guest_text="Guest Visitor"]
<!-- Output (if logged out): Guest Visitor -->
Advanced Combination
You can combine multiple attributes to create complex UI elements, such as a CSS-ready badge.
<span class="user-badge">
[user_role prefix="Role: " case="capitalize" guest_text="Logged Out"]
</span>
<!-- Output: Role: Administrator -->
Technical Notes
- Role Priority: If a user has multiple roles assigned, the shortcode typically retrieves the primary (first) role from the user object array.
- Output Sanitization: The output is passed through standard WordPress filters to ensure it is safe for HTML rendering.
- Shortcode Compatibility: This shortcode can be used within Gutenberg Shortcode blocks, Classic Editor, or directly in theme template files using the
do_shortcode()function.
// Programmatic usage in template files
echo do_shortcode('[user_role case="uppercase"]');