diff --git a/src/wp-includes/post-thumbnail-template.php b/src/wp-includes/post-thumbnail-template.php index f07302df3a..a024b1c00b 100644 --- a/src/wp-includes/post-thumbnail-template.php +++ b/src/wp-includes/post-thumbnail-template.php @@ -170,3 +170,30 @@ function get_the_post_thumbnail( $post = null, $size = 'post-thumbnail', $attr = */ return apply_filters( 'post_thumbnail_html', $html, $post->ID, $post_thumbnail_id, $size, $attr ); } + +/** + * Return the post thumbnail URL. + * + * @since 4.4.0 + * + * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`. + * @param string|array $size Optional. Registered image size to retrieve the source for or a flat + * array of height and width dimensions. Default 'post-thumbnail'. + * @return string Post thumbnail URL or empty string. + */ +function get_the_post_thumbnail_url( $post = null, $size = 'post-thumbnail' ) { + $image = wp_get_attachment_image_url( get_post_thumbnail_id( $post ), $size ); + return isset( $image ) ? $image : ''; +} + +/** + * Display the post thumbnail URL. + * + * @since 4.4.0 + * + * @param string|array $size Optional. Registered image size to retrieve the source for or a flat + * array of height and width dimensions. Default 'post-thumbnail'. + */ +function the_post_thumbnail_url( $size = 'post-thumbnail' ) { + echo get_the_post_thumbnail_url( null, $size ); +} diff --git a/tests/phpunit/tests/post/thumbnails.php b/tests/phpunit/tests/post/thumbnails.php index ed212fad3d..f503383132 100644 --- a/tests/phpunit/tests/post/thumbnails.php +++ b/tests/phpunit/tests/post/thumbnails.php @@ -110,4 +110,50 @@ class Tests_Post_Thumbnail_Template extends WP_UnitTestCase { $this->assertEquals( $expected, $actual ); } + + /** + * @ticket 33070 + */ + function test_get_the_post_thumbnail_url() { + $this->assertEquals( '', get_the_post_thumbnail_url() ); + $this->assertEquals( '', get_the_post_thumbnail_url( $this->post ) ); + + set_post_thumbnail( $this->post, $this->attachment_id ); + + var_dump( get_the_post_thumbnail_url( $this->post ) ); + + $this->assertEquals( '', get_the_post_thumbnail_url() ); + $this->assertEquals( wp_get_attachment_url( $this->attachment_id ), get_the_post_thumbnail_url( $this->post ) ); + + $GLOBALS['post'] = $this->post; + + $this->assertEquals( wp_get_attachment_url( $this->attachment_id ), get_the_post_thumbnail_url() ); + } + + /** + * @ticket 33070 + */ + function test_the_post_thumbnail_url() { + $GLOBALS['post'] = $this->post; + + ob_start(); + the_post_thumbnail_url(); + $actual = ob_get_clean(); + + $this->assertEmpty( $actual ); + + ob_start(); + the_post_thumbnail_url(); + $actual = ob_get_clean(); + + $this->assertEmpty( $actual ); + + set_post_thumbnail( $this->post, $this->attachment_id ); + + ob_start(); + the_post_thumbnail_url(); + $actual = ob_get_clean(); + + $this->assertEquals( wp_get_attachment_url( $this->attachment_id ), $actual ); + } }