Customizing Role Labels
Customizing Role Labels
By default, the [user_role] shortcode displays the raw role slug as stored in the WordPress database (e.g., administrator, editor, author). You can modify the code in app.php to improve readability by capitalizing the labels or mapping them to custom names.
Capitalizing Role Names
If you want the output to appear as "Administrator" instead of "administrator", you can use the PHP ucfirst() function.
Locate the return statement in your app.php file and wrap the role variable:
// Find the line that returns the role:
return $user_role;
// Replace it with:
return ucfirst($user_role);
Mapping to Custom Labels
To use entirely different names—for example, showing "Member" instead of "Subscriber"—you can implement a simple mapping array within the function.
- Open
app.php. - Define a
$labelsarray. - Check if the current role exists in your map.
Example implementation:
function wp_show_user_role_shortcode() {
$user = wp_get_current_user();
if (empty($user->roles)) {
return '';
}
$role = $user->roles[0];
// Define your custom labels here
$custom_labels = [
'administrator' => 'Site Admin',
'editor' => 'Content Manager',
'subscriber' => 'Member',
];
// Return the custom label if it exists, otherwise return the capitalized slug
return isset($custom_labels[$role]) ? $custom_labels[$role] : ucfirst($role);
}
Using WordPress Translated Names
If you want to use the official display names defined by WordPress (which are localized based on the site language), use the global $wp_roles object:
function wp_show_user_role_shortcode() {
$user = wp_get_current_user();
if (empty($user->roles)) {
return '';
}
$role = $user->roles[0];
$wp_roles = wp_roles();
// Returns the translated "Display Name" of the role
return translate_user_role($wp_roles->role_names[$role]);
}
Summary of Changes
| Desired Output | Code Modification |
| :--- | :--- |
| administrator (Default) | No change required. |
| Administrator | Use ucfirst($role). |
| ADMINISTRATOR | Use strtoupper($role). |
| Custom Name | Use a mapping array as shown above. |