Make WordPress Core

Opened 7 years ago

Closed 19 months ago

Last modified 17 months ago

#38942 closed enhancement (fixed)

New filter for header image tag attributes

Reported by: junaidbhura's profile junaidbhura Owned by: johnjamesjacoby's profile johnjamesjacoby
Milestone: 5.9 Priority: normal
Severity: normal Version: 4.7
Component: Themes Keywords: has-patch has-dev-note
Focuses: template Cc:

Description

There is currently no way to filter the attributes of the header image tag before it is used to build the header image tag HTML in the get_header_image_tag() function.

I've attached a diff to solve this problem by adding a new 'get_header_image_tag_attributes' filter.

Attachments (2)

38942.diff (627 bytes) - added by junaidbhura 7 years ago.
38942.1.diff (640 bytes) - added by audrasjb 20 months ago.
Themes: Introduce get_header_image_tag_attributes hook to filter the list of header image attributes.

Download all attachments as: .zip

Change History (20)

@junaidbhura
7 years ago

#1 @junaidbhura
7 years ago

  • Keywords has-patch added

This ticket was mentioned in Slack in #core by junaidbhura. View the logs.


6 years ago

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


6 years ago

#4 follow-up: @obenland
6 years ago

I'm not sure a filter is really needed here since the function accepts an array of attributes that override any defaults. Am I missing something?

#5 in reply to: ↑ 4 @junaidbhura
6 years ago

Replying to obenland:

I'm not sure a filter is really needed here since the function accepts an array of attributes that override any defaults. Am I missing something?

The idea is to have this work similar to wp_get_attachment_image_attributes . There is currently no way to modify the header image's attributes like a normal attachment image.

There is a get_header_image_tag filter, but that is applied after the image's HTML has already been built.

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


6 years ago

This ticket was mentioned in Slack in #themereview by joyously. View the logs.


5 years ago

#8 @sabernhardt
3 years ago

Related/duplicate: #46134

#9 @sabernhardt
2 years ago

Related/duplicate: #34644

#10 @sabernhardt
20 months ago

#34644 was marked as a duplicate.

#11 @sabernhardt
20 months ago

  • Milestone changed from Awaiting Review to 5.9

With 34644.patch, @sebastian.pisula made another version of this filter to add a class or similar attribute. However, I like how 38942.diff includes the $header argument.

I tried these two implementations:

function edit_header_alt_with_url( $attr, $header ) {
	if ( '.jpg' === substr( $header->url, -4 ) ) {
		$attr['alt'] = 'new alt';
	}
	return $attr;
}
add_filter( 'get_header_image_tag_attributes', 'edit_header_alt_with_url', 10, 2 );

function header_image_add_class( $attr ) {
	if ( isset( $attr['class'] ) ) {
		$attr['class'] .= ' special classes';
	} else {
		$attr['class'] = 'special classes';
	}
	return $attr;
}
add_filter( 'get_header_image_tag_attributes', 'header_image_add_class', 10, 1 );

This ticket was mentioned in Slack in #core by chaion07. View the logs.


20 months ago

@audrasjb
20 months ago

Themes: Introduce get_header_image_tag_attributes hook to filter the list of header image attributes.

#13 @audrasjb
20 months ago

Patch refreshed against trunk + refreshed docblock

#14 @chaion07
20 months ago

  • Keywords commit added

Hi @junaidbhura! Thank you for reporting this. This ticket was discussed during a recent Bug-scrub session. Based on the feedback received we're adding the commit keyword. Having Unit Test would also be a good idea. Thanks!

Props to @costdev & @audrasjb

#15 @chaion07
20 months ago

  • Keywords needs-dev-note added

#16 @johnjamesjacoby
19 months ago

  • Owner set to johnjamesjacoby
  • Resolution set to fixed
  • Status changed from new to closed

In 51978:

Themes: Introduce get_header_image_tag_attributes hook.

This change adds a filter inside of the get_header_image_tag() function allowing developers to filter the attributes of the header image HTML tag before they are escaped, concatenated, and returned.

Before this change, it was not possible to externally and preemptively intercept this array of attributes. After this change, these attributes can now be easily filtered, matching it nicely to the wp_get_attachment_image_attributes hook.

Props audrasjb, chaion07, junaidbhura, sabernhardt.

Fixes #38942.

#17 @johnbillion
19 months ago

In 52034:

Docs: Fix some docblock syntax errors and add a missing canonical reference.

See #53399, #52867, #38942, #53668

#18 @sabernhardt
17 months ago

  • Keywords has-dev-note added; commit needs-dev-note removed
Note: See TracTickets for help on using tickets.