Snippets / PHP
PHP
HTML
Display Current Logged in User Role
D
Donald McGuinn
·
175 views
·
Updated 1 year ago
This will show the current user role.
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');
PHP · 20 lines
Shortcode to put in elementor.
[display_user_role]
HTML · 1 lines
Discussion
Ask a question or share how you used this.
Log in
to join the discussion.
No comments yet — be the first.