PrestaShop: Minifying HTML source code

PrestaShop 1.4 introduced a feature called “Minify HTML” to improve performance. In this how to you will find how to add this exciting feature to PrestaShop 1.3!

  1. Download PrestaShop 1.5.6.3 (the latest version of 1.5 branch).
  2. Extract prestashop/tools/minify_html/minify_html.class.php file from the package.
  3. Copy minify_html.class.php file to tools/minify_html/ directory (you will have to create this directory).
  4. Create a new file Media.php with the following content and put it to the class directory:
    <?php
    /*
    * 2007-2013 PrestaShop 
    *
    * NOTICE OF LICENSE
    *
    * This source file is subject to the Open Software License (OSL 3.0)
    * that is bundled with this package in the file LICENSE.txt.
    * It is also available through the world-wide-web at this URL:
    * http://opensource.org/licenses/osl-3.0.php
    * If you did not receive a copy of the license and are unable to
    * obtain it through the world-wide-web, please send an email
    * to license@prestashop.com so we can send you a copy immediately.
    *
    * DISCLAIMER
    *
    * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
    * versions in the future. If you wish to customize PrestaShop for your
    * needs please refer to http://www.prestashop.com for more information.
    *
    *  @author PrestaShop SA <contact@prestashop.com>
    *  @copyright  2007-2013 PrestaShop SA
    *  @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
    *  International Registered Trademark & Property of PrestaShop SA
    */
    
    class	Media extends ObjectModel
    {
    
    	public static function minifyHTML($html_content)
    	{
    		if (strlen($html_content) > 0)
    		{
    			//set an alphabetical order for args
    			/*$html_content = preg_replace_callback(
    				'/(<[a-zA-Z0-9]+)((\s*[a-zA-Z0-9]+=[\"\\\'][^\"\\\']*[\"\\\']\s*)*)>/',
    				array('Media', 'minifyHTMLpregCallback'),
    				$html_content,
    				Media::getBackTrackLimit());*/
    
    			require_once(_PS_TOOL_DIR_.'minify_html/minify_html.class.php');
    			$html_content = str_replace(chr(194).chr(160), '&nbsp;', $html_content);
    			if (trim($minified_content = Minify_HTML::minify($html_content, array('xhtml' => true))) !=  '')
    				$html_content = $minified_content;
    
    			return $html_content;
    		}
    		return false;
    	}
    
    	public static function minifyHTMLpregCallback($preg_matches)
    	{
    		$args = array();
    		preg_match_all('/[a-zA-Z0-9]+=[\"\\\'][^\"\\\']*[\"\\\']/is', $preg_matches[2], $args);
    		$args = $args[0];
    		sort($args);
    		// if there is no args in the balise, we don't write a space (avoid previous : <title >, now : <title>)
    		if (empty($args))
    			$output = $preg_matches[1].'>';
    		else
    			$output = $preg_matches[1].' '.implode(' ', $args).'>';
    		return $output;
    	}
    	
    	public static function getBackTrackLimit()
    	{
    		static $limit = null;
    		if ($limit === null)
    		{
    			$limit = @ini_get('pcre.backtrack_limit');
    			if (!$limit)
    				$limit = -1;
    		}
    
    		return $limit;
    	}
    
    }
    
  5. Open config/smarty.config.inc.php file.

    Find:

    $smarty->debug_tpl		= _PS_ALL_THEMES_DIR_ . 'debug.tpl';
    

    After, add:

    function smartyMinifyHTML($tpl_output, &$smarty)
    {
        $tpl_output = Media::minifyHTML($tpl_output);
        return $tpl_output;
    }
    

    Find:

    $smarty->compile_check	= false;
    

    Before, add:

    $smarty->register_outputfilter('smartyMinifyHTML');
    

Save and close all files and it’s done!

Leave a Reply

Cart  
(empty)

Cart Check out  »

Prices are tax inclusive.

The VAT rate for your country (US) * is 0,0 % because it is not a member of the European Union (EU).

* Please create an account if your country does not match.

Community feed
  • [Zen Cart] Can't execute Magic SEO URLs sitemaps
    I installed Magic SEO URLs Sitemaps Add-On for ZenCart MSU4.x/MSU5.x 2.0 on my server, when I try to generate sitemaps using: https://www.pechesudv155.owally.com/sitemaps.php I received :...
    by peter@pechesud.com
  • [phpBB3] Sharing users between phpBB / PrestaShop
    Hi i have 2 question 1- prestashop module support phpBB 3.3 ? 2- why we can not login in prestashop and phpbb with same user?
    by zohall
  • [phpBB3] AJAX Userinfo Extension
    Hi! I'm having trouble with this extension: https://www.phpbb.com/customise/db/exte ... _userinfo/ I tried asking support from the author but for no avail, since now. My request for support...
    by Lord Phobos
  • [phpBB3] Upgrade to phpBB 3.2.3
    Hello.. after Upgrade to phpBB 3.2.3 I am getting this message.. How can I solve this issue..? Thank you
    by ingbrzy
  • [phpBB3] URL path changed
    Hello.. I have changed my site path after moving to new hosting and now can not activate SEO module.. new path http://www.miuios.cz/domains/miuios.cz/ could you help me? thank you
    by ingbrzy2
Join our support forum » Pre-Sales Questions »
Featured Testimonials

We have a fully customizable website based in osCommerce, we work with it since our start in 2005. When we want to expand our business to other countries near Spain, we know that we would have several problems with SEO and Google. We found Magic SEO URLs luckily and in fact Magic SEO URLs provide us a complete solution to solve our...

Ruben, the owner of MASmuscolo

I was a little concerned about using a 3rd party SEO module for osCommerce - there were some well established ones and they were free - Magic SEO URLs for osCommerce were paid for! But I was looking at my sites and getting concerned with duplicate content - everything was being duplicated and the search engines (allegedly) look down on...

Martin, the owner of Milton Gems and Shop4Silver

More Testimonials »