Created
October 26, 2011 14:02
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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