douna2917 2015-02-25 14:38
浏览 11
已采纳

提交不同行的结果

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.";
    }

}
  • 写回答

1条回答 默认 最新

  • douxing1353 2015-02-25 14:43
    关注

    Use the array syntax for the input names.

    input type="text" name="user_id[]" value="' . $user_id . '" readOnly="true">
    
    <select name="userRole[]">
    

    Your var_dump()calls should now output an array with all values. Then loop through your arrays using a for loop and update each user contained in the arrays.

    The arrays will contain all users. If you want to only process the users where the role changed, you either have to check the old role in the for loop, or you have to add some javascript code to mark certain rows as updated

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 C++使用TWAIN协议如何实现A3幅面扫描仪扫描A4横向
  • ¥15 如何在sql server里完成筛选
  • ¥15 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题