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


