Adding Custom Columns to WordPress Users Dashboard

- Advertisement -

Adding Custom Columns to the WordPress Users Dashboard

I was recently doing some updates to a website that had hundreds of registered users. The updates required checking to see if a specific user meta field was empty or not. Rather than opening every single user and checking, I decided to add a new column to the Users dashboard. This way, I could skim through the dashboard and quickly locate any user that required updating.

- Advertisement -

Adding custom/extra columns to the users dashboard can be very useful when managing sites with many users. Displaying data such as the user’s website, social links, or even Yoast SEO open graph fields can make it easier to manage user data.

Keep on reading to find out how you can add custom user columns without any 3rd party plugins. The code is very simple, and for my example, I will be adding a new “website” column to the users screen.

- Advertisement -

Adding New Columns to the Users Dashboard

To start, we’ll want to hook into the core WordPress `manage_{$screen->id}_columns` filter. This filter is used to register new columns to any admin screen. For our purposes, we’ll specifically target the “users” screen.

- Advertisement -

Here is the code to add a new column to the users dashboard:

“`
/**
* Add new columns to the users dashboard.
*
* @link https://www.wpexplorer.com/how-to-add-custom-columns-to-the-wordpress-users-dashboard/
*/
function wpexplorer_add_new_users_columns( $columns ) {
$new_columns = [
‘website’ => esc_html__( ‘Website’, ‘text_domain’ ),
];
return array_merge( $columns, $new_columns );
}
add_filter( ‘manage_users_columns’ , ‘wpexplorer_add_new_users_columns’ );
“`

For my purposes, I needed to see if the user’s “website” field was empty or not, so that’s what this example will do. With this code added to your site, if you visit the users dashboard, you should see a new “Website” column.

You can add more items to the `$new_columns` array for all the custom columns you need. The columns will be added to the “end” of the table. If you wish to display your custom columns before the core WP columns, you can swap the variables in the `array_merge()` function.

Populate Your Custom User Dashboard Columns

Now that you know how to add new columns, the next step is to display data for the columns. For this, we are going to hook into the `manage_users_custom_column` filter. This filter can be used to modify the output for any user dashboard column. So we’ll need to specifically check for the columns we added.

Here is an example of how you would populate the custom “website” field from the previous snippet:

“`
/**
* Populate new users dashboard columns.
*
* @link https://www.wpexplorer.com/how-to-add-custom-columns-to-the-wordpress-users-dashboard/
*/
function wpexplorer_populate_custom_users_columns( $output, $column_name, $user_id ) {
if ( ‘website’ === $column_name ) {
$user_url = get_userdata( $user_id )->user_url ?? ”;
if ( $user_url ) {
$output = ” . esc_html( esc_url( $user_url ) ) . ”;
} else {
$output = ‘-‘;
}
}
return $output;
}
add_filter( ‘manage_users_custom_column’, ‘wpexplorer_populate_custom_users_columns’, 10, 3 );
“`

With the code added, you should now be able to refresh your users dashboard and view their assigned website in the custom website column added previously.

In this example, I’ve only checked for the website field. If you are adding multiple fields, I would recommend using a switch statement like the following:

“`
function wpexplorer_populate_custom_users_columns( $output, $column_name, $user_id ) {
$user_data = get_userdata( $user_id ); // store user data to use in all custom columns
switch ( $column_name ) {
case ‘custom_column_1’:
$output = ‘Column 1 output’;
break;
case ‘custom_column_2’:
$output = ‘Column 2 output’;
break;
case ‘custom_column_3’:
$output = ‘Column 3 output’;
break;
}
return $output;
}
add_filter( ‘manage_users_custom_column’, ‘wpexplorer_populate_custom_users_columns’, 10, 3 );
“`

Conclusion

As you can see, it’s very easy to add new columns to the users dashboard. If you have any issues or questions, please let me know in the comments! Also, let me know what custom columns you are adding for your WordPress users. I don’t think many websites would ever need to extend the default users dashboard, so I’m curious to know what you are doing!

The post How to Add Custom Columns to the WordPress Users Dashboard appeared first on WPExplorer.

- Advertisement -

Related articles

Ad Spend: Definition and Measurement

What Is Ad Spend & How to Measure It

Tracking your ad spend allows you to be competitive as an advertiser. Learn the basics here.

Googlebot: Understanding Google’s Web Crawler

What Is Googlebot? How Google's Web Crawler Works

Googlebot is the website crawler Google uses to find content on the internet. Learn how it works in this guide.

21 Tips for Effective Google Searches

21 Google Search Tips to Find Exactly What You Want

Get answers faster with these 21 Google search tips: 1) Filter your results, 2) Search within sites, and more.

Adding Custom Options to PTU WordPress Plugin

The Post Types Unlimited plugin, also known as “PTU”, allows you to easily add custom post types and taxonomies to your WordPress site. I created this plugin because other plugins, such as the popular Custom Post Type UI plugin, are bloated & full of upsells. It is a great add-on for any site that requires […]

The post How to Add Custom Options to the PTU WordPress Plugin appeared first on WPExplorer.

13 Digital Marketing Types for Brand Growth in 2024

13 Types of Digital Marketing to Use for Brand Growth in 2024

Discover 13 types of digital marketing your brand needs to stay ahead of the competition and drive growth.