最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

customization - How can I add data to a custom column in the Users section of the wordpress backend?

programmeradmin1浏览0评论

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?

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?

Share Improve this question asked Dec 10, 2019 at 2:34 sansaesansae 1892 silver badges10 bronze badges 1
  • In case you want to be fully customizable, you can use the plugin Advanced Custom Fields. – koppor Commented Jul 9, 2020 at 22:52
Add a comment  | 

1 Answer 1

Reset to default 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);
发布评论

评论列表(0)

  1. 暂无评论