Shortcode Implementation
Shortcode Usage
The primary way to display a user's role on your website is via the [user_role] shortcode. This shortcode dynamically fetches the WordPress role assigned to the currently logged-in user and renders it as plain text.
Implementation in Pages and Posts
You can insert the shortcode anywhere within your content using the WordPress editor.
- Gutenberg (Block Editor): Add a Shortcode block and type
[user_role]. - Classic Editor: Simply paste
[user_role]directly into the visual or text editor.
Example Content:
"Welcome back! Your current account level is: [user_role]."
Implementation in Widget Areas
To show the user role in sidebars, footers, or other widget-ready areas:
- Navigate to Appearance > Widgets.
- Add a Shortcode block (or a Text widget in older versions) to your desired sidebar.
- Enter
[user_role]into the content area and save.
Implementation in PHP Templates
If you are a developer and want to hardcode the user role display directly into your theme's template files (like header.php or author.php), use the do_shortcode function:
<?php echo do_shortcode( '[user_role]' ); ?>
Expected Output
The shortcode identifies the role of the user currently viewing the page.
| User State | Output Example |
| :--- | :--- |
| Administrator | administrator |
| Editor | editor |
| Subscriber | subscriber |
| Logged Out | (Empty string) |
Note: If a visitor is not logged in, the shortcode will return an empty value, as there is no active session or role to retrieve.