HTML, Inline JS & Inline CSS Minify PHP

Minify PHP – HTML, inline JavaScript and inline CSSMinify‘ word always pops up while optimizing and performance tuning for Web Application(s) around the world.  Typically it’s meant for HTML, JavaScript, CSS, Inline JavaScript and Inline CSS.  I would like to describe about minify PHP framework and leverage framework capabilities for PHP based application(s) and simple usage in WordPress blog without plugin.

minify is a PHP5 app/framework – It combines multiple CSS or Javascript files, removes unnecessary whitespace and comments, and serves them with gzip encoding and optimal client-side cache headers.  Minify PHP framework implements rules from Yahoo Exceptional Performance

myjeeva blog minify (HTML, Inline JS & CSS) have been done using minify PHP framework and this article born from it.

Article scope is to do minify of HTML, Inline JS, and Inline CSS.  Most important part is, minify PHP framework doesn’t break your web page (Google Adsens or any ads, inline JavaScript and inline CSS).  It’s is best to leverage on :)


How to achieve Minify in WordPress or PHP

Step 1

Download minify framework from Google Code

Step 2

Choose an appropriate location for minify PHP framework.  Based on usage we can choose location, as per below-

  • Performing HTML, Inline JavaScript and Inline CSS: we can place the minify PHP framework anywhere i.e. inside or outside docroot
  • Performing on-demand JavaScript and CSS combining with Minification (out of scope for this article)

As per point #1 and scope of the article, location for minify PHP framework is outside the docroot for myjeeva.com i.e.

/srv/www/myjeeva.com/min

Step 3

Extract the downloaded archive minify-<version>.zip and upload the directory min into /srv/www/myjeeva.com.

Step 4

Minify PHP function myjeeva_minify_html is capable of minifying HTML, inline JavaScript, inline CSS (much more) using minify PHP framework.

function myjeeva_minify_html ($buffer) {
	if (is_user_logged_in()) {
		$buffer .= "<!-- User is loggedin, compression is not applied. -->";
		return $buffer; // for loggedin users minify is not required
	} else {
		$initial = strlen($buffer);
		$minify_lib_path = '/srv/www/myjeeva.com/min';  // Update absolute path of your minify framework
		if (!class_exists('Minify_HTML')) {
			// Line no. 10 & 11 is only applicable to minify v2.1.7
			require("$minify_lib_path/lib/Minify/Loader.php");
			Minify_Loader::register();
			require_once("$minify_lib_path/lib/Minify/HTML.php");
			ini_set('include_path', ini_get('include_path').":$minify_lib_path/lib");
			require_once("$minify_lib_path/lib/Minify/CSS.php");
			require_once("$minify_lib_path/lib/JSMin.php");
		}

		// Calling minify function with HTML content
		$buffer = Minify_HTML::minify($buffer,
             	array('cssMinifier' => array('Minify_CSS', 'minify'),
       	          	 'jsMinifier' => array('JSMin', 'minify')));

		$final = strlen($buffer);
		$savings = round((($initial-$final)/$initial*100), 3);
		$buffer .= "<!-- Uncompressed size: $initial bytes; Compressed size: $final bytes; $savings% savings -->";

		return $buffer;
	}
}

Note: Update variable $minify_lib_path (line #7 ) value with appropriate absolute path as per your website/file system.

Step 5

Let’s move on, using above minify function in WordPress OR Any PHP application(s)

Place the function from Step 4 and below code snippet into functions.php

// Minifying HTML
function myjeeva_minify() {
    ob_start('myjeeva_minify_html');
}

add_action('get_header', 'myjeeva_minify');

Create a PHP file called minify.php and place the function from Step 4 then below code snippet.  As per article location of the minify.php file is /srv/www/myjeeva.com/minify.php

ob_start('myjeeva_minify_html');

Step 6

Open up php.ini or custom php.ini, based on type of hosting plan you have and place the following line into it. Don't forget to save :)

auto_prepend_file = /srv/www/myjeeva.com/minify.php

Note: Kindly ensure execute and read permission for minify.php and min framework directory by web server.


Conclusion

You have successfully achieved the minify of HTML, inline JavaScript and inline CSS for WordPress blog and any PHP application(s). Simple and elegant way using minify PHP framework and myjeeva blog.

Thoughts and clarifications are welcome!