Manual Installation
Manual Installation
Follow these steps to manually integrate the user role display functionality into your WordPress theme.
1. Prepare Your Environment
Before making changes to your theme files, it is highly recommended to use a Child Theme. This ensures that your customizations are not lost when the parent theme is updated.
Additionally, always create a backup of your functions.php file before editing.
2. Locate the Snippet
- Navigate to the
app.phpfile in this repository. - Copy the entire PHP code block contained within the file.
3. Add to functions.php
- Access your WordPress site files via FTP, File Manager, or the built-in WordPress Theme Editor (Appearance > Theme File Editor).
- Open the
functions.phpfile of your active theme (or child theme). - Scroll to the bottom of the file and paste the code snippet you copied from
app.php. - Save the changes.
// Example of how the code should appear at the end of functions.php
function wpsur_show_user_role() {
// ... snippet logic ...
}
add_shortcode('user_role', 'wpsur_show_user_role');
4. Verify Installation
Once the code is saved, the [user_role] shortcode is registered and ready for use.
Usage Guide
You can display the current logged-in user's role anywhere that supports shortcodes.
In the Block Editor (Gutenberg)
- Open a Post or Page.
- Add a Shortcode block.
- Enter the following:
[user_role] - Publish or Update the page.
In Classic Editor
Simply type [user_role] directly into the text editor where you want the role to appear.
In Theme Template Files
If you want to hardcode the user role display directly into a theme template (like header.php or sidebar.php), use the do_shortcode function:
<?php echo do_shortcode('[user_role]'); ?>
Expected Output
- Logged-in User: Displays the primary role assigned to the user (e.g., "Administrator", "Editor", "Subscriber").
- Guest User: If the user is not logged in, the shortcode will return an empty string or nothing by default.