Make WordPress Core

Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#52842 closed defect (bug) (fixed)

No way to adjust attributes in wp_resource_hints()

Reported by: vanyukov's profile vanyukov Owned by: sergeybiryukov's profile SergeyBiryukov
Milestone: 5.8 Priority: normal
Severity: minor Version:
Component: Script Loader Keywords: has-patch
Focuses: docs Cc:


Not sure if this is a bug or an improvement.

In wp_resource_hints() we're checking for valid attributes:

if ( ! is_scalar( $value )
        || ( ! in_array( $attr, array( 'as', 'crossorigin', 'href', 'pr', 'rel', 'type' ), true ) && ! is_numeric( $attr ) )
) {


but there's no way to define those attributes, because the only ones that are being set are rel and href:

$atts['rel']  = $relation_type;
$atts['href'] = $url;

Can we add a filter to allow modifying all these attributes?

Change History (14)

#1 @desrosj
3 years ago

  • Keywords reporter-feedback added

@vanyukov Could you expand on your use case or what exactly you are seeing vs. what you are expecting?

Are you saying that URLs with the allowed attributes are not being displayed with these attributes? There are some tests that assert the pr, rel, as and href attributes are shown correctly, bot none for type or crossorigin.

I believe that it was an intentional decision to not allow this list to be modified. @swissspidy, @peterwilsoncc, or @ocean90 may be able to confirm.

Some history is found in #38121, #34292.

#2 @peterwilsoncc
3 years ago

It was a while ago so this is stretching the memory, I think the theory was to limit attributes to those defined in the spec.

If the URL is passed as an array that includes the other attributes, then you should be able to add them

Are you wishing to include other attributes in the link tag or having trouble setting those that are defined as permitted?

#3 @vanyukov
3 years ago


This can be closed. I was wrong. Sorry.

I assumed that $urls = apply_filters( 'wp_resource_hints', $urls, $relation_type ); accepted only a list of URLs, as per the parameter description @param array $urls URLs to print for resource hints..

Maybe adjust the param description, to show that it accepts not only the links, but also the attributes?

#4 @peterwilsoncc
3 years ago

  • Focuses docs added
  • Keywords reporter-feedback removed

Thanks for the reply.

You're right, the documentation could be improved so this ticket can stay open to do that. It looks like the docs weren't updated when the $url parameter was changed to accept attributes rather than just the URL.

@param array[] Array of resources to be hinted and their attributes {
	@item string $url URL to include in resource hints
	@item string $as  How the browser should treat the asset (CSS, JavaScript, etc)
	// etc, etc

#5 @desrosj
3 years ago

  • Milestone changed from Awaiting Review to 5.8

This ticket was mentioned in PR #1283 on WordPress/wordpress-develop by Rahmon.

3 years ago

  • Keywords has-patch added; needs-patch removed

This change tries to improve the documentation of $url param in the wp_resource_hints filter.

Trac ticket:

#7 @Rahmohn
3 years ago

Hi @vanyukov and @peterwilsoncc

I've tried to improve the documentation of the $url parameter. I would appreciate any feedback from you.

#8 @vanyukov
3 years ago

@Rahmohn ,

I would probably use the PHP Documentation Standards, and at least change string[]|array[] to string|array.

#9 @Rahmohn
3 years ago

Thanks for the feedback @vanyukov. I've updated the PR to:

* ...
* @param array  $urls Resources to be hinted {
*     String of URLs or array of resources and their attributes.
*     @type string $as          How the browser should treat the asset (CSS, JavaScript, etc).
*     @type string $crossorigin How the element handles crossorigin requests.
*     @type string $href        URL to include in resource hints.
*     @type float  $pr          Expected probability that the resource will be used.
*     @type string $rel         Relationship between the resource and the current document.
*     @type string $type        Type of the resource (text/html, text/css, etc).
* }

Last edited 3 years ago by Rahmohn (previous) (diff)

#10 @SergeyBiryukov
3 years ago

  • Owner set to SergeyBiryukov
  • Status changed from new to reviewing

#11 @SergeyBiryukov
3 years ago

  • Component changed from General to Script Loader

#12 @SergeyBiryukov
3 years ago

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

In 51048:

Docs: Improve documentation for the wp_resource_hints filter.

Clarify that as of WordPress 4.7, the $urls parameter can accept arrays of specific HTML attributes as its child elements, in addition to URLs.

Follow-up to [37920], [38826].

Props vanyukov, Rahmohn, desrosj, peterwilsoncc, SergeyBiryukov.
Fixes #52842.

#13 @SergeyBiryukov
3 years ago

Thanks for the PR!

For reference, just noting that even though rel is in the list of allowed attributes, it was not included in [51048], since it always has the value of $relation_type, based on the list of predefined values, and cannot really be changed via the filter.

For that reason, it was also not mentioned in the Attributes for Resource Hints in 4.7 dev note.

Note: See TracTickets for help on using tickets.