Question

Can anyone help me with adding data to a new User table column that I added?

In the wordpress backend, in the Users section, I added a custom column called "Last Name". Now, I'd like to add last names to that column.

I've looked at this this and this, but nothing has worked.

In my functions.php file, the code below is the closest I've gotten to displaying something on the screen, but the problem is that it's displaying it completely outside of the users list/table (directly above it in fact)

function last_name_value($column_name, $user_id) {
  if ( 'lname' == $column_name ) {
    echo __('last name here','text-domain');
  }
}

add_action('manage_users_custom_column', [$this, 'last_name_value'], 10, 2);

A value of 10 displays 'last name here' above the User table (not where I want it obviously), but I should note that it erases all the data in 5 columns for some reason. Any number below 10 and nothing displays.

I'm sure this is really easy, right? What am I doing wrong?

Was it helpful?

Solution

  1. manage_users_custom_column is a filter hook, so you should use add_filter() and not add_action(). Which also means you should return the output instead of echoing it.

  2. The column name is the second parameter and not the first one — which is the current value for the current column.

So try with:

function last_name_value($output, $column_name, $user_id) {
  if ( 'lname' == $column_name ) {
    return __('last name here','text-domain');
  }
  return $output;
}

add_filter('manage_users_custom_column', [$this, 'last_name_value'], 10, 3);
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top