#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: |
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)
Change History (41)
#3
@
9 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
@
9 years ago
- Keywords has-patch added
Made a simple patch for this, which allows you to use it like so:
<?php the_thumbnail_url(); ?>
#6
in reply to:
↑ 5
@
9 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:
↓ 8
@
9 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
@
9 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 itspost_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:
↓ 10
@
9 years ago
I like the patch but I think you should use "return" the URL not "echo" it.
#10
in reply to:
↑ 9
@
9 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
@
9 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.
@
9 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.
#13
follow-up:
↓ 14
@
9 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()
andthe_post_thumbnail_url()
. - Adds them in the correct file (
post-thumbnail-template.php
) - Adds docblocks
#14
in reply to:
↑ 13
;
follow-up:
↓ 15
@
9 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()
andthe_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
@
9 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
@
9 years ago
- Keywords needs-unit-tests added
#17
@
9 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:
↓ 19
@
9 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.
#19
in reply to:
↑ 18
@
9 years ago
Replying to obenland:
It should probably be named
get_the_post_thumbnail_url()
to be in line withget_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.
#20
@
9 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.
#21
@
9 years ago
- Owner set to wonderboymusic
- Resolution set to fixed
- Status changed from new to closed
In 34373:
#22
@
9 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
@
9 years ago
Also, there is a var_dump()
in the newly introduced tests that should be removed. Attaching a patch.
#24
follow-up:
↓ 25
@
9 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
@
9 years ago
Replying to pathartl:
Shouldn't
get_the_post_thumbnail_url()
returnfalse
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 );
#28
@
9 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
@
9 years ago
- Owner changed from wonderboymusic to johnbillion
- Status changed from reopened to accepted
You could use
wp_get_attachment_url( get_post_thumbnail_id( $post_id ) )
for example. Isn't that easy enough?