#44859 closed enhancement (fixed)
Add filter to has_post_thumbnail() to override default thumbnail use
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Milestone: | 5.1 | Priority: | normal |
| Severity: | normal | Version: | |
| Component: | Post Thumbnails | Keywords: | has-patch |
| Focuses: | Cc: |
Description
Similar to #23983, I have routinely run into issues with overriding standard thumbnail behavior because different plugins or themes are (properly) checking for has_post_thumbnail() before utilizing get_the_post_thumbnail() or get_the_post_thumbnail_url().
Adding a filter to get_post_thumbnail_id() does allow for a means of overriding this function indirectly (similar to how the filter in wp_get_attachment_image_scr() allows one to override get_the_post_thumbnail_url(), but it could also possibly lead to unintended outcomes.
For example, I want to override the post thumbnail output only when a post doesn't have an attached thumbnail (therefore, no thumbnail ID). If the theme is checking has_post_thumbnail() before calling the_post_thumbnail() nothing will be output because get_post_thumbnail_id() currently returns nothing and causes has_post_thumbnail() to present false.
Using the filter added in #23983, I can set an arbitrary, magic number (e.g. 1) to trigger a true response in has_post_thumbnail(). However, my own check in the function that filters post_thumbnail_html will also see a positive response from get_post_thumbnail_id(). This means my filter function will bail (because it sees the post does have a custom image set) or I need an extra condition to confirm the returned value does not match the magic number I used while overriding it previously.
This would work, but I think the code can be clearer and more expressive if its possible to override has_post_thumbnail() independently of overriding get_the_thumbnail_id().
Attachments (2)
Change History (14)
#3
@
7 years ago
- Keywords close removed
- Resolution wontfix deleted
- Status changed from closed to reopened
Hi @mukesh27,
I think you are mistaken. There is nothing in trunk, nor any other trac tickets that I could find, that introduce a filter to the has_post_thumbnail() function. My patch was made fresh, straight from trunk, just yesterday. And your link points to has_post_thumbnail() as it currently exists (sans filter).
If I'm the one who is mistaken, though, please do correct me once again.
#4
@
7 years ago
- Milestone changed from Awaiting Review to 5.0
- Owner set to SergeyBiryukov
- Status changed from reopened to reviewing
#8
@
7 years ago
Thanks for this one, @rzen. I think a filter here is reasonable.
44859.2.diff has the following adjustments to 44859.diff:
- Correctly pass a
boolto the first parameter of the filter. - Include the thumbnail image ID as an optional third parameter for the filter. This could be used to not show a post thumbnail that has a specific meta property.
- Improve the inline documentation. Previously, the
$postparam left out some potential types and had a typo in the variable name. - Updated
sinceto5.1.0.
This ticket was mentioned in Slack in #core-media by desrosj. View the logs.
7 years ago
#10
@
7 years ago
- Keywords commit added
- Milestone changed from Future Release to 5.1
Discussed this in #core-media. This can go in and be followed up with #23983 when it's ready.
@rzen Above patch is already committed in trunk version check this - https://core.trac.wordpress.org/browser/trunk/src/wp-includes/post-thumbnail-template.php#L25