Automatically minimise CSS
#1
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.

Find:

$fp = @fopen(MYBB_ROOT."{$theme_directory}/{$filename}", "wb");
	if(!$fp)
	{
		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.
Reply
#2
Nice tip! Smile
Reply
#3
Thanks Smile I'm using this along with my LESS parser and it's working great so far.
Reply
#4
good tip, thanks Smile

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

http://mods.mybb.com/view/patches
http://mods.mybb.com/view/pluginlibrary (needed)
Reply
#5
Yeah, I use patches too as I have a few core modifications now. I do certainly advise using it.
Reply
#6
Thanks euantor I will try it out right now Big Grin
Reply
#7
You're welcome Smile
Reply
#8
This user has been denied support. This user has been denied support.
Thanks for this TUT
Heart

[Image: logo2.png]
Reply
#9
Thank tutorial Smile share information
Reply
#10
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?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)