Skip to content
blutz edited this page Dec 31, 2012 · 3 revisions

About

This plugin is branched from Scott Bressler's media-credit plugin for WordPress. Quite a few changes were made to make this plugin work with WordPress 3.5 so I can use it on my own sites.

I'll be updating this as frequently as I need for it to work on my site and probably won't be adding any features to it unless they're needed for compatibility with a version of WordPress.

Upgrade

If you're upgrading from Scott's plugin (version 1.1.2 or earlier) you will probably run into problems. The easiest way to fix these are to insert all your photos again using this version.

Since I don't intend to support this plugin I wouldn't recommend upgrading to this version unless you don't have any alternative.

Install

The major change made for this plugin is also a major problem with this plugin. Instead of having opening and closing shortcodes (like [media-credit ...]...[/media-credit]) this version only uses one tag (a "self-closing" tag) like [media-credit ...] in the caption.

Unfortunately if you're seeing [media-credit ...] in the frontend you need to make a change to your WordPress install or your theme. There are two options:

Option 1: Change WordPress core files

This is the easiest option but will break every time WordPress updates. Change lines 654-655 (the return/last line of img_caption_shortcode()) in wp-includes/media.php to return '<div ' . $id . 'class="wp-caption ' . esc_attr($align) . '" style="width: ' . (10 + (int) $width) . 'px">' . do_shortcode( $content ) . '<p class="wp-caption-text">' . do_shortcode( $caption ) . '</p></div>';

Option 2: Add a function to your theme's functions.php file

This will last across WordPress upgrades but not across theme changes. Add this code:

add_shortcode('caption', 'img_caption_shortcode_mediacredit');
function img_caption_shortcode_mediacredit($attr, $content = null) {
	// New-style shortcode with the caption inside the shortcode with the link and image tags.
	if ( ! isset( $attr['caption'] ) ) {
		if ( preg_match( '#((?:<a [^>]+>\s*)?<img [^>]+>(?:\s*</a>)?)(.*)#is', $content, $matches ) ) {
			$content = $matches[1];
			$attr['caption'] = trim( $matches[2] );
		}
	}

	// Allow plugins/themes to override the default caption template.
	$output = apply_filters('img_caption_shortcode', '', $attr, $content);
	if ( $output != '' )
		return $output;

	extract(shortcode_atts(array(
		'id'	=> '',
		'align'	=> 'alignnone',
		'width'	=> '',
		'caption' => ''
	), $attr));

	if ( 1 > (int) $width || empty($caption) )
		return $content;

	if ( $id ) $id = 'id="' . esc_attr($id) . '" ';
	
	return '<div ' . $id . 'class="wp-caption ' . esc_attr($align) . '" style="width: ' . (10 + (int) $width) . 'px">'
	. do_shortcode( $content ) . '<p class="wp-caption-text">' . do_shortcode( $caption ) . '</p></div>'; 
}