Opened 8 years ago
Last modified 6 years ago
#38557 accepted defect (bug)
Format for registering a default header image is ambiguous
Reported by: | bradyvercher | Owned by: | joemcgill |
---|---|---|---|
Milestone: | Future Release | Priority: | normal |
Severity: | normal | Version: | |
Component: | Themes | Keywords: | has-patch 2nd-opinion |
Focuses: | Cc: |
Description
When registering a custom header with a default image, most resources seem to recommend using an absolute URL (Handbook, Codex, Make post). However, it can be registered with sprintf
placeholders, which is what the old HEADER_IMAGE
constant accepted. It's registered that way in Twenty Thirteen as well.
Any time core references the default image URL, it needs to account for the replacements. The problem is it doesn't take this into consideration in at least a couple places and obscures the replacement a bit by passing the default string through get_theme_mod()
in some cases, which handles the replacement itself.
Here are a couple of places where the replacement isn't handled in core:
To see how this manifests itself:
- Switch to Twenty Ten or Twenty Thirteen
- Delete the
header_image
andheader_image_data
theme mods - Access the Customizer
- Open the Header Image section
In either theme, the default image that was visible on the front end is missing in the Customizer preview and the "Current header" control doesn't show an image as being set.
If you access the old Custom Header screen in the admin panel (wp-admin/themes.php?page=custom-header), the option to reset the image is visible when it shouldn't be.
I imagine this also affects default images for the custom background feature as well.
Attachments (5)
Change History (20)
#2
@
8 years ago
38557.1.diff removes a debugging change from the last patch that shouldn't have been included.
#3
@
8 years ago
Default arguments are merged late, so 38557.2.diff adds a check to make sure default-image
exists before doing the replacements to prevent notices in themes that don't define a default header image.
#4
@
8 years ago
- Owner set to pento
- Resolution set to fixed
- Status changed from new to closed
In 39056:
#5
@
8 years ago
- Resolution fixed deleted
- Status changed from closed to reopened
Re-opening, I typoed the ticket number.
#7
@
8 years ago
I made a note on #38553 also, as I think this shouldn't be required now:
I don't think this should have be required now I committed https://core.trac.wordpress.org/changeset/39061 (which I think should probably have been in https://core.trac.wordpress.org/ticket/38586 anyway). Does that sound right to you?
This ticket was mentioned in Slack in #core-restapi by joehoyle. View the logs.
8 years ago
#9
@
8 years ago
38557.3.diff moves the replacement logic to get_theme_support()
, which has a few advantages:
- Automatically works for the default image in the custom background feature, too
- Is easier to unit test since
add_theme_support( 'custom-header' )
can't be tested - Doesn't modify the format of data stored in the
$_wp_theme_features
global
The two things that could potentially be considered breaking changes:
- Calling
get_theme_support( 'current-header', 'default-image' )
will return the full URL instead of potentially returning a string withsprintf
placeholders. I think this is a bug fix since core expects it to return a full URL in a couple of places. - The data stored in
$_wp_default_headers
is modified on assignment to consolidate the replacement logic for registered default headers. If the global shouldn't be changed, introducing a helper function to retrieve the values would be possible.
This ticket was mentioned in Slack in #core-themes by bradyvercher. View the logs.
8 years ago
#11
@
8 years ago
- Keywords 2nd-opinion added
I appreciate trying to reduce redundancy and the make the logic more clear here, but I'm not sure the best approach is to transform the format of the default-image
for custom headers when the setting is registered without at least doing some research into how themes are using the registered data.
Seeing how the purpose of get_theme_support()
(based on the function's description) is to get "theme support arguments passed when registering that support", I also don't think we should be doing any data transformations within that function.
As you mentioned, get_theme_mod()
handles the sprintf
transformation when you pass in a default, but that only helps when the theme mod isn't set. We also seem to process defaults in other places before setting the theme mod (e.g., reset_header_image()
) I agree that one option might be to add a helper function for retrieving the default image URL to consolidate the logic here. In the mean time, I think we should add the sprintf
logic to the customizer, which is where the this ambiguity has lead to bugs.
#12
@
8 years ago
38557.4.diff fixes the two places in core where we weren't accounting for get_theme_support()
returning a string with sprintf placeholders.
This ticket was mentioned in Slack in #core-themes by joemcgill. View the logs.
8 years ago
#15
@
8 years ago
- Milestone changed from Awaiting Review to Future Release
- Owner changed from pento to joemcgill
- Status changed from reopened to accepted
Let's do 38557.4.diff for now since it addresses the bugginess and we can look into adding a helper function or making the stored data less ambiguous in a future release.
The easy fix would be to handle the replacements in the two files mentioned, but in 38557.diff I tried doing the replacements when registering default images instead. That should move a bunch of similar logic to a single location, remove duplicate code, and reduce ambiguity when calling
get_theme_support( 'custom-header', 'default-image' )
.If there's any external code that relies on
$_wp_default_headers
, it could potentially have trouble, but I'd expect that to be an extreme edge case. Otherwise, I can't think of any reason the replacements need to happen when reading the value instead of during assignment.