Quick Start
Quick Start
Get the current user's role displayed on your WordPress frontend in less than five minutes by following these steps.
1. Install the Snippet
Copy the code provided in the app.php file and append it to your theme's functions.php file.
Tip: It is recommended to use a Child Theme or a code snippets plugin to ensure your changes are not lost during theme updates.
// Example: Add this to your functions.php
function wp_show_user_role_shortcode() {
if ( is_user_logged_in() ) {
$user = wp_get_current_user();
$roles = ( array ) $user->roles;
return esc_html( reset( $roles ) );
}
return '';
}
add_shortcode( 'user_role', 'wp_show_user_role_shortcode' );
2. Add the Shortcode
Once the code is saved, you can display the user's role anywhere that supports shortcodes, such as the Block Editor (Gutenberg), Classic Editor, or Sidebar Widgets.
Insert the following shortcode:
[user_role]
3. Verify Output
Navigate to your website frontend while logged in. The shortcode will dynamically render the primary role assigned to your account (e.g., "administrator", "editor", "subscriber").
Usage Example: If you want to create a welcome message in a post or page:
Hello! Your current access level is: [user_role]
Troubleshooting
- Role not appearing: Ensure you are logged in. The shortcode is designed to return an empty string for guest users.
- Syntax Error: Ensure you didn't accidentally paste the code inside another function or leave out the opening
<?phptag if you are creating a new file.