Forked from warwickandersonza/facetwp-optimize-loading-strategy.php
Created
June 12, 2024 19:18
-
-
Save dexit/730c85c9bf7896e287ee7ccd2823c80c to your computer and use it in GitHub Desktop.
Optimize FacetWP preloaded image strategy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Applies eager loading to the first three (3) posts in the "recipe_list" FacetWP listing. | |
* | |
* @param array $output | |
* @param array $params | |
* | |
* @return array | |
*/ | |
function sitecare_facetwp_image_loading_strategy( array $output, array $params ): array { | |
$template_name = $params['template'] ?? null; | |
$template_content = $output['template'] ?? null; | |
if ( 'recipe_list' !== $template_name ) { | |
return $output; | |
} | |
$processor = new WP_HTML_Tag_Processor( $template_content ); | |
for ( $i = 0; $i < 3; $i++ ) { | |
if ( $processor->next_tag( 'img' ) ) { | |
$processor->set_attribute( 'loading', 'eager' ); | |
$processor->set_attribute( 'fetchpriority', 'high' ); | |
$processor->set_attribute( 'decoding', 'async' ); | |
} | |
} | |
$output['template'] = $processor->get_updated_html(); | |
return $output; | |
} | |
add_filter( 'facetwp_render_output', 'between_carpools_facetwp_image_loading_processor', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment