Styling the Output
Styling the Output
By default, the [user_role] shortcode renders the current user's role as plain text. To integrate the output into your theme's design, you can target the output using CSS via your theme's stylesheet or the WordPress Customizer.
Targeting the Output
If you want to apply specific styles to the user role, it is best practice to wrap the shortcode in a container within the WordPress editor (Gutenberg, Classic Editor, or Page Builders).
In the Block Editor (Gutenberg): Insert the shortcode inside a Shortcode block or a Paragraph block, and use the "Advanced" sidebar to add an "Additional CSS Class" to the block.
In HTML/Classic Editor:
Wrap the shortcode manually in a <span> or <div>:
<span class="my-custom-role">[user_role]</span>
CSS Styling Examples
Once you have wrapped your shortcode, you can add CSS to your theme's style.css file or under Appearance > Customize > Additional CSS.
1. Basic Formatting
Make the user role bold and capitalized to ensure it stands out:
.my-custom-role {
font-weight: 700;
text-transform: capitalize;
color: #2c3e50;
}
2. Creating a Badge Style
To display the user role as a colored tag or badge, use the following snippet:
.my-custom-role {
display: inline-block;
background-color: #0073aa; /* WordPress Blue */
color: #ffffff;
padding: 2px 10px;
border-radius: 12px;
font-size: 0.8rem;
line-height: 1.4;
text-transform: uppercase;
}
Best Practices for Layout
- Vertical Alignment: If you are placing the shortcode next to a username or avatar, use
display: inline-flex; align-items: center;on the parent container to ensure proper alignment. - Empty States: If the user is not logged in, the shortcode will return an empty string. Ensure your CSS containers do not have fixed padding or borders that might show as empty boxes when no role is present. You can use the
:emptypseudo-class to hide empty containers:.my-custom-role:empty { display: none; }
Advanced: Customizing the Wrapper in PHP
If you prefer the shortcode to always include a class without manual wrapping in the editor, you can modify the return statement in your app.php file:
// Inside the shortcode function in app.php
return '<span class="wp-user-role">' . esc_html( $role ) . '</span>';
This allows you to globally target .wp-user-role across your entire site.