I have a question, I have been trying to create a page in which the admin can change the roles of a user from a dropdown menu. Just like we do in the user settings in dashboard. I have been trying the function wp_dropdown_roles() but I think it wont work because its only allowed on wp-admin? The code so far is ..
function custom_get_users() {
global $current_user;
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);
if( is_user_logged_in() && $user_role == 'administrator') {
echo "<table class='widefat'>";
echo "<thead>";
echo "<tr class='thead'>";
echo "<th>User ID</th>";
echo "<th>User Name</th>";
echo "<th>Role</th>";
echo "</tr>";
echo "</thead>";
$args = array(
'orderby' => 'ID',
'order' => 'ASC',
);
$blogusers = get_users( $args );
// Array of WP_User objects.
foreach ( $blogusers as $user ) {
$user_id = $user->ID;
$user_role = wp_dropdown_roles( $user->roles );
echo '<tr>';
echo '<td>' . esc_html( $user_id ) . '</td>';
echo '<td>' . esc_html( $user->display_name ) . '</td>';
echo '<td><select>' . esc_html($user_role ) . '</select></td>';
echo '</tr>';
}
echo '</table>';
} else {
echo "Only Administrator can see this page.";
}
}
Aucun commentaire:
Enregistrer un commentaire