MyBB Community Forums

Full Version: Automatically minimise CSS
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
This guide is aimed at those of you not afraid to modify core files. I'll be showing you how to make MyBB automatically minimise your CSS files to increase page load speed by a small amount. To do this, we need to modify the file ./admin/inc/functions_themes.php.


$fp = @fopen(MYBB_ROOT."{$theme_directory}/{$filename}", "wb");
		return false;

Add after:

	$stylesheet = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $stylesheet);
	$stylesheet = str_replace(': ', ':', $stylesheet);
	$stylesheet = str_replace(array("\r\n", "\r", "\n", "\t", '  ', '    ', '    '), '', $stylesheet);

What this does is it strips all the comments, spaces and other pieces of whitespace from your stylesheet when it's saved to the cache. It does help a fair bit - especially if you run your site through a page speed analyser such as GTmetrix which all actively look for minimised CSS.

Unfortunately, MyBB's resync_stylesheet() function means that the CSS stored in the database is also minified too. This is a bit of an annoyance and one I'm going to look into (short of removing the whole function or modifying it a lot, there seems little I can do for now). I therefore advise you back up all your styles before performing this code edit.

Also note that you must go into each stylesheet and save it for the changes to take effect.
Nice tip! Smile
Thanks Smile I'm using this along with my LESS parser and it's working great so far.
good tip, thanks Smile

btw you can modify core files from ACP with these plugins by frostschutz (needed)
Yeah, I use patches too as I have a few core modifications now. I do certainly advise using it.
Thanks euantor I will try it out right now Big Grin
You're welcome Smile
Thanks for this TUT
Thank tutorial Smile share information
For some reason when I save my style sheets, spaces and comments are cut out.

I thought this was about compressing the file just before it was send to the cache folder, not directly at the ACP?
Pages: 1 2