2

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?

1

1 Answer 1

1
  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);
4
  • Thank you so much Sally. This worked perfectly! In other words, @Jack Johansson's answer was incorrect? Jack used add_action and did not return an $output in his function.
    – sansae
    Dec 11, 2019 at 18:20
  • I take it, whenever we use a filter, we HAVE to return something in our function? And so $output has to be included as well? I looked at add_filter here but it's not clear to me how the callback function works. More specifically, the arguments (like $output, $column_name, etc). I haven't been able to find documentation for this. If you can refer me to a resource that talks more about how to construct a callback function like my "last_name_value" fn, please let me know. Thank you so much, Sally :)
    – sansae
    Dec 11, 2019 at 18:33
  • 1
    Hey @sansae :) Sorry, quite busy lately -_- So back then (in 2017),Jack's answer was probably correct and add_action() does actually work on filter hooks because the function uses add_filter(). But personally, if it's a filter hook, I'd always use add_filter() to remind myself that it's a filter and it needs something to be returned. And do you still need help with constructing the hook callback?
    – Sally CJ
    Dec 14, 2019 at 15:23
  • 1
    Hey Sally. Sorry, I kinda checked out after you helped me with this (I also don't check my email regularly). I'm good for now. Thank you so much. As I am an intern at my company and the only developer here, I was really hoping there would be better wordpress documentation. I feel things aren't so clear sometimes. Happy New Year!
    – sansae
    Jan 15, 2020 at 23:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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