WordPress.org

Ready to get started?Download WordPress

Forums

WordPress Media Tags
Small enhancements (6 posts)

  1. unkulunkulu
    Member
    Posted 1 year ago #

    First of all, thanks for this plugin. It's simple enough to use easily and tweak as needed. I added small enhancements to make it work better for me and thought to share in case someone else needs these too.

    I changed the shortcode to return a string instead of echoing it. This allows the shortcode to work properly when inserted in the middle of content.

    I also added support for other file types besides images so that you can use it for Media Galleries. I hard coded the images for these file types, so you need to either change those to suit your needs or make a more robust solution for that.

    Here's the updated code in wp-media-tags.php:

    function media_tags_query($term, $size) {
    	$args = array(
    		'post_type' => 'attachment',
    		'post_mime_type' => 'image,application/postscript,application/pdf,application/msword',
    		'post_status' => 'inherit',
    		'tax_query' => array(
    				array(
    					'taxonomy' => 'media_tag',
    					'terms' => $term,
    					'field' => 'slug',
    				)
    			)
    	);
    	$loop = new WP_Query( $args );
    	$output = '';
    	while ( $loop->have_posts() ) : $loop->the_post();
    		$image = wp_get_attachment_image( '', $size, false );
    		$url = wp_get_attachment_url();
    		$mime = get_post_mime_type();
    
    		if ($image) {
    			$imgTag = $image;
    		} else {
    			switch ($mime) {
    				case 'application/postscript':
    					$thumb_image = 'attachment_eps.png';
    					break;
    				case 'application/pdf':
    					$thumb_image = 'attachment_pdf.png';
    					break;
    				case 'application/msword':
    					$thumb_image = 'attachment_doc.png';
    					break;
    				default:
    					$thumb_image = 'attachment_default.png';
    					break;
    			}
    			$imgTag = '<img src="/img/'.$thumb_image.'" alt="'.get_the_title().'" />';
    		}
    		$output .= '<div class="attachment"><a href="'.$url.'">'.$imgTag.'<br />'.get_the_title().'</a></div>';
    	endwhile;
    
    	return $output;
    }	
    
    function media_tags_shortcode($atts) {
    	extract( shortcode_atts( array(
    		'name' => '',
    		'size' => 'thumbnail',
    	), $atts ) );
    	return wp_media_tags_plugin::media_tags_query($name, $size);
    }

    http://wordpress.org/extend/plugins/wordpress-media-tags/

  2. unkulunkulu
    Member
    Posted 1 year ago #

    Also to add support for using multiple tags in the shortcode you could add this to the wp-media-tags.php:

    function media_tags_query($term, $size) {
    	if (strrpos($term, ',') !== false) {
    		$term = explode(',',$term);
    	}

    By default this lists images with any of the tags you've defined. To change this to list only images with ALL the tags you've defined, change the tax_query Array to following:

    'tax_query' => array(
    	array(
    		'taxonomy' => 'media_tag',
    		'terms' => $term,
    		'field' => 'slug',
    		'operator' => 'AND',
    	)
    )

    Notice the last line.

  3. furiousfrog
    Member
    Posted 1 year ago #

    You're a star! Thank you for this solution that adds a little more flexibility onto an already great plugin. Can't thank you both enough - will be installing this now!

  4. Philipp Speck
    Member
    Plugin Author

    Posted 1 year ago #

    Sorry folks! I'm here the first time. Answering all questions of all my plugins. I will try your code and yes it will be there in the next release, if it's useful. Thank you for your support! You are welcome!

  5. Philipp Speck
    Member
    Plugin Author

    Posted 1 year ago #

    To show mime type icons please do it by CSS3 for better control.

    a[href$=".pdf"] {
    	background: url("images/icon-pdf.png") no-repeat left;
    	padding-left: 20px;
    }
  6. Philipp Speck
    Member
    Plugin Author

    Posted 1 year ago #

    @unkulunkulu: Great! I have added you code for multiple tags in shortcode. Thank you for your support.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic