[wp-trac] [WordPress Trac] #40547: Add a filter to get_the_post_thumbnail_url

WordPress Trac noreply at wordpress.org
Mon Apr 24 01:43:10 UTC 2017


#40547: Add a filter to get_the_post_thumbnail_url
-----------------------------+------------------------------
 Reporter:  ibenic           |       Owner:
     Type:  enhancement      |      Status:  new
 Priority:  normal           |   Milestone:  Awaiting Review
Component:  Post Thumbnails  |     Version:  trunk
 Severity:  normal           |  Resolution:
 Keywords:                   |     Focuses:
-----------------------------+------------------------------

Comment (by SergeyBiryukov):

 Hi @ibenic, welcome to WordPress Trac! Thanks for the ticket.

 Related: #23983 (suggests a filter for `get_post_thumbnail_id()`).

 > If a theme uses the function get_the_post_thumbnail_url, there is no way
 to return the custom url for the post thumbnail.

 This should be possible using `get_post_metadata` and
 `wp_get_attachment_image_src` as a workaround:
 {{{
 function wp40547_filter_post_thumbnail_id( $null, $post_id, $meta_key,
 $single ) {
         if ( '_thumbnail_id' !== $meta_key ) {
                 return $null;
         }

         // Set a non-empty $attachment_id for the passed $post_id. Doesn't
 have to be an existing
         // attachment, just a value you would check later in
 wp40547_filter_post_thumbnail_src().
         // $attachment_id = ...

         return $attachment_id;
 }
 add_filter( 'get_post_metadata', 'wp40547_filter_post_thumbnail_id', 10, 4
 );

 function wp40547_filter_post_thumbnail_src( $image, $attachment_id, $size,
 $icon ) {
         if ( 'post-thumbnail' !== $size ) {
                 return $image;
         }

         // Set the image URL for the passed $attachment_id.
         // $image[0] = ...

         return $image;
 }
 add_filter( 'wp_get_attachment_image_src',
 'wp40547_filter_post_thumbnail_src', 10, 4 );
 }}}

--
Ticket URL: <https://core.trac.wordpress.org/ticket/40547#comment:1>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list