Home / Archive / NexSouk Performance & Accessibility Optimizer
Duplicate Snippet

Embed Snippet on Your Site

NexSouk Performance & Accessibility Optimizer

NexSouk.com Performance & Accessibility Optimizer

Code Preview
php
<?php
// ===============================
// ✅ NexSouk.com Performance & Accessibility Optimizer
// ===============================
add_action('init', function() {
    // ✅ Remove emoji scripts
    remove_action('wp_head', 'print_emoji_detection_script', 7);
    remove_action('wp_print_styles', 'print_emoji_styles');
    // ✅ Reduce Heartbeat API frequency (to lower backend resource usage)
    add_filter('heartbeat_settings', function($settings) {
        $settings['interval'] = 30;
        return $settings;
    });
});
// ✅ Remove query strings from static assets (improves caching)
function nexsouk_strip_query_strings($src) {
    return remove_query_arg('ver', $src);
}
add_filter('script_loader_src', 'nexsouk_strip_query_strings', 15, 1);
add_filter('style_loader_src', 'nexsouk_strip_query_strings', 15, 1);
// ✅ Add lazy loading to all images except the first one (boosts LCP)
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];
        // Add width/height to prevent layout shift
        if (preg_match('/src=["\']([^"\']+)["\']/', $img, $src_match)) {
            $src = $src_match[1];
            $size = @getimagesize($src);
            if ($size && !preg_match('/width=|height=/', $img)) {
                $img = str_replace('<img', '<img width="' . $size[0] . '" height="' . $size[1] . '"', $img);
            }
        }
        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 to improve LCP
add_action('wp_head', function() {
    if (is_singular() && has_post_thumbnail()) {
        $img = wp_get_attachment_image_src(get_post_thumbnail_id(), 'full');
        if ($img && isset($img[0])) {
            echo '<link rel="preload" as="image" href="' . esc_url($img[0]) . '" fetchpriority="high">' . "\n";
        }
    }
});
// ✅ Preconnect to Google Fonts (if used)
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);
// ✅ Automatically add a title to iframes for accessibility
add_filter('the_content', function($content) {
    return preg_replace_callback('/<iframe(?![^>]*title=["\'])([^>]*)>/i', function($matches) {
        return '<iframe title="Embedded content from external site"' . $matches[1] . '>';
    }, $content);
});
// ✅ Add screen-reader text to empty links (accessibility fix)
add_filter('the_content', function($content) {
    return preg_replace_callback('/<a\s([^>]*href=["\'][^"\']+["\'][^>]*)>\s*<\/a>/i', function($matches) {
        return '<a ' . $matches[1] . '><span class="sr-only">Link</span></a>';
    }, $content);
});
// ✅ Minify final HTML output (lightweight compression)
add_action('template_redirect', function() {
    ob_start(function($html) {
        if (is_admin()) return $html;
        return preg_replace('/\s+/', ' ', $html);
    });
});

Comments

Add a Comment