Skip to content

Instantly share code, notes, and snippets.

@brianleejackson
Created December 8, 2023 03:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianleejackson/7a7a7a648e49fd665f6e1cb3a46c980d to your computer and use it in GitHub Desktop.
Save brianleejackson/7a7a7a648e49fd665f6e1cb3a46c980d to your computer and use it in GitHub Desktop.
Disable WordPress core fetch priority
function disable_fetchpriority_high( $loading_attrs ) {
unset( $loading_attrs['fetchpriority'] );
return $loading_attrs;
}
add_filter(
'wp_get_loading_optimization_attributes',
'disable_fetchpriority_high'
);
@Hasan101101
Copy link

I have put the code at the end of functions.php but seems this code is not working. Do you have any idea why it is not working?

@brianleejackson
Copy link
Author

@Hasan101101 Would probably have to take a look at your site. I can confirm the snippet works OK. We are using this on all of our sites. And I regularly use it on client's sites. Are you sure you don't have something else adding fetch? Products like Elementor now have an experimental feature that adds fetch (it also typically places it in on the wrong image), so I would make sure it's WordPress core. Happy to take a look if you want. Just send us a message here: https://perfmatters.io/contact/

@Hasan101101
Copy link

Hasan101101 commented Jan 31, 2024

@brianleejackson thanks for your quick response.

yes, the code is working after disabling the fetchpriority feature from the Elementor.

Actually, I was forced to remove the fetchpriority feature because the fetchpriority was getting applied on the wrong image. I couldn't find any reason. Do you know how to fix this issue? or do you have any resources?

@brianleejackson
Copy link
Author

brianleejackson commented Jan 31, 2024

@Hasan101101 You might be able to use this snippet. You can control the size of the image that WordPress core fetch is getting applied to.

add_filter( 'wp_min_priority_img_pixels', function( $size ) { return 1000000; } );

FYI, we have WordPress core fetch disabled on all of our sites. The way it was implanted into WordPress core is not great. That's also why we added fetch into our plugin. https://perfmatters.io/docs/fetch-priority/ This way you can target anything you want. Although I usually recommend using a preload over fetch. But it depends on the site.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment