Cart totals
Subtotal | $850.00 |
---|---|
Shipping |
Shipping options will be updated during checkout. |
Total | $858.50 |
// 1. Defer selected scripts by handle (safe implementation)
function safe_defer_scripts($tag, $handle, $src) {
$defer_list = ['navigation-js', 'filters-js']; // Adjust these handles to your own
if (in_array($handle, $defer_list)) {
return str_replace(' src', ' defer src', $tag);
}
return $tag;
}
add_filter('script_loader_tag', 'safe_defer_scripts', 10, 3);
// 2. Preload Google Fonts safely via WordPress hooks
function preload_google_fonts_resource_hints($urls, $relation_type) {
if ('preconnect' === $relation_type) {
$urls[] = [
'href' => 'https://fonts.googleapis.com',
'crossorigin' => true,
];
$urls[] = [
'href' => 'https://fonts.gstatic.com',
'crossorigin' => true,
];
}
return $urls;
}
add_filter('wp_resource_hints', 'preload_google_fonts_resource_hints', 10, 2);
// 3. Enqueue the main stylesheet (non-blocking)
function enqueue_async_main_style() {
wp_enqueue_style(
'main-style',
get_stylesheet_uri(),
[],
wp_get_theme()->get('Version')
);
}
add_action('wp_enqueue_scripts', 'enqueue_async_main_style');
// 4. Native lazy loading is built into WordPress 5.5+
// Use this fallback ONLY if absolutely needed for older content:
function force_lazy_loading_on_images($content) {
$pattern = '/]*loading=)([^>]+?)>/i';
$replacement = '
';
return preg_replace($pattern, $replacement, $content);
}
// Uncomment the next line to activate fallback lazy loading:
// add_filter('the_content', 'force_lazy_loading_on_images');