[wp-trac] [WordPress Trac] #54192: `get_header_image_tag` doesn't add `srcset`/`sizes`

WordPress Trac noreply at wordpress.org
Sun Oct 10 14:45:03 UTC 2021


#54192: `get_header_image_tag` doesn't add `srcset`/`sizes`
-------------------------------+------------------------------
 Reporter:  strarsis           |       Owner:  (none)
     Type:  defect (bug)       |      Status:  new
 Priority:  normal             |   Milestone:  Awaiting Review
Component:  General            |     Version:
 Severity:  normal             |  Resolution:
 Keywords:  reporter-feedback  |     Focuses:
-------------------------------+------------------------------

Comment (by strarsis):

 Currently I have to use the following code to add `srcset`/`sizes` to the
 generated custom header image:

 {{{#!php
 <?php
 function get_attachment_id_by_rel_url($urL) {
     $url_abs    = home_url($urL);
     $attachment = attachment_url_to_postid($url_abs);
     return $attachment;
 }

 add_filter('get_header_image_tag', function ($default_html, $header_obj,
 $attr) {
     $header_image_src = $attr['src'];
     $header_image_attachment_id =
 get_attachment_id_by_rel_url($header_image_src);

     $header_image_html = wp_get_attachment_image(
         $header_image_attachment_id,
         'header-image',
         false,
         $attr,
     );

     return $header_image_html;
 }, 5, 3);
 }}}


 Note that as the passed header object doesn't contain the actual custom
 image src (this may be a plugin issue and unrelated to this issue), I have
 to determine the attachment ID by using its URL.

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


More information about the wp-trac mailing list