WordPress.org

Make WordPress Core

Opened 7 weeks ago

Last modified 10 days ago

#52842 new defect (bug)

No way to adjust attributes in wp_resource_hints()

Reported by: vanyukov Owned by:
Milestone: 5.8 Priority: normal
Severity: minor Version:
Component: General Keywords: needs-patch
Focuses: docs Cc:

Description

Not sure if this is a bug or an improvement.

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

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

        continue;
}

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

<?php
$atts['rel']  = $relation_type;
$atts['href'] = $url;

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

Change History (5)

#1 @desrosj
13 days 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
13 days 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 https://core.trac.wordpress.org/browser/tags/5.7.1/src/wp-includes/general-template.php?marks=3300-3306#L3300

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

#3 @vanyukov
11 days ago

@desrosj,

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
11 days 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
10 days ago

  • Milestone changed from Awaiting Review to 5.8
Note: See TracTickets for help on using tickets.