Skip to content

Instantly share code, notes, and snippets.

@jasonreposa
Created October 26, 2011 14:02
Show Gist options
  • Save jasonreposa/1316454 to your computer and use it in GitHub Desktop.
Save jasonreposa/1316454 to your computer and use it in GitHub Desktop.
A quick and painless way to remove the width from a wordpress caption and optionally add a source link as nofollow
// Originally built for http://www.mybanktracker.com/bank-news/
function mbt_img_caption_shortcode($val, $attr, $content = null) {
extract(shortcode_atts(array(
'id' => '',
'align' => 'alignnone',
'width' => '',
'caption' => ''
), $attr));
// if it contains a pipe character, we know it's ours
if (strpos($caption, '|') === FALSE) {
return;
}
$tokens = explode('|', $caption);
$caption = array_shift($tokens);
$href = implode('|', $tokens);
if (!empty($href)) {
$caption .= ' <a href="' . $href . '" rel="nofollow">source</a>';
}
if ( 1 > (int) $width || empty($caption) )
return $content;
if ( $id ) $id = 'id="' . esc_attr($id) . '" ';
return '<div ' . $id . 'class="wp-caption ' . esc_attr($align) . '">'
. do_shortcode( $content ) . '<p class="wp-caption-text">' . $caption . '</p></div>';
}
// what is 10, 3 for?
add_filter('img_caption_shortcode', 'mbt_img_caption_shortcode', 10, 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment