PHP Code SnippetAfter installing a child theme, navigate to Appearance > Theme Editor > functions.php and paste at the bottom of the input.
function display_user_role_shortcode() { // Check if user is logged in if (is_user_logged_in()) { // Get the current user object $user = wp_get_current_user();
// Get the roles assigned to the user $roles = (array) $user->roles;
// Convert roles array into a readable string with each role capitalized $roles_list = array_map('ucfirst', $roles); return implode(', ', $roles_list); } else { // Return nothing if user is not logged in return ''; } }
// Register shortcode with WordPress add_shortcode('display_user_role', 'display_user_role_shortcode');