Minify and compress Javascript files in PHP
Loading time is an important factor in search engine optimization and for the user experience. Especially for theme developers, it’s important to compress or minimize all embedded javascript files for live use on a web page.
How can I compress Javascript files?
The following PHP function provides an easy way to minify javascript without doing it manually. A compressed file version of the original Javascript file could be easily stored in a temporary folder and retrieved if the file exists. This saves a lot of javascript code and a considerable file size.
How do we use the Javascript compression function?
The function minify_js( $file_content );
ensures that all unnecessary tabs, spaces, breaks, and comments are removed within the Javascript file. As result, we get a compressed and minified version of the file content.