customization - Custom column under All Users (multisite network admin)?

admin2025-01-07  4

I want to add a field/column for the super admin/multisite network page under All Users. I want to show a column called 'company' as a column under each user. How do I make this possible? I am able to make this show up under each sites "All Users" but not on the Network Admin pages.

Thank you!

//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);

This function is taken directly from a function which works on the regular User list (not network). Replaced manage_users_columns with wpmu_users_columns and manage_users_custom_column with wpmu_users_custom_column. But it does not work in Network users list.

I want to add a field/column for the super admin/multisite network page under All Users. I want to show a column called 'company' as a column under each user. How do I make this possible? I am able to make this show up under each sites "All Users" but not on the Network Admin pages.

Thank you!

//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);

This function is taken directly from a function which works on the regular User list (not network). Replaced manage_users_columns with wpmu_users_columns and manage_users_custom_column with wpmu_users_custom_column. But it does not work in Network users list.

Share Improve this question edited Apr 5, 2018 at 9:00 jockebq asked Apr 4, 2018 at 15:48 jockebqjockebq 4631 gold badge6 silver badges17 bronze badges 5
  • 1 Possible duplicate of Add custom column to Users admin panel – Pat J Commented Apr 4, 2018 at 15:58
  • But that one is not for Multisite Network. I am able to add the column to each single site. But how do I add it to the Multisite Network admin, All Users? – jockebq Commented Apr 4, 2018 at 16:07
  • The manage_users_custom_column is used by the WP_MS_Users_List_Table class, so you should be able to use it in the Multisite user list. If you want your column to only appear in the network user list, you can check to make sure you're on the Network User screen using get_current_screen(). – Pat J Commented Apr 4, 2018 at 16:27
  • 1 After further investigation, it appears that the filter you want for the Network Admin user list is wpmu_users_columns instead of manage_users_custom_column. – Pat J Commented Apr 4, 2018 at 16:40
  • Great, that let's me create a column, but I cannot print any data in it. Updated the first post with the code I have now. I have taken code which works perfect on the regular users list and changed the filters. But it doesnt print anything? – jockebq Commented Apr 5, 2018 at 9:00
Add a comment  | 

1 Answer 1

Reset to default 2

This is all you need to add a column to the network users table, put it before a chosen column, and add data to it.

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

UPDATE - How to make the column sortable

Use this filter hook

add_filter('manage_users-network_sortable_columns', '');

Reference here - it works the exact same way as manage_users_sortable_columns which is the default for single-site Users table.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736263813a918.html

最新回复(0)