Make WordPress Core

Changeset 34372


Ignore:
Timestamp:
09/22/2015 04:12:44 AM (9 years ago)
Author:
wonderboymusic
Message:

Media: Add a new function, wp_get_attachment_image_url(), which is a shortcut for wp_get_attachment_image_src() - same function signature, but returns just the URL based on $size.

Adds unit test.

Props dipesh.kakadiya, swissspidy, sebastian.pisula.
Fixes #33878.

Location:
trunk
Files:
2 edited

Legend:

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

    r34362 r34372  
    792792
    793793    return $html;
     794}
     795
     796/**
     797 * Get the URL of an image attachment.
     798 *
     799 * @since 4.4.0
     800 *
     801 * @param int          $attachment_id Image attachment ID.
     802 * @param string|array $size          Optional. Registered image size to retrieve the source for or a flat
     803 *                                    array of height and width dimensions. Default 'thumbnail'.
     804 * @param bool         $icon          Optional. Whether the image should be treated as an icon. Default false.
     805 * @return string|false Attachment URL or false if no image is available.
     806 */
     807function wp_get_attachment_image_url( $attachment_id, $size = 'thumbnail', $icon = false ) {
     808    $image = wp_get_attachment_image_src( $attachment_id, $size, $icon );
     809    return isset( $image['0'] ) ? $image['0'] : false;
    794810}
    795811
  • trunk/tests/phpunit/tests/media.php

    r33705 r34372  
    719719        remove_filter( 'embed_maybe_make_link', array( $this, 'filter_wp_embed_shortcode_custom' ), 10 );
    720720    }
     721
     722    /**
     723     * @ticket 33878
     724     */
     725    function test_wp_get_attachment_image_url() {
     726        $this->assertFalse( wp_get_attachment_image_url( 0 ) );
     727
     728        $post_id = $this->factory->post->create();
     729        $attachment_id = $this->factory->attachment->create_object( $this->img_name, $post_id, array(
     730            'post_mime_type' => 'image/jpeg',
     731            'post_type' => 'attachment',
     732        ) );
     733
     734        $image = wp_get_attachment_image_src( $attachment_id, 'thumbnail', false );
     735
     736        $this->assertEquals( $image[0], wp_get_attachment_image_url( $attachment_id ) );
     737    }
    721738}
Note: See TracChangeset for help on using the changeset viewer.