Add Custom Columns to WordPress Dashboard Posts Lists Programmatically (No Plugin)

by | March 20, 2023 | PHP, Website Development

This page may contain affiliate links to recommended products or services. If you choose to purchase after clicking a link, I may receive a commission at no additional cost to you.

Ever wanted to add a custom column to your WordPress dashboard’s posts list page? How about adding a custom column to the WordPress dashboard’s posts list page programmatically without using a plugin? Yes, NO plugins. In this article, I’m going to show you how to add your own new custom columns to the WordPress dashboard posts list pages without using a plugin and you can use this method for any custom post type, too.

Essentially, it’s a 2-part process. Part 1 is literally adding the new column and giving it a column header. Part 2 is populating the column for each row (i.e. each post).

  • Create the column and assign a title
  • Populate the rows for each post

Code

Add the following code to your WordPress theme’s functions.php file.

/**
 * Add a new column to the dashboard's post list screen for the "post" post type.
 *
 * @param array $columns The existing columns for the post list screen.
 * @return array The modified columns with the new column added.
 */
function add_custom_column($columns) {
    $columns['has_featured_image'] = 'Featured Image';
    return $columns;
}
add_filter('manage_post_posts_columns', 'add_custom_column');

/**
 * Display the value "yes" or "no" in the new "Featured Image" column based on whether the "Featured Image" field has a value.
 *
 * @param string $column_name The name of the current column being displayed.
 * @param int $post_id The ID of the current post being displayed.
 */
function display_custom_column($column_name, $post_id) {
    if ($column_name == 'has_featured_image') {
        $has_post_thumbnail = get_the_post_thumbnail($post_id) ? 'yes' : 'no';
        echo $has_post_thumbnail;
    }
}
add_action('manage_post_posts_custom_column', 'display_custom_column', 10, 2);

Explanation

Naming the New Column

The first function creates a new column and provides a title for the column. The function add_custom_column() accepts $columns as an argument. $columns is an array of the existing columns, so manipulating the $columns array here affords you the opportunity to add or remove new columns. In this example, I created a column for “Featured Image” because I wanted an easy way to know whether or not the post had a featured image.

add_custom_column is a function name I made up. You may call it whatever you want, but if you use another name be sure to update the reference to it in the subsequent add_filter hook.

Assigning a Value

The second function is display_custom_column(). display_custom_column() is attached to the manage_[post-type]_posts_custom_column hook and is where you can modify the output of a specific column for each post. As arguments, it accepts the current column’s name and post id of the current row being displayed. If you wanted, you could return different values for specific posts, as well as for specific columns. However, for this example, I’ve decided to output a simple “yes” or “no” depending on whether or not the current post has a featured image.

Similar to before, display_custom_column() is a function name I made up. Again, you may call it whatever you’d like as long as you reference it the same way in the add_action hook it’s attached with.

Use Cases

As you’ve seen, it’s easy to manually create your own custom columns for the post list pages in the WordPress dashboard. My example helped create a simple way to know if a post does or doesn’t have a featured image, but you could create custom columns to visualize nearly any aspect of your WordPress post content. For instance, this technique pairs extremely well with Advanced Custom Fields (ACF) fields, so you can know which posts field data.

Floyd Hartford is a website developer from southern Maine. He's focused on creating and building WordPress websites and loves spending time digging into code like HTML, CSS, scss, jQuery, PHP, and MySQL.

0 Comments

Submit a Comment

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

seven − one =

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Looking for WordPress Help?

I’m a freelance web developer specializing in small business and corporate marketing websites.

Pin It on Pinterest