Filter caption-shortcode in WordPress

For many reason, there are a lot of blogger using WordPress as they publishing platform. Free, easy to use and a lot of extend available on the net. WordPress Theme, WordPress Plugin with many licensed available.


On this post, I will show you some tricks. This tricks is replace default caption-shortcode on wordpress and enhanced with much better caption. Default wordpress caption-shortcode added extra style margin each caption. This extra code is good if you are using standard style design. But for many wordpress designer, need extra style for caption, that why you’ll need this code.Open your functions.php in your current theme and write caption-shortcode reloaded code

add_shortcode('wp_caption', 'fixed_img_caption_shortcode');
add_shortcode('caption', 'fixed_img_caption_shortcode');
function fixed_img_caption_shortcode($attr, $content = null) {
	// Allow <a title="plugins" href="">plugins</a>/themes to override the default caption template.
	$output = apply_filters('img_caption_shortcode', '', $attr, $content);
	if ( $output != '' ) return $output;
		'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: ' . ((int) $width) . 'px">'
	. do_shortcode( $content ) . '<p class="wp-caption-text">'
	. $caption . '</p></div>';

And it’s done, you can try this modified caption-shortcode by edit or view you post contain wp-caption. And viola… you got better caption shortcode right? This post original writen by some member. This caption-shortcode filter also published on WP Tricks

Subscribe to Full RSS Feed

RSS FeedIf you thing this article useful, please consider subscribing to our RSS Feed or e-mail updates and stay updated with us. You can also follow @JauhariNET on twitter for latest updates.

About Jauhari

This is me Nurudin Jauhari. Just human like you, I come from Gunungkidul, small place at Yogyakarta, and Today I live at Malang I was born 30/04/1980 in Ponjong, small village in Gunungkidul, Almost my time in 1 - 15 Years stay at this Village. More

This entry was posted in WordPress Code and tagged , , , .
Bookmark the permalink.

4 Responses to Filter caption-shortcode in WordPress

  1. Federico González Brizzio says:

    Hi, your code fixed my problem with the 10px @images!

    Thanks from Ushuaia, Argentina

  2. Pingback: Resize Images using Shortcode with TimThumb |

  3. Pingback: Nurudin Jauhari

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>