Thord Daniel Hedengren - Smashing WordPress_ Beyond the Blog-Wiley (2014)

(avery) #1

CHAPTER 8 • Plugins or functions.php? 215


database queries. Say you have ten plugins on a page, and each one initializes a loop and
outputs relevant database content. That will not only take time, but it will also put additional
strain on the server, possibly making it move sluggishly.


As mentioned earlier, a lot of plugins are really the loop in disguise. If you’re displaying posts,
comments, tags, or anything that builds around that, you’re really querying the database. It is
just like putting all those custom loops in your theme and watching them spin.


Now, a decent server won’t usually mind the extra burden, but a lot of visitors at the same
time can bring it to its knees. Database queries are heavy stuff, so you should be wary of
having too many of those on the same page without the hardware to back it up.


The same is true for other types of functionality, from simple PHP functions to inclusion of
third-party content. True, the biggest issue with these things comes whenever you fetch
something from outside the server, which means that you’ll be relying not only on your own
host’s hardware, but also the speed that the content can be reached (in other words, the speed
of the Internet) and how quickly the target server can serve you the content. That is why a
bunch of widgets pulling data from various services online will slow down any site out there.


The lesson is to not use too many plugins that put a strain on the server. That way, you’ll keep
site load times as low as possible. If you do need to use a lot of heavy plugins, make sure that
you use whatever cache solutions fit your needs. Ironically, the best ones are plugins them-
selves, such as W3 Total Cache (http://wordpress.org/extend/plugins/
w3-total-cache) or WP Super Cache (http://wordpress.org/plugins/
wp-super-cache). By using a cache plugin, you will serve static files instead of dynamic
content to most of your visitors. You can consult the WordPress plugin repository’s featured
and popular section for some of the options out there. It is also important to remember that
although a lot of plugins can make your site’s performance less than stellar, it is not a universal
truth. You could have a large number of plugins installed without slowing down your site; it
all depends on the plugins themselves.


WHEN TO USE FUNCTIONS.PHP


When is it really a good idea to use functions.php? I have a rule for that too: Only use
functions.php when the added functionality is unique to your theme.


The reasoning around this is simple: functions.php is tied to one particular theme, which
means that if you switch themes, your added functionality won’t be available, forcing you to
either re-create it in the new theme or abandon it altogether. This effectively disqualifies
functions.php from any kind of use that controls output of content because that would mean
when you switch themes, the content would not be outputted or, at worst, it would render the
site broken and full of errors.


Many of the things you do to extend your sites will be minor, and to be honest, you would
probably be smarter to make those changes within the actual template files, keeping the
plugin count to a minimum. However, some features are such big parts of the actual site

Free download pdf