//1. Aggiungi fetchpriority="high" al logo del sito add_filter('get_custom_logo','custom_logo_add_fetchpriority_high',10,2);function custom_logo_add_fetchpriority_high($html,$blog_id){return str_replace('<img','<img fetchpriority="high"',$html)}//2. Aggiungi fetchpriority="high" alla prima <img>nel contenuto add_filter('the_content','add_fetchpriority_to_first_image');function add_fetchpriority_to_first_image($content){if (is_front_page() || is_home()){$pattern='/<img(.*?)>/i';$replacement='<img$1 fetchpriority="high">';$content=preg_replace($pattern,$replacement,$content,1)}return $content;}//3 & 4. Aggiunge preload alle immagini di background e agli slider Elementor add_action('wp_footer','add_fetchpriority_js_elementor_enhanced',20);function add_fetchpriority_js_elementor_enhanced(){if (is_admin()) return;?><script>document.addEventListener('DOMContentLoaded',function (){let found=false;//3. Primo background-image Elementor (container,section,etc.) const bgElems=document.querySelectorAll('[data-element_type][style*=background-image]');for (let el of bgElems){const style=getComputedStyle(el);const match=style.backgroundImage.match(/url\("(.*?)"\)/);if (match && match[1]){preloadImage(match[1]);found=true;break}}//4. Primo <img>visibile in uno slider/carousel if (!found){const sliderImgs=document.querySelectorAll('.elementor-swiper-button, .swiper-wrapper img, .elementor-image-carousel img');for (let img of sliderImgs){if (img.tagName==='IMG' && img.src){preloadImage(img.src);break}}}function preloadImage(url){const preload=document.createElement('link');preload.rel='preload';preload.as='image';preload.href=url;preload.fetchPriority='high';document.head.appendChild(preload)}});</script><?php}