WordPress.org

Make WordPress Core

Opened 8 months ago

Closed 8 months ago

#38633 closed defect (bug) (fixed)

get_header_image_tag() doesn't always return the correct image

Reported by: bradyvercher Owned by: joemcgill
Milestone: 4.7 Priority: normal
Severity: normal Version:
Component: Themes Keywords: has-patch commit
Focuses: Cc:

Description

get_header_image_tag() relies on get_custom_header() to retrieve the URL for the current header image, however get_custom_header() doesn't always return the correct URL.

Most of the header image tags rely on get_header_image(), which uses the header_image theme mod. That theme mod should always contain the direct URL to an image, remove-header, random-default-image, or random-uploaded-image.

get_custom_header() uses the header_image_data theme mod, which will be empty when the header_image theme mod has remove-header, random-default-image, or random-uploaded-image. When that's the case, it will return the URL for the default image instead.

I'm not sure why get_custom_header() relies on a different theme mod or why the behavior is different. The URL it returns doesn't appear to be used anywhere else in core. At the very least, get_header_image_tag() should be updated to use get_header_image() for the image URL.

Attachments (1)

38663.diff (423 bytes) - added by bradyvercher 8 months ago.

Download all attachments as: .zip

Change History (6)

@bradyvercher
8 months ago

#1 @bradyvercher
8 months ago

  • Keywords has-patch added

Whoops, looks like I botched that patch number! 38663.diff uses get_header_image() for the image URL in get_header_image_tag() to make sure remove-header, random-default-image, and random-uploaded-image settings are respected.

This ticket was mentioned in Slack in #core-themes by bradyvercher. View the logs.


8 months ago

#3 @celloexpressions
8 months ago

  • Keywords commit added
  • Milestone changed from Awaiting Review to 4.7
  • Owner set to joemcgill
  • Status changed from new to reviewing

Good catch, looks good to me.

#4 @joemcgill
8 months ago

38663.diff Looks good. I believe we relied on the data from get_custom_header() in [35594] mainly due to performance concerns. The potential for the default image to be displayed was likely an oversight.

#5 @joemcgill
8 months ago

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

In 39121:

Themes: Improve reliability of URL used in get_header_image_tag().

get_header_image_tag() relies on the data returned by get_custom_header()
to build the attributes for the header <img> element. However,
get_custom_header() relies on data from the header_image_data theme
mod rather than the header_image theme mod that get_header_image() uses.
This can create inconsistencies when no header image is set, but a theme
supports a default header image.

This fixes the issue by making get_custom_image_tag() rely on the URL
returned by get_header_image() instead.

Props bradyvercher.
Fixes #38633.

Note: See TracTickets for help on using tickets.