Basic Shortcode Usage
Basic Shortcode Usage
The primary way to display the current user's role is via the [user_role] shortcode. This shortcode is dynamic; it automatically detects the logged-in status of the visitor and retrieves their assigned WordPress role.
Usage in Posts and Pages
You can insert the shortcode directly into your content using the WordPress editor.
- Open the Post or Page where you want the role to appear.
- If you are using the Block Editor (Gutenberg):
- Click the + icon to add a new block.
- Search for and select the Shortcode block.
- Enter
[user_role]into the field.
- If you are using the Classic Editor:
- Paste
[user_role]directly into the text editor where you want the role to display.
- Paste
Usage in Widget Areas
To show the user's role in sidebars, footers, or other widget-ready zones:
- Navigate to Appearance > Widgets in your WordPress dashboard.
- Add a Shortcode block (or a Text widget) to your preferred sidebar.
- Input the shortcode:
[user_role] - Click Update or Save.
Expected Output
The shortcode identifies the role of the user currently viewing the page. The output is a simple string representing the role slug.
- For an Administrator: The output will be
administrator. - For an Editor: The output will be
editor. - For Logged-out Users: If a visitor is not logged in, the shortcode will return an empty string, effectively displaying nothing.
Example Implementation:
If you want to create a personalized welcome message, you might write:
Welcome back! Your current site permissions are: [user_role]
Result on the frontend: "Welcome back! Your current site permissions are: subscriber"