Shortcode Reference
Shortcode Reference
The [user_role] shortcode is a lightweight tool designed to display the WordPress role of the currently logged-in user directly within your site's content. It is ideal for personalized dashboards, welcome messages, or member-only areas.
Basic Usage
To display the current user's role, simply insert the shortcode into any content area that supports WordPress shortcodes.
[user_role]
How to Use
You can use this shortcode across various parts of your WordPress site:
In Posts and Pages
Add the shortcode directly within the Gutenberg editor using a Shortcode block, or paste it into the Classic Editor text area.
Example:
"Hello! You are currently logged in as a [user_role]."
In Widgets
Display the user's role in your sidebar or footer by adding the shortcode to a Text widget or Custom HTML widget. (Note: Ensure your theme supports shortcodes in widgets).
In Template Files
If you are a developer and want to display the user role directly in your theme files (like header.php or sidebar.php), use the do_shortcode function:
<?php echo do_shortcode('[user_role]'); ?>
Behavior and Output
- Logged-in Users: The shortcode will output the primary role assigned to the user (e.g.,
administrator,editor,subscriber). - Logged-out Users: If a visitor is not logged in, the shortcode will return an empty string, effectively hiding the output.
- Formatting: The output is returned as plain text, allowing it to inherit the typography styles of the surrounding paragraph or container.
Practical Example
You can wrap the shortcode in HTML to style the output. For example, to highlight the role in a notice box:
<div class="user-info-badge">
<strong>Account Type:</strong> [user_role]
</div>