This site is running Voxel Theme Version: 1.6.1.1 | Elementor Plugin Version: 3.29.2

Join the Voxel Guide Community!

Get Involved

Display role of the profile author (with custom labels)

Description/Instructions

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.

Instructions

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
Copy Code

Let's Chat About this Snippet

Chat Toggle
Voxel Guide AI
Voxel Guide AI
Voxel Guide AI
Voxel Guide AI
Ask me anything about Voxel!
Send
Powered by AI24