WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#33070 closed enhancement (fixed)

the_thumbnail_url or similar

Reported by: atomicjack Owned by: johnbillion
Milestone: 4.4 Priority: normal
Severity: minor Version:
Component: Post Thumbnails Keywords: needs-patch
Focuses: template Cc:
PR Number:

Description

At the moment, we can pull the thumbnail image with the_post_thumbnail, however, it's a little more tedious if all you want is the URL of the image.

Please could we have a function added to get the URL of the thumbnail image?

Something like:

the_thumbnail_url
the_post_thumbnail_url
thumbnail_url

or similar.

Attachments (8)

post-template.diff (659 bytes) - added by atomicjack 4 years ago.
Adds in the_thumbnail_url() - takes no parameters.
post-template-2.diff (496 bytes) - added by atomicjack 4 years ago.
Fixed an undefined variable of $post_id issue: removed the $post_id variable. Let me know if anyone feels this is needed here.
33070.diff (819 bytes) - added by swissspidy 4 years ago.
33070.2.diff (2.8 KB) - added by swissspidy 4 years ago.
33070.3.diff (2.8 KB) - added by swissspidy 4 years ago.
33878_33070.diff (4.8 KB) - added by dipesh.kakadiya 4 years ago.
wp_get_attachment_image_url #33878, the_post_thumbnail_url and get_the_post_thumbnail_url #33070 function added for get image url
33878_33070.1.diff (4.8 KB) - added by dipesh.kakadiya 4 years ago.
Patch Updated
33070.patch (562 bytes) - added by tyxla 4 years ago.
Removing unnecessary var_dump() in test_get_the_post_thumbnail_url()

Download all attachments as: .zip

Change History (41)

#1 @swissspidy
4 years ago

  • Component changed from Query to Post Thumbnails

#2 @swissspidy
4 years ago

You could use wp_get_attachment_url( get_post_thumbnail_id( $post_id ) ) for example. Isn't that easy enough?

#3 @atomicjack
4 years ago

That would be because, something such as:

                      <a href="<?php wp_get_attachment_url( get_post_thumbnail_id(); ?>" class="project-link" title="<?php the_title(); ?>" data-rel="prettyPhoto[gallery]">

Results in a parse error.

Surely since we have the_content, the_post_thumbnail, etc, a simple function to pull the thumbnail URL would be handy.

#4 @atomicjack
4 years ago

  • Keywords has-patch added

Made a simple patch for this, which allows you to use it like so:

<?php the_thumbnail_url(); ?>

@atomicjack
4 years ago

Adds in the_thumbnail_url() - takes no parameters.

#5 follow-up: @SergeyBiryukov
4 years ago

Previously: #17017

#6 in reply to: ↑ 5 @atomicjack
4 years ago

Replying to SergeyBiryukov:

Previously: #17017

@SergeyBiryukov even though that previous ticket has been marked as wontfix, due to other functions being present. Could we have this patch integrated, so that it matches the rest of the post template functions, and provides a shorthand version?

There's a fair few posts in the forums of people having no clue how to grab the URL of the thumbnail - and then when they're told about the current functions, they struggle to get it to work how they need. This patch calls literally the URL as easy as any other function.

#7 follow-up: @swissspidy
4 years ago

From the previous ticket:

I don't see the point of having to add another function just to get the URL when there is already a function to do that.

Agree with that one. 1 function call instead of 2 doesn't make a huge difference. I don't see the need for that additional function. But I'm open for other opinions.

Besides that, for your use case I recommend the_post_thumbnail and its post_thumbnail_html filter.

#8 in reply to: ↑ 7 @atomicjack
4 years ago

  • Severity changed from normal to minor

Replying to swissspidy:

From the previous ticket:

I don't see the point of having to add another function just to get the URL when there is already a function to do that.

Agree with that one. 1 function call instead of 2 doesn't make a huge difference. I don't see the need for that additional function. But I'm open for other opinions.

Besides that, for your use case I recommend the_post_thumbnail and its post_thumbnail_html filter.

It maintains a standard that new developers and users of WordPress can feel comfortable with.

If someone is dipping into designing a new theme or something, they're going to be using the_title, the_content, etc. Then it comes too wanting to grab the URL of the thumbnail... they search the forums, find the current functions being explained, but struggle to work it to how they desire, after all, they feel it is basic just to grab the URL of the image, and really, it should be.

It's a minor, minor change, that makes it easier for people coming on to WordPress, and also slightly reduces the amount of code and allows for more concise code.

#9 follow-up: @ramiy
4 years ago

I like the patch but I think you should use "return" the URL not "echo" it.

#10 in reply to: ↑ 9 @atomicjack
4 years ago

  • Keywords needs-refresh added

Replying to ramiy:

I like the patch but I think you should use "return" the URL not "echo" it.

Correct. I'll submit an updated patch later today.

#11 @atomicjack
4 years ago

  • Keywords dev-feedback added; needs-refresh removed

Actually it seems it does need to be echo rather than return. 'return' will pull a 404 for the image URL, whereas echo just gives the URL and all done.

@atomicjack
4 years ago

Fixed an undefined variable of $post_id issue: removed the $post_id variable. Let me know if anyone feels this is needed here.

#12 @atomicjack
4 years ago

@wonderboymusic do you have a view on this?

@swissspidy
4 years ago

#13 follow-up: @swissspidy
4 years ago

@atomicjack Next time please don't create a patch for your patch. Instead, update the original patch.

I'm still not really fond of the idea, but I created a new patch that does the following:

  • Adds two new functions, get_post_thumbnail_url() and the_post_thumbnail_url().
  • Adds them in the correct file (post-thumbnail-template.php)
  • Adds docblocks

#14 in reply to: ↑ 13 ; follow-up: @atomicjack
4 years ago

Replying to swissspidy:

@atomicjack Next time please don't create a patch for your patch. Instead, update the original patch.

I'm still not really fond of the idea, but I created a new patch that does the following:

  • Adds two new functions, get_post_thumbnail_url() and the_post_thumbnail_url().
  • Adds them in the correct file (post-thumbnail-template.php)
  • Adds docblocks

I actually don't know how to update a patch on here... I just followed what I've seen others do.

#15 in reply to: ↑ 14 @johnbillion
4 years ago

  • Keywords dev-feedback removed
  • Milestone changed from Awaiting Review to Future Release
  • Type changed from feature request to enhancement
  • Version 4.2.2 deleted

Replying to atomicjack:

I actually don't know how to update a patch on here... I just followed what I've seen others do.

You should just re-generate the patch and upload it again with the same file name. Trac will append a number to the end of the file if one of the same name already exists.

When you use Git to generate a patch, there's no need to commit the change and then generate a patch from that. See Scribu's guide to creating SVN-compatible patches from Git.

Regarding the actual functions you're suggesting, I think they do have value and they're kind of expected.

swissspidy's 33070.diff looks good. get_post_thumbnail_url() should add the following so a WP_Post object can be passed too:

$post_id = get_post( $post_id );

#16 @swissspidy
4 years ago

  • Keywords needs-unit-tests added

Gonna need some unit tests too, see #33723.

Edit: Actually, after #33723 $post_id = get_post( $post_id ); isn't needed here, because get_post_thumbnail_id would accept a WP_Post object. The docblock/params need to be changed to clarify this though.

Last edited 4 years ago by swissspidy (previous) (diff)

@swissspidy
4 years ago

#17 @swissspidy
4 years ago

  • Keywords needs-unit-tests removed
  • Milestone changed from Future Release to 4.4

Attached a new patch, now with unit tests too! I also added a $size parameter that defaults to 'post-thumbnail'. I think that's kinda expected. You never only want the full URL.

If #33878 gets in, the functions can be updated to use the proposed get_attachment_image_url() function.

#18 follow-up: @obenland
4 years ago

It should probably be named get_the_post_thumbnail_url() to be in line with get_the_post_thumbnail().

I personally like to keep the return value of a function to be a single type, so I wonder if it would make sense to return an empty string rather than false. It's not a big deal though.

@swissspidy
4 years ago

#19 in reply to: ↑ 18 @swissspidy
4 years ago

Replying to obenland:

It should probably be named get_the_post_thumbnail_url() to be in line with get_the_post_thumbnail().

I personally like to keep the return value of a function to be a single type, so I wonder if it would make sense to return an empty string rather than false. It's not a big deal though.

Seems legit. The new patch incorporates your suggestions.

@dipesh.kakadiya
4 years ago

wp_get_attachment_image_url #33878, the_post_thumbnail_url and get_the_post_thumbnail_url #33070 function added for get image url

#20 @swissspidy
4 years ago

I think we should still handle those two tickets separately, doing #33878 after this one.

@dipesh.kakadiya Do the tests pass in your patch? get_the_post_thumbnail_url looks broken to me at first glance.

@dipesh.kakadiya
4 years ago

Patch Updated

#21 @wonderboymusic
4 years ago

  • Owner set to wonderboymusic
  • Resolution set to fixed
  • Status changed from new to closed

In 34373:

Media: Add new functions, get_the_post_thumbnail_url() and the_post_thumbnail_url().

Adds unit tests.

Props dipesh.kakadiya, swissspidy, atomicjack.
Fixes #33070.

#22 @Frank Klein
4 years ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

I'd consider escaping the URL output by the_post_thumbnail_url() function with esc_url().

#23 @tyxla
4 years ago

Also, there is a var_dump() in the newly introduced tests that should be removed. Attaching a patch.

@tyxla
4 years ago

Removing unnecessary var_dump() in test_get_the_post_thumbnail_url()

#24 follow-up: @pathartl
4 years ago

Shouldn't get_the_post_thumbnail_url() return false if no URL is found? Returning '' seems backwards. Example precedent, get_the_ID() will return false if there is no valid $post object. the_post_thumbnail_url() would have to be changed also to test for false from get_the_post_thumbnail_url().

#25 in reply to: ↑ 24 @Frank Klein
4 years ago

Replying to pathartl:

Shouldn't get_the_post_thumbnail_url() return false if no URL is found?

Yes, especially because wp_get_attachment_image_url() returns false according to r34372.

So actually we wouldn't need the isset() check in get_the_post_thumbnail_url(), the function could be a one-liner:

return wp_get_attachment_image_url( get_post_thumbnail_id( $post ), $size ); 

#26 @pento
4 years ago

In 34468:

Tests: Remove a var_dump() added in [34373].

See #33070.

#27 @wonderboymusic
4 years ago

  • Resolution set to fixed
  • Status changed from reopened to closed

In 34480:

Post Thumbnails: In get_the_post_thumbnail_url(), return false instead of empty string when no URL is available.

Fixes #33070.

#28 @johnbillion
4 years ago

  • Keywords needs-patch added; has-patch removed
  • Resolution fixed deleted
  • Status changed from closed to reopened

This needs better logic and tests for when the post is invalid or the post has no thumbnail.

In addition, the_post_thumbnail_url() should use esc_url(), as Frank pointed out.

#29 @johnbillion
4 years ago

  • Owner changed from wonderboymusic to johnbillion
  • Status changed from reopened to accepted

#30 @johnbillion
4 years ago

Also, $size should be the first parameter in get_the_post_thumbnail_url().

Last edited 4 years ago by johnbillion (previous) (diff)

#31 @leewillis77
4 years ago

#11571 was marked as a duplicate.

#32 @johnbillion
4 years ago

  • Resolution set to fixed
  • Status changed from accepted to closed

In 34663:

Update get_the_post_thumbnail_url() so it returns false on failure, to bring it inline with wp_get_attachment_image_url(). Correct and introduce new tests.

Fixes #33070

#33 @johnbillion
4 years ago

In 34664:

Avoid using assertNotFalse() which is only available in phpunit >= 4.0.0

See #33070

Note: See TracTickets for help on using tickets.