Comments on: How to Minify HTML in WordPress Without a Plugin https://zuziko.com/tutorials/how-to-minify-html-in-wordpress-without-a-plugin/?utm_source=rss&utm_medium=rss&utm_campaign=how-to-minify-html-in-wordpress-without-a-plugin Tue, 15 Sep 2020 16:57:04 +0000 hourly 1 By: Martin https://zuziko.com/tutorials/how-to-minify-html-in-wordpress-without-a-plugin/#comment-2148 Tue, 15 Sep 2020 16:57:04 +0000 https://zuziko.com/?p=2487#comment-2148 After some more time I run into further issues with my early fix. Here is the updated fix.

Replace:

function flhm_wp_html_compression_start()
{
ob_start(‘flhm_wp_html_compression_finish’);
}
add_action(‘get_header’, ‘flhm_wp_html_compression_start’);

With:

function flhm_wp_html_compression_start()
{
// don’t minify HTML on this domain or part of the domain
$blacklist_domain = ‘.localdev’;

// if the function is called on a blacklisted domain, don’t minify HTML
if (false !== strpos(site_url(), $blacklist_domain) || is_user_logged_in()) {
return;
}

ob_start(‘flhm_wp_html_compression_finish’);
}
add_action(‘init’, ‘flhm_wp_html_compression_start’, 1);

]]>
By: Martin https://zuziko.com/tutorials/how-to-minify-html-in-wordpress-without-a-plugin/#comment-2070 Thu, 06 Aug 2020 19:16:50 +0000 https://zuziko.com/?p=2487#comment-2070 Unfortunately, the above code breaks for me the current version of WordPress.

Using add_action(‘init’, ‘flhm_wp_html_compression_start’); instead of add_action(‘get_header’, ‘flhm_wp_html_compression_start’); breaks creating and updating Gutenberg posts for me. It happens even when you prevent the code from running in the admin dashboard.

if (!is_admin()) {
add_action(‘init’, ‘flhm_wp_html_compression_start’);
}

Disabling CSS, JS and comments compression also didn’t help.

Here is the fix that works for me. I also included a code that prevents HTML compression on a specified by you domain, for example the local environment.

Replace:

function flhm_wp_html_compression_start()
{
ob_start(‘flhm_wp_html_compression_finish’);
}
add_action(‘get_header’, ‘flhm_wp_html_compression_start’);

With:

function flhm_wp_html_compression_start()
{
// don’t minify HTML on this domain or part of the domain
$blacklist_domain = ‘.localdev’;

// if the function is called on a blacklisted domain, don’t minify HTML
if (false !== strpos(site_url(), $blacklist_domain)) {
return;
}

ob_start(‘flhm_wp_html_compression_finish’);
}
add_action(‘template_redirect’, ‘flhm_wp_html_compression_start’);

]]>
By: Diego https://zuziko.com/tutorials/how-to-minify-html-in-wordpress-without-a-plugin/#comment-1837 Tue, 19 May 2020 19:19:26 +0000 https://zuziko.com/?p=2487#comment-1837 Change add_action(‘get_header’, ‘flhm_wp_html_compression_start’); to add_action(‘init’, ‘flhm_wp_html_compression_start’);

]]>
By: mike https://zuziko.com/tutorials/how-to-minify-html-in-wordpress-without-a-plugin/#comment-1179 Sun, 29 Mar 2020 09:00:57 +0000 https://zuziko.com/?p=2487#comment-1179 You should at least provide your source: https://blog.finderonly.net/wp-content/uploads/2011/05/minifier-script.txt

]]>
By: Charles Smith https://zuziko.com/tutorials/how-to-minify-html-in-wordpress-without-a-plugin/#comment-996 Mon, 20 Jan 2020 23:30:48 +0000 https://zuziko.com/?p=2487#comment-996 I usually build in Laravel and avoid WordPress when possible. I decided to build my marketing site in WordPress and find the out-of-the-box messy source code horrible at best.

I didn’t want to load a plugin for this, so when I found your code to be so simple to implement, I was super excited.

Simply Brilliant!!!

]]>
By: Prathiban Sukumaran https://zuziko.com/tutorials/how-to-minify-html-in-wordpress-without-a-plugin/#comment-376 Tue, 11 Jun 2019 05:36:07 +0000 https://zuziko.com/?p=2487#comment-376 Hi, your code worked great. I was using Autoptimize but now I do not need that plugin, thank you. But there is one issue, my product carousel has stopped working. Could you tell me how to fix this issue?

Thanks in advance!

]]>
By: David Green https://zuziko.com/tutorials/how-to-minify-html-in-wordpress-without-a-plugin/#comment-224 Mon, 29 Apr 2019 12:06:19 +0000 https://zuziko.com/?p=2487#comment-224 In reply to Roger.

Hi Roger,

Yes, that is correct! If you don’t want to compress JS and CSS just set those values to false like shown below.

protected $flhm_compress_css = false;
protected $flhm_compress_js = false;
]]>
By: Roger https://zuziko.com/tutorials/how-to-minify-html-in-wordpress-without-a-plugin/#comment-223 Mon, 29 Apr 2019 08:43:41 +0000 https://zuziko.com/?p=2487#comment-223 I used the code but essential JS and CSS stopped working. I was looking for HTML minify and it did work. However, can it be edited to just simply minify HTML? Do I set the values to false if I don’t want JS or CSS minified?

]]>
By: Ant https://zuziko.com/tutorials/how-to-minify-html-in-wordpress-without-a-plugin/#comment-212 Fri, 12 Apr 2019 18:10:05 +0000 https://zuziko.com/?p=2487#comment-212 Awesome! I’m always looking for ways to do things without installing a million plugins. Works great on my website.

Thank you!

]]>