diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index 527e5dbc22..2507ce10d8 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -793,6 +793,22 @@ function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = fa return $html; } +/** + * Get the URL of an image attachment. + * + * @since 4.4.0 + * + * @param int $attachment_id Image attachment ID. + * @param string|array $size Optional. Registered image size to retrieve the source for or a flat + * array of height and width dimensions. Default 'thumbnail'. + * @param bool $icon Optional. Whether the image should be treated as an icon. Default false. + * @return string|false Attachment URL or false if no image is available. + */ +function wp_get_attachment_image_url( $attachment_id, $size = 'thumbnail', $icon = false ) { + $image = wp_get_attachment_image_src( $attachment_id, $size, $icon ); + return isset( $image['0'] ) ? $image['0'] : false; +} + /** * Adds a 'wp-post-image' class to post thumbnails. Internal use only. * diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index 34f12813e5..af1c2517b8 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -718,4 +718,21 @@ EOF; remove_filter( 'embed_maybe_make_link', array( $this, 'filter_wp_embed_shortcode_custom' ), 10 ); } + + /** + * @ticket 33878 + */ + function test_wp_get_attachment_image_url() { + $this->assertFalse( wp_get_attachment_image_url( 0 ) ); + + $post_id = $this->factory->post->create(); + $attachment_id = $this->factory->attachment->create_object( $this->img_name, $post_id, array( + 'post_mime_type' => 'image/jpeg', + 'post_type' => 'attachment', + ) ); + + $image = wp_get_attachment_image_src( $attachment_id, 'thumbnail', false ); + + $this->assertEquals( $image[0], wp_get_attachment_image_url( $attachment_id ) ); + } }