Adding Category/Tag/Taxonomy Support to Images/Media

Here’s how I recently added a custom taxonomy to the media library as a sortable column:

// Add a new column
add_filter('manage_media_columns', 'add_topic_column');
function add_topic_column($posts_columns) {
    $posts_columns['att_topic'] = _x('Topic', 'column name');
    return $posts_columns;
}

// Register the column as sortable
function topic_column_register_sortable( $columns ) {
    $columns['att_topic'] = 'att_topic';
    return $columns;
}
add_filter( 'manage_upload_sortable_columns', 'topic_column_register_sortable' );

add_action('manage_media_custom_column', 'manage_attachment_topic_column', 10, 2);
function manage_attachment_topic_column($column_name, $id) {
    switch($column_name) {
    case 'att_topic':
        $tagparent = "upload.php?";
        $tags = wp_get_object_terms( $id, 'taxonomy_name', '' );
        if ( !empty( $tags ) ) {
            $out = array();
            foreach ( $tags as $c )
                $out[] = "<a href="".$tagparent."tag=$c->slug"> " . esc_html(sanitize_term_field('name'
                         , $c->name, $c->term_id, 'post_tag', 'display')) . "</a>";
            echo join( ', ', $out );
        } else {
            _e('No Topics');
        }
        break;
    default:
        break;
    }
}

Leave a Comment