An Ongoing Post on How-to Optimize WordPress

This post contains tricks and tips on how I’ve optimized time while working with WordPress.

Stop using bloginfo(‘something’)!

Quit making unnecessary calls to your database.  A lot of themes I’ve worked on over the years I see this:

<a href="<?php bloginfo('url'); ?>" title="<?php bloginfo('name'); ?>"><?php bloginfo('name'); ?></a>

Another one I see a lot goes something like this:

<img src="<?php bloginfo('template_url'); ?>/images/image_file.jpg" alt="<?php bloginfo('name'); ?>" />

I’m just going to reiterate what a lot of people have already blogged about before.  Please save yourselves from yourself by pasting this at the bottom of your wp-config.php file.

$url = get_bloginfo('url');
define('url', $url);

$template_url = get_bloginfo('template_url');
define('template_url', $template_url);

$name = get_bloginfo('name');
define('name', $name);

$description = get_bloginfo('description');
define('description', $description);

$template_directory = get_bloginfo('template_directory');
define('template_directory', $template_directory);

Now you can reference your bloginfo variables like this:

<a href="<?php echo url; ?>" title="<?php echo name; ?>"><?php echo name; ?></a>
<img src="<?php echo template_url; ?>/images/image_file.jpg" alt="<?php echo name; ?>" />

Use one stylesheet!

When building custom themes using 960.gs, the HTML5 reset, etc, run them through a CSS Compressor and include them at the top of your stylesheet.  This will save space in your code editor and reduce the amount of network requests.

/* http://html5doctor.com/html-5-reset-stylesheet/ */
  html,body,div,span,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,abbr,address,cite,code,del,dfn,em,img,ins,kbd,q,samp,small,strong,sub,sup,var,b,i,dl,dt,dd,ol,ul,li,fieldset,form,label,legend,table,caption,tbody,tfoot,thead,tr,th,td,article,aside,canvas,details,figcaption,figure,footer,header,hgroup,menu,nav,section,summary,time,mark,audio,video{border:0;outline:0;font-size:100%;vertical-align:baseline;background:transparent;margin:0;padding:0}body{line-height:1}article,aside,details,figcaption,figure,footer,header,hgroup,menu,nav,section{display:block}nav ul{list-style:none}blockquote,q{quotes:none}blockquote:before,blockquote:after,q:before,q:after{content:none}a{font-size:100%;vertical-align:baseline;background:transparent;margin:0;padding:0}ins{background-color:#ff9;color:#000;text-decoration:none}mark{background-color:#ff9;color:#000;font-style:italic;font-weight:700}del{text-decoration:line-through}abbr[title],dfn[title]{border-bottom:1px dotted;cursor:help}table{border-collapse:collapse;border-spacing:0}hr{display:block;height:1px;border:0;border-top:1px solid #ccc;margin:1em 0;padding:0}input,select{vertical-align:middle}

/* 960gs */
  .container_16{margin-left:auto;margin-right:auto;width:976px}.grid_1,.grid_2,.grid_3,.grid_4,.grid_5,.grid_6,.grid_7,.grid_8,.grid_9,.grid_10,.grid_11,.grid_12,.grid_13,.grid_14,.grid_15,.grid_16{display:inline;float:left;position:relative;margin-left:10px;margin-right:10px}.alpha{margin-left:0}.omega{margin-right:0}.container_16 .grid_1{width:41px}.container_16 .grid_2{width:102px}.container_16 .grid_3{width:163px}.container_16 .grid_4{width:224px}.container_16 .grid_5{width:285px}.container_16 .grid_6{width:346px}.container_16 .grid_7{width:407px}.container_16 .grid_8{width:468px}.container_16 .grid_9{width:529px}.container_16 .grid_10{width:590px}.container_16 .grid_11{width:651px}.container_16 .grid_12{width:712px}.container_16 .grid_13{width:773px}.container_16 .grid_14{width:834px}.container_16 .grid_15{width:895px}.container_16 .grid_16{width:956px}.container_16 .prefix_1{padding-left:61px}.container_16 .prefix_2{padding-left:122px}.container_16 .prefix_3{padding-left:183px}.container_16 .prefix_4{padding-left:244px}.container_16 .prefix_5{padding-left:305px}.container_16 .prefix_6{padding-left:366px}.container_16 .prefix_7{padding-left:427px}.container_16 .prefix_8{padding-left:488px}.container_16 .prefix_9{padding-left:549px}.container_16 .prefix_10{padding-left:610px}.container_16 .prefix_11{padding-left:671px}.container_16 .prefix_12{padding-left:732px}.container_16 .prefix_13{padding-left:793px}.container_16 .prefix_14{padding-left:854px}.container_16 .prefix_15{padding-left:915px}.container_16 .suffix_1{padding-right:61px}.container_16 .suffix_2{padding-right:122px}.container_16 .suffix_3{padding-right:183px}.container_16 .suffix_4{padding-right:244px}.container_16 .suffix_5{padding-right:305px}.container_16 .suffix_6{padding-right:366px}.container_16 .suffix_7{padding-right:427px}.container_16 .suffix_8{padding-right:488px}.container_16 .suffix_9{padding-right:549px}.container_16 .suffix_10{padding-right:610px}.container_16 .suffix_11{padding-right:671px}.container_16 .suffix_12{padding-right:732px}.container_16 .suffix_13{padding-right:793px}.container_16 .suffix_14{padding-right:854px}.container_16 .suffix_15{padding-right:915px}.container_16 .push_1{left:61px}.container_16 .push_2{left:122px}.container_16 .push_3{left:183px}.container_16 .push_4{left:244px}.container_16 .push_5{left:305px}.container_16 .push_6{left:366px}.container_16 .push_7{left:427px}.container_16 .push_8{left:488px}.container_16 .push_9{left:549px}.container_16 .push_10{left:610px}.container_16 .push_11{left:671px}.container_16 .push_12{left:732px}.container_16 .push_13{left:793px}.container_16 .push_14{left:854px}.container_16 .push_15{left:915px}.container_16 .pull_1{left:-61px}.container_16 .pull_2{left:-122px}.container_16 .pull_3{left:-183px}.container_16 .pull_4{left:-244px}.container_16 .pull_5{left:-305px}.container_16 .pull_6{left:-366px}.container_16 .pull_7{left:-427px}.container_16 .pull_8{left:-488px}.container_16 .pull_9{left:-549px}.container_16 .pull_10{left:-610px}.container_16 .pull_11{left:-671px}.container_16 .pull_12{left:-732px}.container_16 .pull_13{left:-793px}.container_16 .pull_14{left:-854px}.container_16 .pull_15{left:-915px}.clear{clear:both;display:block;overflow:hidden;visibility:hidden;width:0;height:0}.clearfix:after{clear:both;content:' ';display:block;font-size:0;line-height:0;visibility:hidden;width:0;height:0}.clearfix{display:block}* html .clearfix{height:1%}

/* fancybox */
  #fancybox-loading{position:fixed;top:50%;left:50%;width:40px;height:40px;margin-top:-20px;margin-left:-20px;cursor:pointer;overflow:hidden;z-index:1104;display:none}#fancybox-loading div{position:absolute;top:0;left:0;width:40px;height:480px;background-image:url(img/fancybox/fancybox.png)}#fancybox-overlay{position:absolute;top:0;left:0;width:100%;z-index:1100;display:none}#fancybox-tmp{border:0;overflow:auto;display:none;margin:0;padding:0}#fancybox-wrap{position:absolute;top:0;left:0;z-index:1101;outline:none;display:none;padding:20px}#fancybox-outer{position:relative;width:100%;height:100%;background:#fff}#fancybox-content{width:0;height:0;outline:none;position:relative;overflow:hidden;z-index:1102;border:0 solid #fff;padding:0}#fancybox-hide-sel-frame{position:absolute;top:0;left:0;width:100%;height:100%;background:transparent;z-index:1101}#fancybox-close{position:absolute;top:-15px;right:-15px;width:30px;height:30px;background:transparent url(img/fancybox/fancybox.png) -40px 0;cursor:pointer;z-index:1103;display:none}#fancybox-error{color:#444;font:normal 12px/20px Arial;margin:0;padding:14px}#fancybox-img{width:100%;height:100%;border:none;outline:none;line-height:0;vertical-align:top;margin:0;padding:0}#fancybox-frame{width:100%;height:100%;border:none;display:block}#fancybox-left,#fancybox-right{position:absolute;bottom:0;height:100%;width:35%;cursor:pointer;outline:none;background:transparent url(img/fancybox/blank.gif);z-index:1102;display:none}#fancybox-left{left:0}#fancybox-right{right:0}#fancybox-left-ico,#fancybox-right-ico{position:absolute;top:50%;left:-9999px;width:30px;height:30px;margin-top:-15px;cursor:pointer;z-index:1102;display:block}#fancybox-left-ico{background-image:url(img/fancybox/fancybox.png);background-position:-40px -30px}#fancybox-right-ico{background-image:url(img/fancybox/fancybox.png);background-position:-40px -60px}#fancybox-left:hover,#fancybox-right:hover{visibility:visible}#fancybox-left:hover span{left:20px}#fancybox-right:hover span{left:auto;right:20px}.fancybox-bg{position:absolute;border:0;width:20px;height:20px;z-index:1001;margin:0;padding:0}#fancybox-bg-n{top:-20px;left:0;width:100%;background-image:url(img/fancybox/fancybox-x.png)}#fancybox-bg-ne{top:-20px;right:-20px;background-image:url(img/fancybox/fancybox.png);background-position:-40px -162px}#fancybox-bg-e{top:0;right:-20px;height:100%;background-image:url(img/fancybox/fancybox-y.png);background-position:-20px 0}#fancybox-bg-se{bottom:-20px;right:-20px;background-image:url(img/fancybox/fancybox.png);background-position:-40px -182px}#fancybox-bg-s{bottom:-20px;left:0;width:100%;background-image:url(img/fancybox/fancybox-x.png);background-position:0 -20px}#fancybox-bg-sw{bottom:-20px;left:-20px;background-image:url(img/fancybox/fancybox.png);background-position:-40px -142px}#fancybox-bg-w{top:0;left:-20px;height:100%;background-image:url(img/fancybox/fancybox-y.png)}#fancybox-bg-nw{top:-20px;left:-20px;background-image:url(img/fancybox/fancybox.png);background-position:-40px -122px}#fancybox-title{font-family:Helvetica;font-size:12px;z-index:1102}.fancybox-title-inside{padding-bottom:10px;text-align:center;color:#333;background:#fff;position:relative}.fancybox-title-outside{padding-top:10px;color:#fff}.fancybox-title-over{position:absolute;bottom:0;left:0;color:#FFF;text-align:left}#fancybox-title-over{background-image:url(img/fancybox/fancy_title_over.png);display:block;padding:10px}.fancybox-title-float{position:absolute;left:0;bottom:-20px;height:32px}#fancybox-title-float-wrap{border:none;border-collapse:collapse;width:auto}#fancybox-title-float-wrap td{border:none;white-space:nowrap}#fancybox-title-float-left{background:url(img/fancybox/fancybox.png) -40px -90px no-repeat;padding:0 0 0 15px}#fancybox-title-float-main{color:#FFF;line-height:29px;font-weight:700;background:url(img/fancybox/fancybox-x.png) 0 -40px;padding:0 0 3px}#fancybox-title-float-right{background:url(img/fancybox/fancybox.png) -55px -90px no-repeat;padding:0 0 0 15px}.fancybox-ie6 #fancybox-close{background:transparent;}.fancybox-ie6 #fancybox-left-ico{background:transparent;}.fancybox-ie6 #fancybox-right-ico{background:transparent;}.fancybox-ie6 #fancybox-title-over{background:transparent;zoom:1}.fancybox-ie6 #fancybox-title-float-left{background:transparent;}.fancybox-ie6 #fancybox-title-float-main{background:transparent;}.fancybox-ie6 #fancybox-title-float-right{background:transparent;}.fancybox-ie6 #fancybox-bg-w,.fancybox-ie6 #fancybox-bg-e,.fancybox-ie6 #fancybox-left,.fancybox-ie6 #fancybox-right,#fancybox-hide-sel-frame{height:expression(this.parentNode.clientHeight+"px")}#fancybox-loading.fancybox-ie6{position:absolute;margin-top:0;top:expression((-20+(document.documentElement.clientHeight?document.documentElement.clientHeight/2:document.body.clientHeight/2) 0 (ignoreMe=document.documentElement.scrollTop?document.documentElement.scrollTop:document.body.scrollTop)) 0 px)}#fancybox-loading.fancybox-ie6 div{background:transparent;}.fancybox-ie .fancybox-bg{background:transparent!important}

/* client name */
  .start_your_css_here{
   stop:creating-more-requests;
}

Comments

Aug 21, 2011
2:10pm

Phil Lewis says:

Excellent idea to minimize database hits by caching key bloginfo(*) values-- I should start doing this myself. I would highly recommend against polluting the global namespace with constants such as "name" and "description", however.I agree with consolidating your stylesheet information to cut down on server requests but I'm not a big fan of minifying, personally. Fine with large libraries/frameworks that you won't be modifying (eg jQuery), but I'd say configure your server to deliver your text content via gzip and leave it at that.Just my $.02, thanks for the thoughtful write-up.
Aug 30, 2011
4:31pm

jhatweb says:

I guess if Wordpress does eventually begin using constants or a plugin tries to override them it would go bad, but I haven't ran across that issue yet. I'll start using "_" or "__" as a prefix.The stylesheet thing is something I started doing when I'd have a bunch of extra files in an asset directory. I asked myself, why don't I just consolidate them? So I did. I won't minify the actual CSS for the site, but libraries yes.I'm sure you've seen some of my work with Mark and Sarah. I keep myself very systematic, and without being able to start a project off with these tips I'll feel lost.I'll actually be making a visit sometime, so we'll have a chance to bump into one another. :)
Sep 1, 2011
11:55am

Phil Lewis says:

It'll be great to meet you. I've more than "seen" some of you work-- I've borrowed a couple of very useful pieces to solve puzzles on new sites.

Post a comment

Your email is never published nor shared. Required fields are marked *

You may use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


− two = 2