Make WordPress Core

Ticket #24990: img_caption_array_filter.diff

File img_caption_array_filter.diff, 1.5 KB (added by pputzer, 10 years ago)

An attempt at a generic filter for matching the caption attribute and the media tag.

  • media.php

    old new  
    859859function img_caption_shortcode( $attr, $content = null ) {
    860860        // New-style shortcode with the caption inside the shortcode with the link and image tags.
    861861        if ( ! isset( $attr['caption'] ) ) {
    862                 if ( preg_match( '#((?:<a [^>]+>\s*)?<img [^>]+>(?:\s*</a>)?)(.*)#is', $content, $matches ) ) {
     862                $matches = array();
     863                $regex = '#((?:<a [^>]+>\s*)?<img [^>]+>(?:\s*</a>)?)(.*)#is';
     864                preg_match( $regex, $content, $matches );
     865               
     866                /**
     867                 * Filter the content and caption for use in the caption shortcode.
     868                 *
     869                 * If the filtered array `$matches` is empty, an old-style caption attribute
     870                 * will be assumed.
     871                 *
     872                 * @since 4.4.0
     873                 *
     874                 * @param array $matches        The default result of `preg_match`.
     875                 * @param string $content       The content included in the caption shortcode (including the image).
     876                 * @param string $regex         The regular expression capturing the image and the caption attribute.
     877                 *
     878                 * @return array $matches {
     879                 *              Array corresponding to the result of `preg_match`.
     880                 *
     881                 *              @type string 1  The new `$content` used in the shortcode (i.e. the `<img>` tag).
     882                 *              @type string 2  The caption (set as `$attr['caption']`.
     883                 * }
     884                 */
     885                $matches = apply_filters( 'img_caption_shortcode_content', $matches, $content, $regex );
     886                               
     887                if ( ! empty ( $matches ) ) {
    863888                        $content = $matches[1];
    864889                        $attr['caption'] = trim( $matches[2] );
    865890                }