How to remove column from WordPress users list? You may don’t wish to show some column to the non-admin users with access to the users list, for example.
WordPress “Users” table has 6 built-in columns: checkbox for row selection, Username, Name, E-mail, Role, Posts. Every column has its ID.
Look as them are defined at the WP_Users_List_Table::get_columns()
method of wp-admin/includes/class-wp-users-list-table.php, line #269:
1 2 3 4 5 6 7 8 |
$c = array( 'cb' => '<input type="checkbox" />', 'username' => __( 'Username' ), 'name' => __( 'Name' ), 'email' => __( 'E-mail' ), 'role' => __( 'Role' ), 'posts' => __( 'Posts' ) ); |
If for example you need to remove E-mail column you should unset a column with ’email’ ID. Let’s go to the final step:
1 2 3 4 5 6 7 8 |
add_filter('manage_users_columns','remove_users_columns'); function remove_users_columns($column_headers) { if (current_user_can('moderator')) { unset($column_headers['email']); } return $column_headers; } |
We use manage_users_columns
filter to achieve this purpose. This code removes ‘E-mail’ folder for users with ‘moderator’ role. Replace it with your own one.
In order to remove other column replace ’email’ column ID to that column ID: role, posts, etc.
Other variant for the list of roles:
1 2 3 4 5 6 7 8 9 10 11 12 |
add_filter('manage_users_columns','remove_users_columns'); function remove_users_columns($column_headers) { $roles = array('moderator', 'users-list-viewer'); foreach($roles as $role) { if (current_user_can($role)) { unset($column_headers['email']); break; } } return $column_headers; } |
Another variant for the list of users ID:
1 2 3 4 5 6 7 8 9 10 11 12 |
add_filter('manage_users_columns','remove_users_columns'); function remove_users_columns($column_headers) { global $current_user; $users = array(27, 70530, 70531, 70532); if (in_array($current_user->ID, $users)) { unset($column_headers['email']); } return $column_headers; } |
Put a selected variant of a code to your active theme functions.php
file or into .php
file at the wp-content/mu-plugins/
folder in order to start use it at your site.
A strange thing – manage_users_columns
filter was not documented. I think that a reason is its complex definition. Look into the WP_List_Table
class constructor:
1 |
add_filter( "manage_{$this->screen->id}_columns", array( $this, 'get_columns' ), 0 ); |
This shows that there are a lot of useful filters which may help to manage columns list of any WordPress items list: posts, pages, media library items, any custom post type. What you should know in order to use such filter is just a screen ID of that table list. For example:
List | Screen ID | Filter | Columns ID |
---|---|---|---|
Posts | edit-post | manage_edit-post_columns | cb, title, author, categories, tags, ‘taxonomy-‘ . $taxonomy, comments, date |
Media | upload | manage_upload_columns | cb, title, author, categories, tags, ‘taxonomy-‘ . $taxonomy, parent, comments, date |
Pages | edit-page | manage_edit-page_columns | cb, title, author, comments, date |
Comments | edit-comments | manage_edit-comments_columns | cb, author, comment, response |
Plugins | plugins | manage_plugins_columns | cb, name, description |
Users | users | manage_users_columns | cb, username, name, email, role, posts |
For custom post types you may use this filter: “manage_{$post_type}_posts_columns”.
Источник: https://www.role-editor.com