• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Tips and Tricks HQ

  • Home
  • Blog
  • Projects
    • All Projects
    • Simple WP Shopping Cart
    • WP Express Checkout Plugin
    • WP Download Monitor
    • WP Security and Firewall Plugin
    • WP eStore Plugin
    • WP Affiliate Platform
    • WP eMember
    • WP Lightbox Ultimate
    • WP Photo Seller
  • Products
    • All Products
    • Checkout
  • Support
    • Support Portal
    • Customer Only Forum
    • WP eStore Documentation
    • WP Affiliate Software Documentation
    • WP eMember Documentation
  • Contact

Adding a Custom Column to the Users Table in WordPress

You are here: Home / Web Development / Adding a Custom Column to the Users Table in WordPress

Last updated: September 2, 2014





In this tutorial I will explain how you can add a custom column to the users table in WordPress. I will go through an example of adding a custom field in the WP users table to display all post statuses and types at a glance.

I used to find myself often in the “Screen Options” area of the users table interface, unchecking some fields I didn’t want, plus trying to hunt down plugins to add fields that I did want.

These days though, I play with the functions.php file like she’s my best friend. Let me share some of my work in hopes to make your life simpler as well, even if just a little bit.

The WordPress Users Table Can Be Expanded on to Show More Post Related Data for Multi-author Sites

Let’s take the users table in the WordPress admin for example. Many people are running multi-author sites. For me… the default table data isn’t sufficient. I mean, it’s a good start, but sometimes I want to work with something that is a little more customized.



A lot of times I add unique data to the “profile edit” page and I want to expose that data in the users table. Other times, it is existing data that I want to pull in and see at a glance.

I have exposed information such as last logged in date and time, whether the user has added AdSense to their account for revenue sharing, among other things. Today though, I want to discuss something that I think just about anyone will find useful.

On one WordPress site that I administer we have the need to know what the writers are up to in a sense. We would like to see, at a glance, how many posts they have matching each status.

Let’s Go Through How to Display Post Related Data for Users for Any Post Type and Any Status

The code I offer below will work for any status, and any post type. So, even if you use custom post types and custom statuses (for example, if using the Edit Flow WordPress plugin), this will work for you.

So, rather than having a column for “Posts” with just one number in it, we can have one with several values. See the image below to get an idea of what I am referring to.

custom-column-in-wp-users-list-table

I also want to make it very simple to access a list of each of the posts. Fortunately that is very simple, and it just requires passing an author ID and post status to an existing WordPress admin URL so that we can filter by those criteria.

Now, how do we do it?

Rather than Remove Columns on an Admin by Admin Basis, Let’s Get Rid of the Default Columns with the WordPress “unset” Command

First, I want to remove the default posts column from the Users table. We could use the “Screen Options” area but lets make it a little more clean and “unset” it from the functions.php file. This way, if there are other admins, it will just “work” and we won’t have to explain to them the process of removing that column. They will simply login and the changes will be made, as if like magic.

Here’s the code below that you can add to the Theme Functions file:

add_action('manage_users_columns','kjl_modify_user_columns');
function kjl_modify_user_columns($column_headers) {
  unset($column_headers['posts']);
  return $column_headers;
}

That’s it. Now, when you visit the Users area (/wp-admin/users.php) the default “Posts” column will be gonzo.

Oh, while we are at it, if you agree that the roles column is just taking up valuable real estate, you can “unset” that too by adding the following line to the above function. Just drop it before or after the posts unset command.

unset($column_headers['role']);

Adding New Columns to the User Table Is Just as Simple as Removing Them

Alright.. the next step is to add a new column. At first it won’t have any data, but we will get to that.

The column name will be unique but we can still title it “Posts”.

Let’s just add a line to the previous function. Put it just before the “return” statement. Here’s the line to add:

$columns['num_posts'] = 'Posts';

Here’s the complete code with the optional removal of the “role” column.

add_action('manage_users_columns','kjl_modify_user_columns');
function kjl_modify_user_columns($column_headers) {
  unset($column_headers['posts']);
  unset($column_headers['role']);
  $column_headers['num_posts'] = 'Posts';
  return $column_headers;
}

With CSS You Can Modify the New Column Width – Here’s How

Sometimes WordPress will make the column too wide unnecessarily. It may not matter… it will depend a lot on how many columns you will be working with. If you find it needs to be smaller, then just add some CSS to the admin.

You can use the code below for that, if, when you are done… you feel the column width needs adjusting.

add_action('admin_head', 'kjl_custom_admin_css');
function kjl_custom_admin_css() {
  echo '<style>
  .column-num_posts {width: 8%}
  </style>';
}

Just modify the percentage to suit your needs.

We Are Now Ready to Create a Function to Query the WordPress Database and Return the Post Count That We Are Looking for

At this stage we can write a function that will return the post count based on post status, post type, and author id.

Here’s the code, short and sweet:

function kjl_count_user_posts_by_type_and_status( $userid, $post_status, $post_type = "post" ) {
  global $wpdb;
  $where = "WHERE post_author = " . $userid . " AND post_type = '" . $post_type . "' AND (post_status = '" . $post_status . "')";
  $count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" );
  return apply_filters( 'get_usernumposts', $count, $userid );
}

This sexy bit of code returns a number based on the 3 parameters given. Post type will default to “post” if no value is given. So, if you aren’t working with custom post types then this will save you .3 seconds of your time.

Filling the New Custom Column with Relevant Data

Now, we want to add the code that will display the results that we are after in the new custom “Posts” column. This code will call the previously created function as many times as we need it to.

Here you go:

add_action('manage_users_custom_column', 'kjl_user_posts_count_column_content', 10, 3);
function kjl_user_posts_count_column_content($value, $column_name, $user_id) {
  $user = get_userdata( $user_id );
  if ( 'num_posts' == $column_name ) {
    $drafts = kjl_count_user_posts_by_type_and_status( $user_id, 'draft', 'post' );
    $published = kjl_count_user_posts_by_type_and_status( $user_id, 'publish', 'post' );
    $pending = kjl_count_user_posts_by_type_and_status( $user_id, 'pending', 'post' );
    $all = $drafts + $published + $pending;
    return 'All: <a href="/wp-admin/edit.php?author='.$user_id.'" target="_blank">'.$all.'</a>
    Draft: <a href="/wp-admin/edit.php?post_status=draft&amp;post_type=post&amp;author='.$user_id.'" target="_blank">'.$drafts.'</a>
    Pending: <a href="/wp-admin/edit.php?post_status=pending&amp;post_type=post&amp;author='.$user_id.'" target="_blank">'.$pending.'</a>
    Published: <a href="/wp-admin/edit.php?post_status=publish&amp;post_type=post&amp;author='.$user_id.'" target="_blank">'.$published.'</a>';
  }
  return $value;
}

That’s all there is to it. You can add more to it if you have other statuses you want to display. Just make sure you create a variable to store the value, add that value to “all” and then echo that value on it’s own like shown in the HTML that is “returned” above.

I hope that you find this useful.

Related Posts

  • WordPress Action Hooks and Filter Hooks – An Introduction
  • How to Create a WordPress plugin
  • A Brief Introduction to WordPress Nonces
  • Inserting an Audio Player in Your Theme’s Template File

Web Development,  Wordpress wordpress developer tutorial,  WordPress Tweaks

Reader Interactions

Comments (4 responses)

  1. admin says:
    September 1, 2018 at 11:54 pm

    @Patricia, It should continue to work in the future.

  2. Patricia says:
    September 1, 2018 at 11:52 pm

    Does this impact future wordpress updates?

  3. John says:
    March 29, 2016 at 7:38 pm

    Helped me out big time! Thanks for the post!

  4. Tim H says:
    December 29, 2015 at 6:39 am

    I find it very useful. Thank you 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Featured & Popular Articles

Video Answers to Top WordPress QuestionsWordPress Optimization Tips and Tricks for Better Performance and SpeedEssential WordPress Security Tips - Is Your Blog Protected?WordPress Simple PayPal Shopping Cart PluginTop 15 Search Engine Optimization (SEO) Techniques I Forget to DoList of the Best and Must Use WordPress PluginsHow do I Start a Blog and Make Money Online?Good Domain Name Picking Tips for Your Blog SetupFind Out Which WordPress Web Hosting Company Offers the Cheapest and Reliable Web Hosting Solution

Featured WordPress Plugins

wordpress estore plugin
wordpress membership plugin
WP Express Checkout Plugin
WordPress Lightbox Ultimate Plugin
WordPress photo seller plugin
wordpress affiliate plugin

Recent Posts

  • Accept Donations via PayPal from Your WordPress Site Easil [...]
  • Buy Now Button Graphics for eCommerce Websites [...]
  • Subscription Button Graphics for eCommerce Websites [...]
  • Adding PayPal Payment Buttons to Your WordPress Sidebar Ea [...]
  • PayPal QR Codes [...]

Comment & Socialize

  • @Rodrigo Souza, Thank you f ...
    - admin
  • The example for 'slm_add_ed ...
    - Rodrigo Souza
  • @Ron, All the valid transac ...
    - admin
  • Hello, when people have sel ...
    - Ron
  • We have hte following featu ...
    - admin

Check out our solutions

View our WordPress plugin collection and start using them on your site.

Our WordPress Solutions

Footer

Company

  • About
  • Privacy Policy
  • Terms and Conditions
  • Affiliate Login

Top WordPress Plugins

  • Simple Shopping Cart
  • PayPal Donations
  • WP Express Checkout
  • WP eStore
  • WP eMember

Blogging Tips

  • How to Start a Blog
  • Selecting a Good Domain
  • Cheap WP Hosting
  • WP Video Tutorials
  • Simple SEO Tips

Search


Keep In Touch

Copyright © 2023 | Tips and Tricks HQ