PressTigers

Minify WordPress HTML via functions.php file

WordPress – an online Open Source website creation tool written in PHP is probably the easiest and most powerful blogging or website content management system (CMS) in existence today. Many plugins are available that will help you minify the HTML of your WordPress website. Sometimes plugins fail to deliver the required output and break the website.

Minifying the HTML of your website will increase the loading speed of your website. It will also increase your website rank in Google Page Insights. You can see your website rank here. As bunch of plugins available at WordPress repository can minify your WordPress site HTML, but lots of other things come along with these plugins and for sure you don’t need all of them.

If you only want to minify your site HTML then you should not use any plugin that comes with a bundle of extra features. Before minification, your HTML will look like the following screenshot:

before-minify-html

Code for Minifying WordPress HTML:

Today, we are revealing the following code that you can put into your Theme‘s functions.php file and your website HMTL will be minified:

get_header is a hook that runs at the very start of get_header function call. If you pass the name for a specific header file, like get_header( ‘new’ ), the do_action will pass the name as a parameter for the hook. This will allow you to limit your add_action calls to specific templates if you wish. Actions added to this hook should be added to your functions.php file.

As this code contains get_header hook function that will save the output of HTML to buffer until all HTML is minified, so ob_start will be turned on for output buffering. ob_start function calls “pt_html_minyfy_finishfunction in its argument that will actually minify the HTML.

It will also match the comments by regular expressions and remove commented HTML from site. We have used ‘string replace‘ in this code that will remove all the spacing, tab space, next line space from the HTML. Also, we have used ‘while‘ to match one character space from HTML and removed that space from HTML. Lastly, the minified HTML will be returned. Now the minified HTML will look like the below screenshot:

minified-html
Note:

Using inline javascript in your HTML may break your javascript code. Best practice is to use a separate file for your javascript and include it into your HTML.

User Comments

7 thoughts on “Minify WordPress HTML via functions.php file

    Amitpal Singh says:

    This breaked my website. Replaced all the ‘r’, ‘n’ and ‘t’ 🙁

      Hello Amitpal,

      Make sure you are using inline JavaScript or not? As, there is a warning for using inline JavaScript code, also this may break poorly coded HTML.

      Thanks for reaching us out. Let us know if you need our further assistance.

      Thanks & Regards,

      Abdullah | WP Plugin Developer
      Email: support@presstigers.com

      Hello Amitapal,

      Sorry for the inconvenience.

      We have updated the code after testing. Please use this code and then let us know if you are still facing any issue.

      Thanks & Regards,

      Abdullah | WP Plugin Developer
      Email: support@presstigers.com

    Richard says:

    The problem with the code snippet above is that this line:

    $html = str_replace(array(“rn”, “r”, “n”, “t”), ”, $html);

    Should read:

    $html = str_replace(array(“\r\n”, “\r”, “\n”, “\t”), ”, $html);

    Otherwise, the script just replaces all of the r, n, and t characters in the html.

    Udegbunam Chukwudi says:

    Thanks a million. It works just fine on my site

    David says:

    This code breaks forms created by the Ninja Forms plugin, specifically it removes the space between “input” and “type” for a field, leaving “inputtype” as one word, which simply doesn’t render the form field.

Leave a Reply

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

    Get in Touch