A lightweight WordPress plugin that automatically adds the fetchpriority="high" attribute to featured images to improve page loading performance and Core Web Vitals scores.
FetchPriority Featured Image is a simple plugin that helps improve your website's performance by adding the fetchpriority="high" attribute to featured images. This tells browsers to prioritize loading these important images, which can improve your Largest Contentful Paint (LCP) scores and overall user experience.
- Automatically adds
fetchpriority="high"to featured images - Intelligently applies the attribute only where it matters most:
- On single posts and pages
- On the first post of archive pages, blog home, and search results
- Zero configuration required - install and activate
- No settings page to keep things simple and lightweight
- Compatible with most WordPress themes
- Download the latest release from the GitHub repository
- Upload the plugin folder to the
/wp-content/plugins/directory of your WordPress installation - Activate 'FetchPriority Featured Image' from your Plugins page
- Clone the repository:
git clone https://github.com/gunjanjaswal/FetchPriority-Featured-Image.git - Upload the
fetchpriority-featured-imagefolder to the/wp-content/plugins/directory - Activate the plugin through the 'Plugins' menu in WordPress
No, this plugin only adds an HTML attribute to the image tag. It doesn't modify your actual image files or database entries.
This plugin should work with any theme that uses WordPress's standard featured image functions. If your theme uses custom code to display featured images, the plugin might not affect those images.
No, the plugin works automatically once activated. There are no settings to configure.
You can view the HTML source of your pages and look for fetchpriority="high" in the featured image HTML.
This plugin is licensed under the GPL v2 or later.
Created by Gunjan Jaswaal
The plugin uses WordPress's post_thumbnail_html filter to add the fetchpriority="high" attribute to the HTML of featured images. It intelligently applies this attribute only to:
- Featured images on single posts and pages
- The first post's featured image on archive pages, blog home, and search results
This targeted approach ensures that only the most important images get the high priority treatment, which is in line with best practices for using the fetchpriority attribute.
function fpfi_add_fetchpriority_to_featured_image( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
// Only add fetchpriority to featured images on single posts/pages or the first post on archive pages
if ( is_singular() || ( is_home() || is_archive() || is_search() ) && ! isset( $GLOBALS['fpfi_first_post_processed'] ) ) {
// Mark that we've processed the first post on archive pages
if ( ! is_singular() ) {
$GLOBALS['fpfi_first_post_processed'] = true;
}
// Add fetchpriority="high" attribute if it doesn't already exist
if ( strpos( $html, 'fetchpriority=' ) === false ) {
$html = str_replace( '<img ', '<img fetchpriority="high" ', $html );
}
}
return $html;
}Contributions are welcome! Feel free to:
- Fork the repository
- Create a feature branch:
git checkout -b my-new-feature - Commit your changes:
git commit -am 'Add some feature' - Push to the branch:
git push origin my-new-feature - Submit a pull request
If you find this plugin useful, consider buying me a coffee to support the development.
- Updated for WordPress 6.9 compatibility
- Improved security with nonce verification for AJAX calls
- Updated minimum PHP requirement to 7.4
- Enhanced code quality and WordPress coding standards compliance
- Added proper input sanitization and escaping
- Aligns with WordPress 6.9's frontend performance improvements
- Initial release
This project is licensed under the GPL v2 or later - see the LICENSE file for details.