Home / Archive / Optimized & Safe NexSouk Performance Booster Snippet (with toggles off for embeds + block CSS)
Duplicate Snippet

Embed Snippet on Your Site

Optimized & Safe NexSouk Performance Booster Snippet (with toggles off for embeds + block CSS)

// NexSouk Performance Booster (Safe Version)
// PHP performance enhancements for:
// - Load speed
// - Core Web Vitals
// - Accessibility

Code Preview
php
<?php
// ===============================
// ✅ NexSouk Performance Booster (Safe Version)
// PHP performance enhancements for:
// - Load speed
// - Core Web Vitals
// - Accessibility
// ===============================
add_action('init', function() {
    // ✅ Disable Emoji Scripts (safe)
    remove_action('wp_head', 'print_emoji_detection_script', 7);
    remove_action('wp_print_styles', 'print_emoji_styles');
    // ⚠️ Optional: Disable oEmbed JavaScript (commented out for safety)
    // remove_action('rest_api_init', 'wp_oembed_register_route');
    // remove_filter('rest_pre_serve_request', '_oembed_rest_pre_serve_request', 10);
    // remove_filter('oembed_dataparse', 'wp_filter_oembed_result', 10);
    // remove_action('wp_head', 'wp_oembed_add_discovery_links');
    // remove_action('wp_head', 'wp_oembed_add_host_js');
});
// ✅ Remove Query Strings from Static Assets (safe)
add_filter('script_loader_src', 'nexsouk_strip_query_strings', 15, 1);
add_filter('style_loader_src', 'nexsouk_strip_query_strings', 15, 1);
function nexsouk_strip_query_strings($src) {
    return remove_query_arg('ver', $src);
}
// ⚠️ Optional: Remove Gutenberg Block Editor CSS (commented out)
add_action('wp_enqueue_scripts', function() {
    // wp_dequeue_style('wp-block-library');
    // wp_dequeue_style('wp-block-library-theme');
}, 100);
// ✅ Optimize Heartbeat API frequency (safer version)
add_filter('heartbeat_settings', function($settings) {
    $settings['interval'] = 30; // Less aggressive, still helpful
    return $settings;
});
// ✅ Set Lazy Loading for Images (safe)
add_filter('wp_lazy_loading_enabled', '__return_true');
// ✅ Add Width/Height to Images to Prevent CLS (safe)
add_filter('the_content', function($content) {
    return preg_replace_callback('/<img(.*?)src=["\']([^"\']+)["\'](.*?)>/i', function($matches) {
        $img_tag = $matches[0];
        $src = $matches[2];
        $size = @getimagesize($src);
        if ($size && isset($size[0]) && isset($size[1])) {
            if (strpos($img_tag, 'width=') === false && strpos($img_tag, 'height=') === false) {
                return str_replace('<img', '<img width="' . $size[0] . '" height="' . $size[1] . '"', $img_tag);
            }
        }
        return $img_tag;
    }, $content);
});
// ✅ Lazy Load All Images Except First (safe)
add_filter('the_content', function($content) {
    static $img_counter = 0;
    return preg_replace_callback('/<img(.*?)>/i', function($matches) use (&$img_counter) {
        $img_counter++;
        $img = $matches[0];
        if ($img_counter === 1) {
            return str_replace('<img', '<img decoding="async" fetchpriority="high"', $img);
        }
        return str_replace('<img', '<img loading="lazy" decoding="async"', $img);
    }, $content);
});
// ✅ Preload Featured Image in Head for Faster LCP (safe)
add_action('wp_head', function() {
    if (is_singular() && has_post_thumbnail()) {
        $image = wp_get_attachment_image_src(get_post_thumbnail_id(), 'full');
        if ($image && isset($image[0])) {
            echo '<link rel="preload" as="image" href="' . esc_url($image[0]) . '" fetchpriority="high">' . "\n";
        }
    }
});
// ✅ Preconnect to Google Fonts (safe)
add_action('wp_head', function() {
    echo '<link rel="preconnect" href="https://fonts.googleapis.com" crossorigin>' . "\n";
    echo '<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>' . "\n";
}, 1);

Comments

Add a Comment