Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save warwickandersonza/cb5bef7a8a8a45a36ec892e61227d8e7 to your computer and use it in GitHub Desktop.
Save warwickandersonza/cb5bef7a8a8a45a36ec892e61227d8e7 to your computer and use it in GitHub Desktop.
Optimize FacetWP preloaded image strategy
/**
* 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