Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've been trying to add category, tag, or custom taxonomy support to images (or all media, though I'm only concerned with Images). I've got it part-way figured out with:

add_action('init', 'create_image_taxonomies');

function create_image_taxonomies() {
$labels = array(
    'name' => 'Media Category'
);

$args = array(
    'labels' => $labels,
    'public' => true
);

register_taxonomy('imagetype', 'attachment', $args);
}

This properly adds a Media Category field to the media screen. I've also confirmed this can be accessed with get_the_terms($my_attachment_id, 'imagetype').

Where I'm running into trouble is getting this information to show anywhere in the admin/dashboard except when viewing the media directly -- I want it either in a sub-menu or as a custom column, or both, as can be done for Posts and Pages.

I've tried using manage_posts_custom_column along with manage_edit-attachment_columns, but nothing at all is showing up. I've tried to use add_media_page to display something like the page automatically generated for Page and Post categories, but here I'm having trouble pulling the images to which I've given categories. You can see both attempts here: http://pastebin.com/S8KYTKRM

Thanks in advance for any assistance!

share|improve this question
add comment

1 Answer

up vote 8 down vote accepted

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;
    }
}
share|improve this answer
    
Thank you! The filter and action were the pieces I was missing. –  Roxanne Ready Oct 3 '11 at 21:26
    
To make each item in the column clickable, I added an <a> tag to $out with: href="upload.php?imagetype='.$tag->slug.'". –  Roxanne Ready Oct 5 '11 at 0:00
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.