WordPress.org

Ready to get started?Download WordPress

Forums

Add tag support to Media Library (3 posts)

  1. stoneskely
    Member
    Posted 7 months ago #

    I'd love to see tag support added to the Media Library so I can categorize my uploaded media for easy sorting in the Media Library (eg. Header Images, Social Media Images, Category Images, Holiday Images, Cats, etc.).

  2. keesiemeijer
    moderator
    Posted 7 months ago #

    try it with a plugin:
    http://wordpress.org/plugins/media-tags/
    http://wordpress.org/plugins/enhanced-media-library

    Or register a new taxonomy for the attachment post type.
    http://codex.wordpress.org/Function_Reference/register_taxonomy

    And put this in your theme's functions.php:

    // register a Tag taxonomy for attachments
    add_action( 'init', 'add_attachment_taxonomy' );
    
    function add_attachment_taxonomy() {
    	register_taxonomy( 'image_tags', 'attachment', array(
    			'hierarchical' => false,
    			'label'        => 'Tags',
    			'query_var'    => true,
    			'rewrite'      => true
    		) );
    }
    
    // add image_tags column to media library screen
    add_filter( 'manage_media_columns', 'add_image_tags_columns' );
    
    function add_image_tags_columns( $columns ) {
    	$columns['image_tags'] = __( 'Image Tags' );
    	return $columns;
    }
    
    // display terms in image_tags column
    add_action( 'manage_media_custom_column' , 'custom_columns', 10, 2 );
    
    function custom_columns( $column, $post_id ) {
    
    	if ( 'image_tags' == $column ) {
    		$terms = get_the_terms( $post_id, 'image_tags' );
    		$html = '';
    
    		if ( $terms && ! is_wp_error( $terms ) ) {
    
    			foreach ( $terms as $term ) {
    				$url = admin_url( 'edit-tags.php?action=edit&post_type=attachment&taxonomy=image_tags&tag_ID=' . $term->term_id . '' );
    				$html .= '<a href="' . esc_url( $url ) . '">' . $term->name . '</a>, ';
    			}
    
    		} else {
    			$html .= '—';
    		}
    
    		echo trim( $html, ', ' );
    	}
    }

    btw:
    consider creating a child theme instead of editing your theme directly - if you upgrade the theme all your modifications will be lost.

  3. stoneskely
    Member
    Posted 7 months ago #

    I've been looking at some plugins that do what I need, and the code looks cool (I may try integrating it into my child theme), but this functionality seems like it should be native to a basic WordPress installation.

Reply

You must log in to post.

About this Topic