#56180 closed feature request (fixed)
get_header_image(): may consider adding filter in 'get_header_image()'
Reported by: | hztyfoon | Owned by: | audrasjb |
---|---|---|---|
Milestone: | 6.1 | Priority: | normal |
Severity: | normal | Version: | 2.1 |
Component: | Themes | Keywords: | has-patch has-unit-tests has-testing-info commit needs-dev-note |
Focuses: | administration | Cc: |
Description
Hey everyone, 🙂
I think we can add a filter hook in 'header_image' function in 'wp-includes/theme.php' file.
Original Idea by: @audrasjb.
Attachments (1)
Change History (28)
This ticket was mentioned in PR #2958 on WordPress/wordpress-develop by hz-tyfoon.
2 years ago
#1
- Keywords has-patch added
#3
@
2 years ago
I just thought I should mention my good friend @manzurahammed as he helped me with the patch. :)
#6
@
2 years ago
- Milestone changed from Awaiting Review to 6.1
- Owner set to audrasjb
- Status changed from new to reviewing
#10
@
2 years ago
- Keywords needs-unit-tests added
Hi there!
Thanks for the ticket and patch.
The approach looks good to me. Can you please add a Unit test that covers the new filter header_image
?
#11
follow-up:
↓ 13
@
2 years ago
It looks like the proposed filter would affect header_image()
, but not get_header_image()
, which may be confusing.
Should this be a filter on the result of get_header_image()
instead? That way it would work for both functions.
See the get_header_image_tag
and get_header_video_url
filters for similar examples.
#12
@
2 years ago
I'm also not sure that "Displays" should be changed to "Prints" as part of this PR, it would be inconsistent with other similar descriptions:
* Displays localized stylesheet link element. * Displays the custom header text color in 3- or 6-digit hexadecimal form (minus the hash symbol). * Displays the image markup for a custom header image. * Displays header image URL. * Displays header video URL. * Displays background image path. * Displays background color value.
For some of these, "Prints" might indeed be more appropriate than "Displays", but I would suggest changing that in a separate ticket.
#13
in reply to:
↑ 11
@
2 years ago
Replying to SergeyBiryukov:
It looks like the proposed filter would affect
header_image()
, but notget_header_image()
, which may be confusing.
Should this be a filter on the result of
get_header_image()
instead? That way it would work for both functions.
Great point!
#15
@
2 years ago
I agree on both points @SergeyBiryukov - the filter should be in get_header_image()
instead, and the "Displays" > "Prints" change is outside the scope of this ticket and should be removed from the PR.
#16
@
2 years ago
- Keywords has-unit-tests has-testing-info added; needs-unit-tests needs-testing-info removed
Testing Instructions
Steps to Test
- Add the following code to
functions.php
:<?php add_filter( 'header_image', 'my_custom_header_image' ); function my_custom_header_image( $header_image ) { return 'http://example.org/my-custom-header-image.jpg'; } add_action( 'admin_notices', 'my_custom_header_image_notice' ); function my_custom_header_image_notice() { echo '<div class="notice notice-info"><p>'. get_header_image() . '</p></div>'; }
- Navigate to the Dashboard.
Expected Results
- A notice should appear containing the following:
http://example.org/my-custom-header-image.jpg
#17
@
2 years ago
- Summary changed from header_image(): may consider adding filter in 'header_image()' to get_header_image(): may consider adding filter in 'get_header_image()'
⬆️ changing the summery from 'header_image' to 'get_header_image'.
Thanks a ton @SergeyBiryukov for those 2 points. 👍
I also agree with both of them. Will try to update the PR accordingly.
Also, thanks @costdev. I've took a look at your .diff
& you've also added unit_test there. great work.
hz-tyfoon commented on PR #2958:
2 years ago
#18
Just updated the PR.
removed the filter from header_image
& added a filter in get_header_image
.
Looks like it may make more sense to add the filter to get_header_image
function as this way it'll work for both header_image
& get_header_image
(as inside header_image()
there's only get_header_image()
and then just printing the image url ).
Thanks @SergeyBiryukov for pointing this out.
#20
@
2 years ago
Thanks for updating the PR but it looks like it doesn't include the Unit Tests proposed by @costdev in 56180.1.diff
. It would be useful if you can update your PR to implement those unit tests, but if you can't, it's probably better to close the PR in favor of 56180.1.diff
:)
#21
@
2 years ago
Ok, closed the PR in favor of 56180.1.diff
by @costdev as it already includes unit tests.
Just a minor change suggestion: Wouldn't the hook_name get_header_image
make more sense than header_image
?
#22
@
2 years ago
Indeed, so it's more consistent with the other get_header_image_tag_attributes
filter.
PR incoming.
This ticket was mentioned in PR #2999 on WordPress/wordpress-develop by audrasjb.
2 years ago
#23
Trac ticket: https://core.trac.wordpress.org/ticket/56180
#24
@
2 years ago
- Keywords commit added; needs-testing removed
Tested using the following snippet, works fine:
add_filter( 'get_header_image', function() { return 'https://example.com/image.jpg'; // Replace this with the image of your choice. } );
Adding needs-dev-note
keyword.
Marking for commit.
2 years ago
#27
Committed in https://core.trac.wordpress.org/changeset/53741
Trac ticket: 56180