Today I was optimizing a theme when I found some interesting code on the theme’s “functions.php” file. Before revealing the code in question (nothing too terrible, by the way) you should know that the file functions.php of the current theme is always loaded by WordPress for the administrative side as well as for the public side.

On a high traffic blog, this file should be made as much as efficient as possible and it should only run the code needed on public side when the blog is serving a page of content to the public.

WordPress lets coders to check if we are on public or administrative context with a simple function, is_admin(), and it is defined early in the process (hence it can be used as soon as the theme’s “function.php” file is loaded). This function returns “true” if we are inside the administrative panels, so a plug-in or a theme can register hooks or run code that is pertinent to that context. It returns “false” if the context is for the public side of the site.

The theme in question, as very first thing, checks:

  • PHP version
  • WordPress version

They seem simple checks, but why I should check that EVERY time a reader loads a page of my blog? When the theme is activated, for sure we are on the admin side and so that check can be limited to the administrative context. May be only during the first activation or may be every time (the administrative traffic is almost irrelevant).

I understand the coders point of view: check as much as possible in every context to avoid support requests. But my point of view is that I cannot upgrade a virtual server because it needs more CPU to do nothing.

Shortly I would release a plug-in example where we can discuss good coding and optimization. Would you participate?

Similar Posts

Leave a Reply