Skip to content

Speeding Up WordPress With If Statements

Speeding up WordPress has just got easier; using PHP’s if statements you can only load the stuff you need.

When building ShiftNews, one thing we were always vary wary of was the effect on load time stuff would have. That meant sprites, not making too many database calls, that kind of thing. And then Alex C split the CSS file in two (just about in two anyway) and put half of it in single.css and the rest in style.css. The single.css file only gets loaded on posts and pages. In a moment the loading time of the homepage’s stylesheet was (just about) halved. Pretty neat. Good news is you can do it too!

Choosing what you need

Choosing which bits you need is probably the hardest bit (and it’s not too hard either!). Obviously you’ll need to keep the barebones layout in the style.css, but things like comment styling, post styles (perhaps you’ve got a custom post layout grid), list styling, blockquotes and the like can go into a new file.

What if…

Next stage is to include the two stylesheets. Open up your header and add the following (or something like):

<link href="<?php bloginfo('stylesheet_url'); ?>" rel="stylesheet" type="text/css" /> <?php if ( is_singular() || is_404() || is_search() || is_archive() ) { ?> <link href="<?php bloginfo('template_url'); ?>/css/single.css" rel="stylesheet" type="text/css" /> <? } else {} ?>

That’s stylesheets done. We can now move onto plugins.

Loading plugins conditionally

Whilst we can’t (easily) load the whole plugin conditionally, it is possible to stop plugins loading additional stylesheets and the like when you don’t want them. For example, Yoast wrote up how to stop lightbox or thickbox loading when he didn’t want them in this post. For others largely the same principal applies as stylesheets – if this kind of page then do this, otherwise, do this:

Do this regardless. <?php if ( is_singular() || is_404() || is_search() || is_archive() ) { ?> But if it's any of the types of pages listed above then do this. <? } else {} ?>

By doing this you can very quickly get rid of all the stuff you don’t need.

Loading the lot together

That’s all very well, you say, but you’re just creating additional stylesheets for yourself. Not so if you combine them all with the excellent Minify. That way you just have one file, regardless; on certain pages you’d just add the extra stylesheets to the Minified file.

I hope this has enlightened you to the possibilities of reducing the amount of stuff you need to load with each page. Now go and implement!

Yay! 🎉 You made it to the end of the article!
Alex Denning

1 Comment
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

Or start the conversation in our Facebook group for WordPress professionals. Find answers, share tips, and get help from other WordPress experts. Join now (it’s free)!

Would love your thoughts, please comment.x

Most Searched Articles

Best JavaScript Libraries and Frameworks: Try These 14 in 2024

In this post, we look at the best JavaScript libraries and frameworks to try out this year. Why? Well, with JavaScript being available in every web browser, this makes it the most accessible programming language of ...

20 Best Free WordPress Themes for 2024 (Responsive, Mobile-Ready, Beautiful)

If you're looking for only the best free WordPress themes in the market for this year, then you're in the right place. We have more than enough such themes for you right ...

12 Best WordPress Hosting Providers of 2024 Compared and Tested

Looking for the best WordPress hosting that you can actually afford? We did the testing for you. Here are 10+ best hosts on the market ...

Handpicked Articles

How to Make a WordPress Website: Ultimate Guide for All Users – Beginners, Intermediate, Advanced

Many people wonder how to make a WordPress website. They’ve heard about WordPress, its incredible popularity, excellent features and designs, and now they want to join the pack and build a WordPress website of their own. So, where does one get ...

How to Start an Ecommerce Business: Ultimate Guide for 2024

Is this going to be the year you learn how to start an eCommerce business from scratch? You’re certainly in the right place! This guide will give you a roadmap to getting from 0 to a fully functional eCommerce business. ...