This tutorial will augment the technique of automatically enabling WP-Cache during heavy load with the ability to switch to a low-bandwidth WordPress theme at the same time.
Few reasons to do this
1. WP-Cache messes with your site statistics, so you do not want to leave it on when your site is not being hammered.
2. You don’t want to use a bandwidth efficient theme all the time because it’s not pretty-lookin’.
3. During traffic storms (e.g. Digg Effect), every 1/100 second optimization tweak counts.
4. If you host your site on a shared host, you will most likely have a bandwidth quota. Switching to a leaner theme conserves your bandwidth (duh!)
5. If you host your site on a home connection, your upload is not up to par with most hosting services, so you need to use that small pipe efficiently.
6. Each “IMG” tag, even if it’s a 1×1 pixel gif, requires an HTTP request to your web server. If you have 10 images on your page, and 10 users are loading your page, that’s 100 simultaneous calls to your server already. Leaner themes usually means less/no images, giving Apache some break.
7. If you’re server is non-uber, you don’t deserve to administer it.
How to do this
I’m building on top of my post about Automatically Enabling WP-Cache during heavy load, so you should go read that first if you haven’t done so already.
- I prepared a “lite” theme, but copying my current theme to a separate folder and named it omninoggin_lite. In the lite theme, I removed all the images and all the visual plugins (including the sidebar).
- I need a way to easily switch my WordPress theme between my normal and lean themes. I know the easiest way to switch themes is to go to wp-admin > presentation > themes, but here is a way to do it from a script (remember to substitute “YOUR-WP-ADMIN-PASSWORD” with your own password).
#!/bin/bash export PATH=$PATH:/usr/local/bin:/usr/bin curl -b cookies.txt -c cookies.txt -d "log=admin&pwd=YOUR-WP-ADMIN-PASSWORD" http://omninoggin.com/wp-login.php > /dev/null curl -b cookies.txt -c cookies.txt -d "log=admin&pwd=YOUR-WP-ADMIN-PASSWORD" http://omninoggin.com/wp-admin/themes.php > themes.php LITE_URL=`grep -o ""themes.php.*=omninoggin_lite&.*"" themes.php | head -1 | sed s/"//g | sed s/#038;//g` FULL_URL=`grep -o ""themes.php.*=omninoggin&.*"" themes.php | head -1 | sed s/"//g | sed s/#038;//g` echo $LITE_URL echo $FULL_URL rm themes.php if [ "$1" = "lite" -o "$1" = "full" ] ; then [ "$1" = "lite" ] && wget --load-cookies cookies.txt "http://omninoggin.com/wp-admin/$LITE_URL" -O /dev/null [ "$1" = "full" ] && wget --load-cookies cookies.txt "http://omninoggin.com/wp-admin/$FULL_URL" -O /dev/null else echo usage: wptheme '<lite|full>' exit 1 fi rm cookies.txt
Now I can easily switch between themes by executing:
sh wptheme lite sh wptheme full
- Building on top of the Auto WP-Cache post, I have to make sure that the ‘autowpcache’ python script will switch my WordPress to my “lite” theme when the server load is too high. To do this, I renamed ‘autowpcache’ to ‘autooptimize’ since it will be enabling both WP-Cache and switching my theme to a leaner theme. I then made the following modifications:
#... commands.getoutput("sh /usr/local/www/apache22/data/wp-content/plugins/wp-cache-on-demand/wptheme lite") time.sleep(end + 5) commands.getoutput("sh /usr/local/www/apache22/data/wp-content/plugins/wp-cache-on-demand/wpcache on") print "Load time for Apache status page went overboard" # ... time.sleep(3600) commands.getoutput("sh /usr/local/www/apache22/data/wp-content/plugins/wp-cache-on-demand/wptheme full") time.sleep(5) commands.getoutput("sh /usr/local/www/apache22/data/wp-content/plugins/wp-cache-on-demand/wpcache off") commands.getoutput("logger -p user.notice WP-Cache has been turned off")
- Enjoy being L33T.
I clocked my page generation time and page load time. Keep in mind that page generation time is how long it takes the server to generate the HTML file from PHP code. Page load time is the period of time between the client request and client response (end of HTML file received). For things like WP-Cache, there is only one page generation time as no PHP code is being executed on subsequent runs. So without further adieu:
0.330 gen time
0.645 load time
0.155 gen time
0.249 load time
0.375 gen time (first only)
0.088 load time
Lean Theme & WP-Cache:
0.231 gen time (first only)
0.068 load time
Notice that if both Lean Theme and WP-Cache is enabled, then there is a whopping 10x speedup from the normal unoptimized load time! Also keep in mind that since all the images are stripped, HTTP requests are reduced and bandwidth is conserved. This ultimately lets your server handle 10+x more visitors than what you would be able to handle with your normal settings. Phew, that was tiring. Enjoy!