所有用户(多站点网络管理员)下的自定义列?

时间:2018-04-04 作者:jockebq

我想在“所有用户”下为超级管理员/多站点网络页面添加一个字段/列。我想在每个用户下显示一个名为“company”的列。我如何才能做到这一点?我可以在每个网站的“所有用户”下显示,但不能在网络管理页面上显示。

非常感谢。

//Add column to Network Admin User panel list page
function add_user_columns( $defaults ) {
     $defaults[\'company\'] = __(\'Company\', \'user-column\');
     return $defaults;
}
add_filter(\'wpmu_users_columns\', \'add_user_columns\', 15, 1);

//Print the user data in the new column
function add_custom_user_columns($value, $column_name, $id) {
      if( $column_name == \'company\' ) {
        return get_the_author_meta( \'company\', $id );
      }
}
add_action(\'wpmu_users_custom_column\', \'add_custom_user_columns\', 15, 3);
此函数直接取自在常规用户列表(非网络)上工作的函数。将manage\\u users\\u列替换为wpmu\\u users\\u列,将manage\\u users\\u custom\\u列替换为wpmu\\u users\\u custom\\u列。但它在网络用户列表中不起作用。

1 个回复
最合适的回答,由SO网友:Nikolay 整理而成

这就是向network users表中添加列、将其放在所选列之前以及向其中添加数据所需的全部内容。

add_filter( \'wpmu_users_columns\', \'my_awesome_new_column\' );

add_action( \'manage_users_custom_column\', \'my_awesome_column_data\', 10, 3 );

// Creates a new column in the network users table and puts it before a chosen column
function my_awesome_new_column( $columns ) {
    return my_awesome_add_element_to_array( $columns, \'my-awesome-column\', \'Awesome\', \'registered\' );
}

// Adds data to our new column
function my_awesome_column_data( $value, $column_name, $user_id ) {

    // If this our column, we return our data
    if ( \'my-awesome-column\' == $column_name ) {
        return \'Awesome user ID \' . intval( $user_id );
    }

    // If this is not any of our custom columns we just return the normal data
    return $value;
}

// Adds a new element in an array on the exact place we want (if possible).
function my_awesome_add_element_to_array( $original_array, $add_element_key, $add_element_value, $add_before_key ) {

    // This variable shows if we were able to add the element where we wanted
    $added = 0;

    // This will be the new array, it will include our element placed where we want
    $new_array = array();

    // We go through all the current elements and we add our new element on the place we want
    foreach( $original_array as $key => $value ) {

        // We put the element before the key we want
        if ( $key == $add_before_key ) {
            $new_array[ $add_element_key ] = $add_element_value;

            // We were able to add the element where we wanted so no need to add it again later
            $added = 1;
        }

        // All the normal elements remain and are added to the new array we made
        $new_array[ $key ] = $value;
    }

    // If we failed to add the element earlier (because the key we tried to add it in front of is gone) we add it now to the end
    if ( 0 == $added ) {
        $new_array[ $add_element_key ] = $add_element_value;
    }

    // We return the new array we made
    return $new_array;
}

结束