Snippets / PHP
PHP
Display role of the profile author (with custom labels)
U
unidfr
·
138 views
·
Updated 1 year ago
This snippet allows you to display the WordPress role of the author of a profile post (CPT). It's ideal for use in Voxel templates when you want to show user roles (like Administrator, Editor, Member, etc.) with translated or custom labels. The output can be embedded anywhere using a shortcode.
Paste the snippet below into your functions.php file or a custom plugin.In your Voxel template (e.g. single profile), insert a Shortcode widget (not HTML).Use the shortcode [display_profile_author_role] where you want the user role to appear.Customize the role labels in the $role_labels array if needed.
function display_profile_author_role_shortcode() {
$post_id = get_the_ID();
$user_id = get_post_field('post_author', $post_id);
if (!$user_id) return '';
$user = get_user_by('ID', $user_id);
if (!$user) return '';
$roles = (array) $user->roles;
$role_labels = [
'subscriber' => 'Member',
'superviseur' => 'Supervisor',
'editeur' => 'Editor',
'administrator' => 'Administrator',
];
$translated = array_map(function($role) use ($role_labels) {
return $role_labels[$role] ?? ucfirst($role);
}, $roles);
return implode(', ', $translated);
}
add_shortcode('display_profile_author_role', 'display_profile_author_role_shortcode');
PHP · 25 lines
Discussion
Ask a question or share how you used this.
Log in
to join the discussion.
No comments yet — be the first.