‘Minify‘ 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.
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; } }
Step 5
Let’s move on, using above minify function in WordPress OR Any PHP application(s)
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!