Make WordPress Core

Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#44859 closed enhancement (fixed)

Add filter to has_post_thumbnail() to override default thumbnail use

Reported by: rzen's profile rzen Owned by: sergeybiryukov's profile SergeyBiryukov
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)

44859.diff (752 bytes) - added by rzen 6 years ago.
44859.2.diff (987 bytes) - added by desrosj 6 years ago.

Download all attachments as: .zip

Change History (14)

@rzen
6 years ago

#1 @rzen
6 years ago

  • Keywords has-patch added; needs-patch removed

#2 @mukesh27
6 years ago

  • Keywords close added
  • Resolution set to wontfix
  • Status changed from new to closed

@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

#3 @rzen
6 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 @SergeyBiryukov
6 years ago

  • Milestone changed from Awaiting Review to 5.0
  • Owner set to SergeyBiryukov
  • Status changed from reopened to reviewing

#5 @mukesh27
6 years ago

@rzen sorry for my misunderstanding.

#6 @pento
6 years ago

  • Milestone changed from 5.0 to 5.1

#7 @pento
6 years ago

  • Milestone changed from 5.1 to Future Release

Bumping along with #23983.

@desrosj
6 years ago

#8 @desrosj
6 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 bool to 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 $post param left out some potential types and had a typo in the variable name.
  • Updated since to 5.1.0.

This ticket was mentioned in Slack in #core-media by desrosj. View the logs.


6 years ago

#10 @desrosj
6 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.

#11 @desrosj
6 years ago

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

In 44542:

Media: Introduce the has_post_thumbnail filter.

Props rzen, desrosj.
Fixes #44859.

#12 @desrosj
6 years ago

  • Keywords commit removed
Note: See TracTickets for help on using tickets.