Andrew-Biss.com logo


WordPress backend: Addding custom columns

on in Articles

Get a better overview of your posts, pages, media and comments

Wordpress media library custom columns screenshot
Adding custom columns to the WordPress media library list is a great way to spot missing information. The same technique is easily applied to other WordPress Admin lists.

Searching through your WordPress media library for items without “alt” text, caption or description can be time consuming and is error prone, especially if you’ve more than a few images to check. A quick scan could easily reveal what information is missing; if only these three columns were available. Well, as you’ll see, its easy to add them with two minor additions to your WordPress theme’s functions.php file.

Define your custom columns

First we need to tell WordPress the internal and external names of the custom columns we want to add to the media library.

add_filter('manage_media_columns','isv_custom_media_column_headings');

function isv_custom_media_column_headings($defaults) {
   $defaults['isv_alt']     = 'Alt';
   $defaults['isv_caption'] = 'Caption';
   $defaults['isv_desc']    = 'Description';
   return $defaults;
}

You can enable and disable your custom columns using the screen options panel at the top of the page in exactly the same way as for the standard columns.

Fill your custom columns with data

For each media item WordPress asks us what value to show in each of our custom columns. The ALT text comes from a post metadata field, whereas the caption is stored in the post excerpt and the description in the post content.

add_action('manage_media_custom_column','isv_custom_media_column_content',10,2);

function isv_custom_media_column_content($column_name,$id) {
   $none = '<span style="background-color:yellow; color:black;">None</span>';
   switch ($column_name) {
      case 'isv_alt':
         $alt = get_post_meta($id,'_wp_attachment_image_alt',true);
         echo $alt ? $alt : $none;
         break;
      case 'isv_caption':
         $caption = get_the_excerpt();
         echo $caption ? $caption : $none;
         break;
      case 'isv_desc':
         $desc = get_the_content();
         echo $desc ? $desc : $none;
         break;
   }
}

I highlight “none” with an inline CSS style so missing information really stands out.

And that’s all there is to it. Simple!

Add custom columns to other WordPress Admin lists

Use the exact same approach to add custom columns to other WordPress Admin lists. Here are the filter and action names you’ll need:

Posts
Filter: manage_posts_columns, Action: manage_posts_custom_column
Pages
Filter: manage_pages_columns, Action: manage_pages_custom_column
Comments
Filter: manage_edit-comments_columns, Action: manage_comments_custom_column

I’ve added custom columns to my post and page lists for the excerpt and featured image thumbnail. On my comments list I’ve added a custom column to highlight pingbacks and trackbacks so they stand out amongst the other comments.

 

Tags

Share

If you liked this Andrew-Biss.com post then please consider passing it along:

Updates

Subscribe to the full-text RSS feed or daily email newsletter to receive all new content from Andrew-Biss.com. For notifications please Follow on Twitter or Like on Facebook.

 

4 Comments and counting

  1. Tommy

    July 22nd 2011 at 06:52

    Very cool… I just created a custom tag for attachments (mainly images). I would like these tags to show on the media library columns.

      • Andrew Biss

        July 22nd 2011 at 10:31

        Glad to hear you got this working OK. The custom columns feature in WordPress Admin is great and I keep finding additional information that can be usefully added to the management lists.

  2. Sebastian

    August 22nd 2011 at 10:10

    Thank you for the code it worked.

    I am trying to do a gallery with sort by price as well as the sort by name asc desc.

    So I want to get a new field in the wp-admin/media.php file and also a column in the wp-admin/post-new.php Add an Image popup.

    So as well as sort order asc desc one also has sort by price.

    You wouldn’t have happened to have see anything that can point me in the way of how to do that.

Please add your comment