WordPress.org

Support

Support » Plugins and Hacks » [Plugin: WordPress Media Tags] Small enhancements

[Plugin: WordPress Media Tags] Small enhancements

  • unkulunkulu
    Member

    @unkulunkulu

    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/

Viewing 5 replies - 1 through 5 (of 5 total)
  • unkulunkulu
    Member

    @unkulunkulu

    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.

    furiousfrog
    Member

    @furiousfrog

    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!

    Plugin Author Philipp Speck
    Member

    @destio

    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!

    Plugin Author Philipp Speck
    Member

    @destio

    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;
    }
    Plugin Author Philipp Speck
    Member

    @destio

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

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘[Plugin: WordPress Media Tags] Small enhancements’ is closed to new replies.