Last active
April 23, 2025 19:37
-
-
Save coulterpeterson/bc7efec1563ade5ba27b8810bc4d14c3 to your computer and use it in GitHub Desktop.
Display the current user roles on edit profile screen
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Display the user's current roles on their 'Edit Profile' screen in the WP dashboard. | |
* | |
* @param WP_User $user The current WP_User object. | |
*/ | |
function display_user_roles_on_profile($user) { | |
// Get the user's roles | |
$roles = $user->roles; | |
// Get global WP_Roles object | |
global $wp_roles; | |
// Display the roles | |
echo '<h2>User Roles</h2>'; | |
echo '<table class="form-table">'; | |
echo '<tr>'; | |
echo '<th><label for="user_roles">Roles</label></th>'; | |
echo '<td>'; | |
echo '<ul>'; | |
foreach ($roles as $role) { | |
$role_name = isset($wp_roles->role_names[$role]) ? $wp_roles->role_names[$role] : $role; | |
echo '<li>' . esc_html($role_name) . ' | ' . esc_html($role) . '</li>'; | |
} | |
echo '</ul>'; | |
echo '</td>'; | |
echo '</tr>'; | |
echo '</table>'; | |
} | |
// Hook the function to both profile screens | |
add_action('show_user_profile', 'display_user_roles_on_profile'); | |
add_action('edit_user_profile', 'display_user_roles_on_profile'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment