• Hello,

    I’m trying to add fetchpriority="high" to my featured image using the following code:

    add_filter( 'wp_get_attachment_image_attributes', function ( $attr ) {
        if (
            isset( $attr['class'] ) &&
            strpos( $attr['class'], 'wp-post-image' ) !== false
        ) {
            $attr['fetchpriority'] = 'high';
            $attr['loading'] = 'eager';
            $attr['data-test'] = 'YES';
        }
        return $attr;
    }, 999 );

    The featured image correctly receives:

    loading="eager"
    data-test="YES"

    but fetchpriority="high" is removed from the final HTML.

    I also tested with a custom attribute named fetchpriority-test, and it is removed as well, while other custom attributes are preserved.

    I have already contacted Astra Pro support and WP Rocket support, and neither seems to be the source of the issue.

    Is WordPress sanitizing or overriding fetchpriority somewhere after wp_get_attachment_image_attributes runs? What is the correct way to force fetchpriority="high" on a featured image?

    Thank you.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Post content is sanitized and validated for security purposes. WP maintains an array of allowed HTML, anything else is stripped out. You can modify what’s allowed via the ‘wp_kses_allowed_html’ filter. Examine a dump of the passed array so you can add the desired attribute to the correct place in the array.

    I cannot say for sure that modifying the array will solve your specific problem, but this has been the solution for various similar issues.

    Hi @webec,

    Have you received any feedback from Astra Pro or WP Rocket support regarding this issue? If so, could you please share their findings or recommendations?

    Maybe you can try the following functions:

    https://developer.wordpress.org/reference/functions/wp_get_loading_optimization_attributes/

    https://developer.wordpress.org/reference/functions/the_post_thumbnail/

    WordPress manages fetchpriority through its loading optimization system, so these functions should be a good place to start when testing fetchpriority=”high” on a featured image.

    Thread Starter webec

    (@webec)

    @bcworkz thanks, I’ll check it and tell you if I can do something with ‘wp_kses_allowed_html’ filter.

    @mbigul Astra told me I could add fetchpriority to my features image as I was already doing but not working. WP-Rocket told me to install the Disable Critical Images Optimization plugin (https://docs.wp-rocket.me/article/1816-optimize-critical-images#how-to-deactivate-this-feature) to make ‘wp_get_attachment_image_attributes’ function work, but nothing changed.

    I performed additional tests.
    Custom attributes such as data-test, data-pepito, and data-fetchpriority are preserved correctly in the final HTML.
    The loading=”eager” attribute is also preserved.
    However, both fetchpriority=”high” and decoding=”async” are removed from the final HTML when added through wp_get_attachment_image_attributes().
    This makes me think that WordPress is not simply filtering unknown attributes, but that fetchpriority and decoding are being specifically recalculated or overridden later by WordPress image optimization functions.

    Could this be related to wp_get_loading_optimization_attributes() or another image optimization routine introduced in recent WordPress versions?

    • This reply was modified 3 weeks, 3 days ago by webec.
    Moderator bcworkz

    (@bcworkz)

    I briefly reviewed the source code for wp_get_loading_optimization_attributes() and do not see anything that would remove an existing fetchpriority attribute. It might complain if you try something odd like lazy load a high priority image. In some circumstances it may add fetchpriority if it doesn’t already exist. Nothing about removal though. Admittedly I may have missed something, it was a brief review.

    I added your filter code to my test site. For me, the fetchpriority attribute is added and exists in the post’s output. My test site is running WP 7.0 with TwentyTwentyOne theme. Thus I have to believe the removal in your case is due to your theme or one of your plugins. Core WP is apparently not responsible for the behavior.

Viewing 4 replies - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.