From 031576d79eeb9de6ae0fb475b5d1b8cf8e5e32c6 Mon Sep 17 00:00:00 2001 From: Andrew Nacin Date: Sat, 14 Sep 2013 20:45:09 +0000 Subject: [PATCH] Introduce a img_caption_shortcode_width filter for controlling the inline style of the image caption shortcode. props iandunn for the initial patch. fixes #14380. Built from https://develop.svn.wordpress.org/trunk@25444 git-svn-id: http://core.svn.wordpress.org/trunk@25365 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/media.php | 47 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/wp-includes/media.php b/wp-includes/media.php index 8a41c79d2c..10b09d62ad 100644 --- a/wp-includes/media.php +++ b/wp-includes/media.php @@ -640,20 +640,49 @@ function img_caption_shortcode($attr, $content = null) { if ( $output != '' ) return $output; - extract(shortcode_atts(array( - 'id' => '', - 'align' => 'alignnone', - 'width' => '', + $atts = shortcode_atts( array( + 'id' => '', + 'align' => 'alignnone', + 'width' => '', 'caption' => '' - ), $attr, 'caption')); + ), $attr, 'caption' ); - if ( 1 > (int) $width || empty($caption) ) + $atts['width'] = (int) $atts['width']; + if ( $atts['width'] < 1 || empty( $atts['caption'] ) ) return $content; - if ( $id ) $id = 'id="' . esc_attr($id) . '" '; + if ( ! empty( $atts['id'] ) ) + $atts['id'] = 'id="' . esc_attr( $atts['id'] ) . '" '; - return '
' - . do_shortcode( $content ) . '

' . $caption . '

'; + $caption_width = 10 + $atts['width']; + + /** + * Filter the width of an image's caption. + * + * By default, the caption is 10 pixels greater than the width of the image, + * to prevent post content from running up against a floated image. + * + * @since 3.7.0 + * + * @param int $caption_width Width in pixels. To remove this inline style, return zero. + * @param array $atts { + * The attributes of the caption shortcode. + * + * @type string 'id' The ID of the div element for the caption. + * @type string 'align' The class name that aligns the caption. Default 'alignnone'. + * @type int 'width' The width of the image being captioned. + * @type string 'caption' The image's caption. + * } + * @param string $content The image element, possibly wrapped in a hyperlink. + */ + $caption_width = apply_filters( 'img_caption_shortcode_width', $caption_width, $atts, $content ); + + $style = ''; + if ( $caption_width ) + $style = 'style="width: ' . (int) $caption_width . 'px" '; + + return '
' + . do_shortcode( $content ) . '

' . $atts['caption'] . '

'; } add_shortcode('gallery', 'gallery_shortcode');