MyBB Community Forums
Automatically minimise CSS - Printable Version

+- MyBB Community Forums (
+-- Forum: Resources (
+--- Forum: Tutorials (
+--- Thread: Automatically minimise CSS (/thread-102661.html)

Pages: 1 2

Automatically minimise CSS - Euan T - 2011-08-28

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.

RE: Automatically minimise CSS - faviouz - 2011-08-28

Nice tip! Smile

RE: Automatically minimise CSS - Euan T - 2011-08-28

Thanks Smile I'm using this along with my LESS parser and it's working great so far.

RE: Automatically minimise CSS - aglioeolio - 2011-08-28

good tip, thanks Smile

btw you can modify core files from ACP with these plugins by frostschutz (needed)

RE: Automatically minimise CSS - Euan T - 2011-08-28

Yeah, I use patches too as I have a few core modifications now. I do certainly advise using it.

RE: Automatically minimise CSS - Omar G. - 2011-08-28

Thanks euantor I will try it out right now Big Grin

RE: Automatically minimise CSS - Euan T - 2011-08-29

You're welcome Smile

RE: Automatically minimise CSS - nadlerz - 2011-08-29

Thanks for this TUT

RE: Automatically minimise CSS - Dragonzsoul - 2011-08-29

Thank tutorial Smile share information

RE: Automatically minimise CSS - Omar G. - 2011-08-29

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?