In this tutorial, we´ll explain you how to costumize the list of users in WordPress dashboard admin panel by using custom coding.
First of all, we´ll start with a simple function that´s is to be added to a theme or a customized plugin. Once handled that step, you can observe gradually how to develop the necessary changes that you would like to include on your platform.
A basic code that we´ll create, will allow us to display a new column with user´s ID in the list. Unfortunately there is no specific panel usage by WordPress that includes this feature yet.
Besides, we´ll also remove the column that indicates the number of entries for each user, so we can get more space and replace it for a new column with the ID.
Remove a users list column on the admin dashboard
First of all, we´ll remove one of the columns of the users list on the admin dashboard of WordPress. Include this function in your plugin or theme that will enable the modification of WordPress and then bind it to the core programming with a Hook.
Code for removing a users list column on the admin dashboard
function wph_admin_user_columns($columns) {
unset($columns['posts']);
return $columns;
}
add_filter('manage_users_columns', 'wph_admin_user_columns', 10, 3);
function wph_admin_user_columns($columns) {
unset($columns['posts']); return $columns;
}
add_filter('manage_users_columns', 'wph_admin_user_columns', 10, 3);
As you can observe in the code, we introduce it to the core of WordPress through a Hook, ‘manage_users_columns’. We include the $columns parameter as well to our ‘wph_admin_user_columns’ function that will access to an array in which the whole columns that are configured on the admin dashboard, are included.
In order to get the ID of the column that we you would like to remove, it is as easy as to inspect the HTML element and use the ID of the tag that contains it. Getting back to our element, we´ll just remove Posts that has ‘posts’ ID by using function unset($columns[‘posts’]);
If your code is OK, the Posts Column ID will dissapear after refreshing your WP admin panel of users.
Add a column to the users list on the admin dashboard of WordPress
Now we´ll add one more column, the correspondent ID of each user. This column is truely useful, particularly when you are programming the each aspect of you WP. This will give you an access to a strategic data that might take you much more effort and time loosing just because of getting this data on the other parts of the dashboard or directly in database.
Let´s use the next function that will help us integrate a code inside our WordPRess installation .
Code for adding a column to the users list on the admin dashboard of WordPress
function wph_admin_user_columns($columns) {
unset($columns['posts']);
$columns['wpsite-show-ids'] = 'ID';
return $columns;
}
add_filter('manage_users_columns', 'wph_admin_user_columns', 10, 3);
function wph_admin_users_custom_columns($value, $column_id, $user_id) {
if ($column_id == 'wpsite-show-ids'){
return $user_id;
}
return $value;
}
add_action('manage_users_custom_column', 'wph_admin_users_custom_columns', 10, 2);
function wph_admin_user_columns($columns) {
unset($columns['posts']);
$columns['wpsite-show-ids'] = 'ID';
return $columns;
}
add_filter('manage_users_columns', 'wph_admin_user_columns', 10, 3);
function wph_admin_users_custom_columns($value, $column_id, $user_id) {
if ($column_id == 'wpsite-show-ids'){
return $user_id;
}
return $value;
}
add_action('manage_users_custom_column', 'wph_admin_users_custom_columns', 10, 2);
You can check it out that not only we have modified wph_admin_user_columns but also we have firstly removed Posts columns and added a new column for the users´IDs with wpsite-show-ids.
Next thing is to use a pretty simple hook ‘manage_users_custom_column’ that will be bind to WordPress and the second function will fill the fields of that column with the user correspondent id. The function will give us within its parameters.
The new column will appear at the end of the list and we can find the associated ID of each of the users in the list. Thanks to this, we can query easily the ID of each user in the same row.
Sort the columns of users list on WP dashboard
Imagine that now we would like to arrange the order of our columns. For instance, it may result more efficient that the ID column next to the user´s name and not at the end of the table.
To do so, you should execute the next code on your installation:
Code for sorting the columns of users list on WP dashboard
function wph_admin_user_columns_order($columns) {
$n_columns = [];
$move = 'wpsite-show-ids';
$before = 'name';
foreach($columns as $key => $value) {
if ($key == $before){
$n_columns[$move] = $move;
}
$n_columns[$key] = $value;
}
return $n_columns;
}
add_filter('manage_users_columns', 'wph_admin_user_columns_order', 10, 3);
function wph_admin_user_columns_order($columns) {
$n_columns = [];
$move = 'wpsite-show-ids';
$before = 'name';
foreach($columns as $key => $value) {
if ($key == $before){
$n_columns[$move] = $move;
}
$n_columns[$key] = $value;
}
return $n_columns;
}
add_filter('manage_users_columns', 'wph_admin_user_columns_order', 10, 3);
As you can see in the code a foreach loop is used to iterate each column. When we get the column before the one we would like to insert to our new column, we´ll alter the content of array.