A shortcode function that returns a list of the current user's roles they have assigned.

Home » Snippets » A shortcode function that returns a list of the current user's roles they have assigned.
0

Created with:

Visibility: 

public

Creator: Haugendaus

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
					<?php
/**
 * Plugin Name: WP Turbo Plugin
 * Description: Shortcode to display current user's roles.
 * Version: 1.0
 * Author: WP Turbo
 * Author URI: https://wpturbo.dev
 * Text Domain: wpturbo
 */

function wpturbo_get_current_user_roles() {
    $current_user = wp_get_current_user();
    $roles = $current_user->roles;

    $output = '<ul>';
    foreach ($roles as $role) {
        $output .= '<li>' . translate_user_role($role) . '</li>';
    }
    $output .= '</ul>';

    return $output;
}
add_shortcode('current_user_roles', 'wpturbo_get_current_user_roles');
				

Explanation:

You can now use the [current_user_roles] shortcode to display the list of roles assigned to the current user.

Register an account to save your snippets or go Pro to get more features.