How To Filter caption-Shortcode in WordPress


Sponsored Links




WordPress 2.5 and up added new featured that called wp-caption, this is nice custom, you can add extra text on your image post, beside that tricks you can explore more with this featured. My bigg problem is the default wp-caption is added extra inline style. This inline style added extra padding on the left and right on your wp-caption. If you wp-caption style is using standard method of wordpress, you didn’t need this tricks.

But if you theme design need another style, like disable padding on your wp-caption, you’ll need it. Let’s explore more details.

On this wp tricks, we will modified default function on wp-caption and hook it with this modified caption. All need to do, is open functions.php on your theme and write this 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 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: ' . ((int) $width) . 'px">'
	. do_shortcode( $content ) . '<p class="wp-caption-text">'
	. $caption . '</p></div>';
}

And done, you can check it on your theme and the inline style is gone ;) This tricks I got it from WordPress.org forum, WP Engineer show different method. Do you have another solutions to do this? Share to me

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 @WPTricksNet on twitter for latest updates.

About WP Tricks

We are WordPress Family, we collected and writed useful tips and tricks for you WordPress. We share it for you...

Related Tips and Trick from Local Data and on The Net

Filter caption-Shortcode in WordPress - WP Engineer

Filter caption-Shortcode in WordPress. March 20th, 2009 by Frank • WordPress Hacks • 11 Comments. The title may not speak for itself and therefore I will explain Read More

Function Reference/add filter « WordPress Codex

Hooks a function to a specific filter action. Filters are the hooks that WordPress launches to The filter img_caption_shortcode is applied in media.php using the Read More

Shortcode API « WordPress Codex

function caption_shortcode( $atts, $content = null do_shortcode() is registered as a default filter on In the following instance WordPress may see the second Read More

WordPress › Support » Add new caption shortcode attribute

The * filter is 'img_caption_shortcode' and passes an empty string, the attr * parameter and Latest reply from jimisdon; This topic is resolved; WordPress version: 2.8 Read More

img_caption_shortcode Wordpress hook details -- Adam Brown, BYU

Detailed information about every action hook and filter used in WordPress. Makes Plugin API easier to use. Lists appearance, file location, and deprecation data for Read More


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>