This code snippet is designed to optimize a WordPress site’s performance by compressing HTML output, removing unnecessary assets, and improving loading efficiency. It begins by compressing the HTML output, removing extra spaces and newlines to reduce the overall size of the HTML sent to the browser. This is achieved using the ob_start function, which starts output buffering and applies a series of regular expressions to the output buffer to minify the HTML. The shutdown action ensures that the buffer is properly flushed at the end of the request to send the optimized HTML to the browser.
The snippet also optimizes WordPress assets by removing unnecessary scripts and styles. It removes emoji-related scripts and styles, oEmbed scripts, and the jQuery Migrate script if it’s not needed on the frontend. Additionally, it conditionally dequeues the comment-reply script if comments are not open or threaded comments are not enabled.
Further optimizations include enabling lazy loading for images, which delays the loading of off-screen images until the user scrolls near them, thereby improving page load times. The code also removes the WordPress Block Library CSS from non-Gutenberg pages to avoid loading unnecessary styles. Finally, it deregisters the Dashicons stylesheet from the frontend for non-admin users to reduce the amount of CSS loaded.
The optimize_wp_assets function encapsulates these optimizations and is hooked into the wp_enqueue_scripts action with a high priority to ensure it runs after all other scripts and styles have been enqueued. This comprehensive approach helps improve the website’s performance by minimizing the amount of data sent to the user’s browser and reducing the load on the server.
// Compress HTML output and remove specific footer text
add_action('init', function() {
ob_start(function($buffer) {
$buffer = preg_replace('/\n\s+/', "\n", $buffer);
$buffer = preg_replace('/>\s+</', '><', $buffer); $buffer = preg_replace('/\s+/', ' ', $buffer); return $buffer; }); }); // Ensure the buffer is flushed at the end of the request add_action('shutdown', function() { if (ob_get_length()) { ob_end_flush(); } }, 999); // Optimize WordPress assets function optimize_wp_assets() { // Remove Emoji scripts and styles remove_action('wp_head', 'print_emoji_detection_script', 7); remove_action('wp_print_styles', 'print_emoji_styles'); // Remove oEmbed scripts remove_action('wp_head', 'wp_oembed_add_host_js'); // Dequeue jQuery Migrate if not needed add_filter('wp_default_scripts', function (&$scripts) { if (!is_admin()) { $scripts->remove('jquery');
$scripts->add('jquery', false, array('jquery-core'), '1.12.4');
}
});
// Dequeue comment-reply script if comments or threaded comments are not used
add_action('wp_enqueue_scripts', function() {
if (!is_singular() || !comments_open() || !get_option('thread_comments')) {
wp_dequeue_script('comment-reply');
}
});
// Lazy load images
add_filter('wp_get_attachment_image_attributes', function($attr) {
$attr['loading'] = 'lazy';
return $attr;
});
// Remove WordPress Block Library CSS for non-Gutenberg pages
add_action('wp_enqueue_scripts', function() {
wp_dequeue_style('wp-block-library');
wp_dequeue_style('wp-block-library-theme');
wp_dequeue_style('global-styles'); // Since WordPress 5.9
}, 100);
// Remove Dashicons from the frontend for non-admin users
add_action('wp_enqueue_scripts', function() {
if (!current_user_can('update_core') && !is_admin_bar_showing()) {
wp_deregister_style('dashicons');
}
});
}
// Hook the optimize_wp_assets function into wp_enqueue_scripts
add_action('wp_enqueue_scripts', 'optimize_wp_assets', 99);
Warning: All code found on this site is use at your own risk vibe. It worked for me when I wrote it, but if it breaks your stuff don’t come shout at me. Happy to help if I can, but there are no guarantees expressed and implied. This is merely an example for developers most of the code examples on this site are scripts that are used in Code Snippets functions.
Removing unnecessary scripts and styles can significantly improve Google Page Speed scores by reducing the amount of data that needs to be transferred from the server to the client, minimizing render-blocking resources, and decreasing the overall time it takes for a page to load. Here’s a detailed explanation:
- Reduced Data Transfer:
- Unnecessary scripts and styles increase the amount of data that must be downloaded by the browser. By removing these, the browser has fewer resources to download, resulting in faster page load times.
- Minimized Render-Blocking Resources:
- Scripts and styles, especially those in the head of an HTML document, are render-blocking, meaning they must be downloaded, parsed, and executed before the page can be rendered. Removing unnecessary resources allows the browser to render the page more quickly.
- Improved First Contentful Paint (FCP):
- The FCP metric measures the time it takes for the first piece of content to be rendered on the screen. By reducing the number of scripts and styles, the FCP is improved as the browser can display content sooner.
- Enhanced Time to Interactive (TTI):
- TTI measures how long it takes for a page to become fully interactive. By removing unnecessary scripts, the main thread is less congested, enabling the page to become interactive more quickly.
- Lower Total Blocking Time (TBT):
- TBT is the total time between FCP and TTI that the main thread was blocked. Fewer scripts result in less blocking time, contributing to better TBT scores.
Gutenberg
Oh, WordPress. What happened to the platform we once loved for its simplicity and speed? Enter Gutenberg, the so-called innovative editor that’s more of a Frankenstein’s monster than a gift. What was once a clean, efficient CMS is now weighed down by an over-engineered, clunky mess. The block-based approach might sound clever on paper, but in practice? It’s a disaster. Every block drags in its own pile of excessive CSS and JavaScript, turning pages into slow-loading nightmares. Google Page Speed scores? Tanked. User experience? Suffering. And let’s not forget the endless scripts and render-blocking resources that make visitors stare at blank screens.
WordPress seems to have lost its way, prioritizing flexibility over the core values that made it great: simplicity and performance. Gutenberg isn’t progress. It’s a bloated, resource-hungry beast, forcing users and developers to wrestle with sluggish sites and wasted bandwidth. And now, the contagion has spread to WooCommerce. This once-reliable e-commerce solution has been shackled with Gutenberg’s inefficiencies, leaving merchants to grapple with longer load times, frustrated customers, and lost sales. WooCommerce was a powerhouse of streamlined functionality, but now it’s dragging Gutenberg’s dead weight behind it.
The community is fed up. This isn’t evolution—it’s a step backward. It’s time for WordPress to remember its roots and return to what worked: lightweight, efficient tools that empower users, not hinder them. The call is clear: ditch the bloat, kill the blocks, and bring back the WordPress we knew and loved.
"Microsoft isn't evil, they just make really crappy operating systems."