A Guide on How to Add Custom Columns to the WordPress Users Dashboard

- Advertisement -


Adding custom columns to the WordPress Users Dashboard can greatly enhance the management of websites with a large number of registered users. Whether you want to display additional user data such as their website, social links, or Yoast SEO open graph fields, custom columns can make it easier to navigate and update user information. In this article, we will guide you through the process of adding custom columns without the need for any third-party plugins.

To begin, we need to utilize the core WordPress `manage_{$screen->id}_columns` filter. This filter allows us to register new columns to any admin screen, and in this case, we will focus on the “users” screen. By adding the following code to your site, a new “Website” column will be added to the users dashboard:

- Advertisement -

“`php
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’ );
“`

In the example provided, the code checks if the user’s “website” field is empty or not. You can add more items to the `$new_columns` array to create additional custom columns. The columns will be added to the end of the table by default. If you prefer to display your custom columns before the core WordPress columns, you can swap the variables in the `array_merge()` function.

- Advertisement -

Once you have added the custom columns, the next step is to populate them with data. This can be achieved by hooking into the `manage_users_custom_column` filter, which allows us to modify the output for any user dashboard column. In order to populate the custom “website” field, you can use the following code:

“`php
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 );
“`

- Advertisement -

After adding this code, you can refresh your users dashboard and view the assigned website in the custom website column. If you plan on adding multiple fields, it is recommended to use a switch statement to handle each column individually.

“`php
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 );
“`

In conclusion, adding custom columns to the WordPress Users Dashboard can greatly improve the management of websites with numerous registered users. The process is simple and does not require any third-party plugins. If you encounter any issues or have any questions, feel free to leave a comment. We would also love to hear about the custom columns you are adding for your WordPress users. Share your ideas and experiences with us!

- Advertisement -

Stay in Touch

spot_img

Related Articles