[wp-trac] [WordPress Trac] #44859: Add filter to has_post_thumbnail() to override default thumbnail use

WordPress Trac noreply at wordpress.org
Tue Aug 28 20:48:01 UTC 2018


#44859: Add filter to has_post_thumbnail() to override default thumbnail use
-----------------------------+-----------------------------
 Reporter:  rzen             |      Owner:  (none)
     Type:  enhancement      |     Status:  new
 Priority:  normal           |  Milestone:  Awaiting Review
Component:  Post Thumbnails  |    Version:
 Severity:  normal           |   Keywords:  needs-patch
  Focuses:                   |
-----------------------------+-----------------------------
 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()`.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/44859>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list