I have a problem here guys and I need your suggestions. The thing is that I created a code for a wordpress website in which a administrator is able to change roles of user from a page not the administrator dashboard. Now I have created the code and I am stuck in one place, the problem is that upon submit, the data should be changed for each and every user which has updated selected roles. But at the moment it only processes the last one in que. So the issue is that the role is not being updated for each different user and besides updating each the code only processes the last user in que.
Image Help
var_dump
results ...
CODE:
function custom_get_users() {
require_once('wp-admin/includes/template.php');
require_once('wp-includes/registration.php');
if ( !function_exists('get_editable_roles') ) {
require_once( ABSPATH . '/wp-admin/includes/user.php' );
}
global $current_user;
global $wp_roles;
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);
if ( is_user_logged_in() && $user_role == 'administrator' ) {
$args = array(
'orderby' => 'ID',
'order' => 'ASC',
);
$blogusers = get_users( $args );
if ( isset( $_POST['submit'] ) ) {
$user_id = $_POST['user_id'];
$new_role = $_POST['userRole'];
var_dump($user_id);
var_dump($new_role);
// update user role using wordpress function
wp_update_user( array ('ID' => $user_id, 'role' => $new_role ) ) ;
}
echo "<form method='post' action='#'>";
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>";
// Array of WP_User objects.
foreach ( $blogusers as $user ) {
$user_id = $user->ID;
$user_role = $user->roles;
echo '<tr>';
echo '<td><input type="text" name="user_id" value="' . $user_id . '" readOnly="true"></td>';
echo '<td>' . esc_html( $user->display_name ) . '</td>';
echo '<td><select name="userRole">';
wp_dropdown_roles( $user_role[0] );
echo '</select></td>';
echo '</tr>';
}
echo '</table>';
echo '<input type="submit" name="submit" id="submit" class="button button-primary" value="Save">';
echo '</form>';
} else {
echo "Only Administrator can see this page.";
}
}