3

I know how to add columns to the users.php users page with manage_users_columns and manage_users_custom_column, but I don't know to modify the information shown in the default columns (e.g. username, name, email, etc.).

How can I add text as shown by the red "some text here" text in the image below?

enter image description here

5

A Replica Of The Name Column In WP_Users_List_Table

I think we'll have to remove the current Name column and add in our own column instead.

Here are the steps:

Step #1

First we add in our own custom Name column and then remove the default Name column. Here I use the array slicing trick, mentioned here, to do it in a single sweep:

add_filter( 'manage_users_columns', function( $columns )
{
    return array_slice( $columns, 0, 2, true ) 
        + [ 'mycol' => __( 'Name' ) ] 
        + array_slice( $columns, 3, null, true );
} );

Step #2

Then we need to display the first- and lastname of the corresponding user, together with the extra "Some text here" message:

add_filter( 'manage_users_custom_column', function( $output, $column_name, $user_id )
{
    if( 'mycol' === $column_name )
    {
        $u = new WP_User( $user_id ); 
        if( $u instanceof \WP_User )
        {
            // Default output
            $output .= "$u->first_name $u->last_name";

            // Extra output
            $output .= "<p>Some text here!</p>";

            // Housecleaning
            unset( $u ); 
        }
    }       
    return $output;
}, 10, 3 );   

Step #3

Then we only need to adjust the sortable columns with:

add_filter( 'manage_users_sortable_columns', function( $columns )
{
    $columns['mycol'] = 'name';
    return $columns;
} );

to handle the name sorting, that's already supported by the public orderby query varaiable:

/wp-admin/users.php?orderby=name&order=asc

and

/wp-admin/users.php?orderby=name&order=desc

Hopefully you can adjust this to your needs.

  • That's an interesting approach. Are you saying there's no way to filter the existing display and add our own content then? – j08691 Feb 23 '16 at 15:53
  • 2
    I skimmed through source of WP_List_Table and WP_Users_List_Table, and specifically the WP_Users_List_Table::single_row() method, but couldn't find a suitable entry point for this kind of text injection. The @j08691 – birgire Feb 23 '16 at 15:58
  • 1
    This works great in functions.php, but when I added it as a plugin I had to change the priority of the manage_users_custom_column filter to something higher than 10 (11 seemed fine). Thanks! – j08691 Feb 26 '16 at 16:28
  • Glad to hear it worked for you and thanks for the status update @j08691 – birgire Feb 27 '16 at 10:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.