You can’t alter the default Name column, but what you can do is hide it and create your own custom column.
First, we add the action and filter on admin_init
that will add and manage our new column:
function wpa66544_user_custom_column_init(){
add_filter( 'manage_users_columns', 'wpa66544_user_add_column' );
add_action( 'manage_users_custom_column', 'wpa66544_user_manage_column', 100, 3 );
}
add_action( 'admin_init', 'wpa66544_user_custom_column_init' );
Next, the function hooked to our filter above which removes the default name column and adds our own custom column:
function wpa66544_user_add_column( $columns ) {
// hide the default name column
unset( $columns['name'] );
// add our custom name column
$columns['custom_name'] = 'Name';
return $columns;
}
Last, the function hooked to the action in the first step which outputs the value of the column for each user:
function wpa66544_user_manage_column( $val, $column_name, $user_id ) {
$last_first="";
if ( 'custom_name' == $column_name ):
$first_name = get_the_author_meta( 'first_name', $user_id );
$last_name = get_the_author_meta( 'last_name', $user_id );
$last_first = $last_name . ' ' . $first_name;
endif;
return $last_first;
}
The one thing missing here is that the column is not sortable. I haven’t quite figured out how to do this, though I know the possible method is via the pre_user_query
action. It seems the SQL has to be modified directly, as the normal means of making a column sortable won’t work in this instance. first/last name are user meta, and you can’t order by user meta within this context of a user query.
It also seems that the default Name column is also not sortable anyway, clicking the Name header just does the same thing as clicking the username header- sorts by username, so I suppose you’re not really losing any functionality here!