Manual Installation
Manual Installation
Follow these steps to manually integrate the user role display functionality into your WordPress theme. This method involves adding a small PHP snippet to your theme's configuration files.
1. Access Your Theme's functions.php File
To add the shortcode functionality, you need to edit your theme's functions.php file. You can access this file via:
- FTP/SFTP: Navigate to
/wp-content/themes/your-active-theme/functions.php. - WordPress Dashboard: Go to Appearance > Theme File Editor and select
functions.phpfrom the right-hand sidebar.
[!TIP] It is highly recommended to use a Child Theme to prevent your changes from being overwritten during theme updates.
2. Add the Code Snippet
Copy the code provided in the app.php file of this repository and paste it at the bottom of your functions.php file.
// Add the snippet from app.php here
add_shortcode('user_role', 'wp_show_user_role_frontend');
function wp_show_user_role_frontend() {
if ( is_user_logged_in() ) {
$user = wp_get_current_user();
$roles = ( array ) $user->roles;
return esc_html( ucfirst( $roles[0] ) );
}
return '';
}
3. Verify Implementation
Once the code is saved, the [user_role] shortcode is registered and ready for use.
Usage
You can display the current user's role anywhere that supports shortcodes, such as posts, pages, or sidebar widgets.
In the Block Editor (Gutenberg)
- Open the page or post where you want to show the role.
- Add a Shortcode block.
- Enter the following:
[user_role]
In Classic Editor or PHP Templates
If you are using the Classic Editor, simply paste [user_role] into the text area. To call the shortcode directly within a PHP template file (like header.php), use the following:
<?php echo do_shortcode('[user_role]'); ?>
Expected Output
- Logged-in Administrator: Displays "Administrator"
- Logged-in Subscriber: Displays "Subscriber"
- Logged-out User: Displays nothing (empty string)