WordPress.org

Make WordPress Core

Ticket #22075: 22075.diff

File 22075.diff, 2.2 KB (added by webord, 6 years ago)

post-template.php

  • wp-includes/post-template.php

     
    11381138 *
    11391139 * @since 2.5.0
    11401140 * @uses apply_filters() Calls 'wp_get_attachment_link' filter on HTML content with same parameters as function.
     1141 * @uses apply_filters() Calls 'wp_get_attachment_link_attributes' hook on attributes array
     1142 * @uses wp_parse_args() Merges the defaults with those defined by $args and allows for strings.
    11411143 *
    11421144 * @param int $id Optional. Post ID.
    11431145 * @param string $size Optional, default is 'thumbnail'. Size of image, either array or string.
    11441146 * @param bool $permalink Optional, default is false. Whether to add permalink to image.
    11451147 * @param bool $icon Optional, default is false. Whether to include icon.
    11461148 * @param string|bool $text Optional, default is false. If string, then will be link text.
     1149 * @param string|array $attr Optional, default is empty string.
    11471150 * @return string HTML content.
    11481151 */
    1149 function wp_get_attachment_link( $id = 0, $size = 'thumbnail', $permalink = false, $icon = false, $text = false ) {
     1152function wp_get_attachment_link( $id = 0, $size = 'thumbnail', $permalink = false, $icon = false, $text = false, $attr='' ) {
    11501153        $id = intval( $id );
    11511154        $_post = get_post( $id );
     1155        $attr_html = '';
    11521156
    11531157        if ( empty( $_post ) || ( 'attachment' != $_post->post_type ) || ! $url = wp_get_attachment_url( $_post->ID ) )
    11541158                return __( 'Missing Attachment' );
     
    11681172        if ( trim( $link_text ) == '' )
    11691173                $link_text = $_post->post_title;
    11701174
    1171         return apply_filters( 'wp_get_attachment_link', "<a href='$url' title='$post_title'>$link_text</a>", $id, $size, $permalink, $icon, $text );
     1175        $default_attr = array(
     1176                'href'  => $url,
     1177                'title' => $post_title,
     1178        );
     1179
     1180        $attr = wp_parse_args($attr, $default_attr);
     1181        $attr = apply_filters( 'wp_get_attachment_link_attributes', $attr, $_post );
     1182        $attr = array_map( 'esc_attr', $attr );
     1183
     1184        foreach ( $attr as $name => $value ) {
     1185                $attr_html .= " {$name}=\"{$value}\"";
     1186        }
     1187        return apply_filters( 'wp_get_attachment_link', "<a $attr_html>$link_text</a>", $id, $size, $permalink, $icon, $text );
    11721188}
    11731189
    11741190/**