Make WordPress Core

Changeset 25444 for trunk


Ignore:
Timestamp:
09/14/2013 08:44:26 PM (13 years ago)
Author:
nacin
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/media.php

    r25128 r25444  
    641641                return $output;
    642642
    643         extract(shortcode_atts(array(
    644                 'id'    => '',
    645                 'align' => 'alignnone',
    646                 'width' => '',
     643        $atts = shortcode_atts( array(
     644                'id'      => '',
     645                'align'   => 'alignnone',
     646                'width'   => '',
    647647                'caption' => ''
    648         ), $attr, 'caption'));
    649 
    650         if ( 1 > (int) $width || empty($caption) )
     648        ), $attr, 'caption' );
     649
     650        $atts['width'] = (int) $atts['width'];
     651        if ( $atts['width'] < 1 || empty( $atts['caption'] ) )
    651652                return $content;
    652653
    653         if ( $id ) $id = 'id="' . esc_attr($id) . '" ';
    654 
    655         return '<div ' . $id . 'class="wp-caption ' . esc_attr($align) . '" style="width: ' . (10 + (int) $width) . 'px">'
    656         . do_shortcode( $content ) . '<p class="wp-caption-text">' . $caption . '</p></div>';
     654        if ( ! empty( $atts['id'] ) )
     655                $atts['id'] = 'id="' . esc_attr( $atts['id'] ) . '" ';
     656
     657        $caption_width = 10 + $atts['width'];
     658
     659        /**
     660         * Filter the width of an image's caption.
     661         *
     662         * By default, the caption is 10 pixels greater than the width of the image,
     663         * to prevent post content from running up against a floated image.
     664         *
     665         * @since 3.7.0
     666         *
     667         * @param int $caption_width Width in pixels. To remove this inline style, return zero.
     668         * @param array $atts {
     669         *     The attributes of the caption shortcode.
     670         *
     671         *     @type string 'id'      The ID of the div element for the caption.
     672         *     @type string 'align'   The class name that aligns the caption. Default 'alignnone'.
     673         *     @type int    'width'   The width of the image being captioned.
     674         *     @type string 'caption' The image's caption.
     675         * }
     676         * @param string $content The image element, possibly wrapped in a hyperlink.
     677         */
     678        $caption_width = apply_filters( 'img_caption_shortcode_width', $caption_width, $atts, $content );
     679
     680        $style = '';
     681        if ( $caption_width )
     682                $style = 'style="width: ' . (int) $caption_width . 'px" ';
     683
     684        return '<div ' . $atts['id'] . $style . 'class="wp-caption ' . esc_attr( $atts['align'] ) . '">'
     685        . do_shortcode( $content ) . '<p class="wp-caption-text">' . $atts['caption'] . '</p></div>';
    657686}
    658687
Note: See TracChangeset for help on using the changeset viewer.