LATEST >>

Welcome Here And Thanks For Visiting. Like Us On Facebook...

EXEIdeas – Let's Your Mind Rock » WordPress / WordPress Tricks » How To Minify HTML Without Any Plugin In WordPress Easily?

How To Minify HTML Without Any Plugin In WordPress Easily?

Minify-HTML-In-WordPress-Without-Any-Plugin
To make your WordPress blog too fast, you have to consider all mentions that any Page Speed checker says. You have seen Minify HTML error on Page Speed testers when you were checking your blog speed. Now to Minify your blog HTML, you tried to use many WordPress plugins that we dont want to mention here but the result is useless as it still showing Minify HTML error in Page Speed tests.

Now if you think that any plugin can do it then keep in mind that using too many plugins can be dangerous for your WordPress security and also SEO as they can interrupt with your other function and can make a loop hole for hackers so you should avoid plugins as mush as you can and if there is an alternative to plugin work then you should leave plugin 101% surely.

Now here we have a trick to Minify HTML without using any plugin that will also Minify CSS and JavaScript if they are inline. Minifying will make your page smaller at frontend only means when user will open it but it will be same as it was at backend means while you want to edit your codes you can find easily formatted codes at your theme files so it clearly shows that it does not edit your theme file or your does not have to edit your theme files.

You have already seen many tricks to Minify HTML and after following those tricks they really Minify HTML but Page Speed testers are still giving the error so it means that they are useless because they have not showing the notice that this page is minimized. When you will add our below code then after seeing your view-source of your WordPress blog, you will find the below line at the end of your page that means your page is minimized and will help to remove your Page Speed errors.

<!–-HTML compressed, size saved ...%. From ...bytes, now ...bytes-–>

Now without more preface, This post will show you the step by step guide on how to increase your page load speed by minifying inline JavaScript, css and HTML without a plugin. So move below anbd follow the tutorial.

How To Minify HTML Without Any Plugin In WordPress Easily?

Step 1:) Copy the below code and Paste in in your Theme Function(function.php) file.

/* ------------------------------------------------------------------------- *
 * Compress HTML Codes Of Your WordPress Blog
/* ------------------------------------------------------------------------- */
class WP_HTML_Compression
{
 // Settings
 protected $compress_css = true;
 protected $compress_js = true;
 protected $info_comment = true;
 protected $remove_comments = true;
 // Variables
 protected $html;
 public function __construct($html)
 {
 if (!empty($html))
 {
 $this->parseHTML($html);
 }
 }
 public function __toString()
 {
 return $this->html;
 }
 protected function bottomComment($raw, $compressed)
 {
 $raw = strlen($raw);
 $compressed = strlen($compressed);
 $savings = ($raw-$compressed) / $raw * 100;
 $savings = round($savings, 2);
 return '<!--HTML compressed, size saved '.$savings.'%. From '.$raw.' bytes, now '.$compressed.' bytes-->';
 }
 protected function minifyHTML($html)
 {
 $pattern = '/<(?<script>script).*?<\/script\s*>|<(?<style>style).*?<\/style\s*>|<!(?<comment>--).*?-->|<(?<tag>[\/\w.:-]*)(?:".*?"|\'.*?\'|[^\'">]+)*>|(?<text>((<[^!\/\w.:-])?[^<]*)+)|/si';
 preg_match_all($pattern, $html, $matches, PREG_SET_ORDER);
 $overriding = false;
 $raw_tag = false;
 // Variable reused for output
 $html = '';
 foreach ($matches as $token)
 {
 $tag = (isset($token['tag'])) ? strtolower($token['tag']) : null;
 $content = $token[0];
 if (is_null($tag))
 {
 if ( !empty($token['script']) )
 {
 $strip = $this->compress_js;
 }
 else if ( !empty($token['style']) )
 {
 $strip = $this->compress_css;
 }
 else if ($content == '<!--wp-html-compression no compression-->')
 {
 $overriding = !$overriding;
 // Don't print the comment
 continue;
 }
 else if ($this->remove_comments)
 {
 if (!$overriding && $raw_tag != 'textarea')
 {
 // Remove any HTML comments, except MSIE conditional comments
 $content = preg_replace('/<!--(?!\s*(?:\[if [^\]]+]|<!|>))(?:(?!-->).)*-->/s', '', $content);
 }
 }
 }
 else
 {
 if ($tag == 'pre' || $tag == 'textarea')
 {
 $raw_tag = $tag;
 }
 else if ($tag == '/pre' || $tag == '/textarea')
 {
 $raw_tag = false;
 }
 else
 {
 if ($raw_tag || $overriding)
 {
 $strip = false;
 }
 else
 {
 $strip = true;
 // Remove any empty attributes, except:
 // action, alt, content, src
 $content = preg_replace('/(\s+)(\w++(?<!\baction|\balt|\bcontent|\bsrc)="")/', '$1', $content);
 // Remove any space before the end of self-closing XHTML tags
 // JavaScript excluded
 $content = str_replace(' />', '/>', $content);
 }
 }
 }
 if ($strip)
 {
 $content = $this->removeWhiteSpace($content);
 }
 $html .= $content;
 }
 return $html;
 }
 public function parseHTML($html)
 {
 $this->html = $this->minifyHTML($html);
 if ($this->info_comment)
 {
 $this->html .= "\n" . $this->bottomComment($html, $this->html);
 }
 }
 protected function removeWhiteSpace($str)
 {
 $str = str_replace("\t", ' ', $str);
 $str = str_replace("\n", '', $str);
 $str = str_replace("\r", '', $str);
 while (stristr($str, ' '))
 {
 $str = str_replace(' ', ' ', $str);
 }
 return $str;
 }
}
function wp_html_compression_finish($html)
{
 return new WP_HTML_Compression($html);
}
function wp_html_compression_start()
{
 ob_start('wp_html_compression_finish');
}
add_action('get_header', 'wp_html_compression_start');

Step 2:) Save and you are done.

Theme Files Editing Warning:

Keep in mind that every code that you will add in your themes files will be removed when you will change the theme. So after changing your theme, you have to follow this article again and you will get everything back same as it is now.

Last Words:

This is what we have and shared in easy steps for newbies so that they can easily know how it works. Stay with us because we are going to share a whole guide step by step about WordPress and make it easy for you. If you liked it then share it and be with us to get next tutorial. If you have any problem then feel free to ask us. We will help you with what we can or have.

You Like It, Please Share This Recipe With Your Friends Using...

2 Responses to “How To Minify HTML Without Any Plugin In WordPress Easily?”

  1. alfredo says:

    this code no work fuck you bichh!!

Leave a Reply

Your email address will not be published. Required fields are marked *