·

photo credit: Peat Bakke via photopin cc

3 different ways to setup your WordPress theme

When it comes to customizing your WordPress installation, there are various ways to get the same thing done. I already wrote an article a while ago, describing how you can keep your WordPress template clean. In this article I’ll explain how a specific task in WordPress can be done in different ways. Each method has it’s pros and cons and I’ll try my best to explain these to you.

Method 1: functions, functions everywhere…

This is most probably the method you already are most familiar with, since this is the method WordPress promotes and it’s the most used method in all the tutorials out there. If you ask me, it’s the most ugliest method, but that’s just one mans’ opinion.
The idea is very basic: you create a function, and you use the name of that function as parameter when defining actions, filters, shortcodes, etc.:

Pros

  • It’s very simple PHP and easy to understand for people who don’t speak PHP as a native language.

Cons

  • Every function is in the global scope of your WordPress theme.
  • Adding your actions, hooks, shortcodes, etc., always requires 2 steps that need taken.
  • Your functions.php -file will most likely become a very long file doing various stuff in various places. Pin-pointing a single area of ‘stuff’ might become challenging while this file keeps gaining weight.

Method 2: inline functions

Now I’m surprised that I don’t see this method more often. Since most WordPress developers are also familiar with jQuery and it’s anonymous function structure, why not apply this principal to PHP? After all, most hooks, filters and actions in your functions.php -file are called once anyway:

Pros

  • It’s still very simple PHP and easy to understand.
  • The code is significant shorter.
  • There are no longer functions in your global scope.

Cons

  • Your functions.php -file is still very likely to become a very long file doing various stuff in various places.
  • You can’t use anonymous functions if you want to know the return values of the functions. the add_filter() -function for example can return either true  or false .

Method 3: classes, classes everywhere

This method is also described in this article, and basically requires the functionality of your theme to be wrapped in classes. Take the following code for example:

While this may look overwhelming, it’s actually quite elegant to setup your theme like this. All functions are in their own scope and you only need to initialize the class to set it all up. And since the code is separated in different classes, it’s also easier to re-use your code. Think about custom post types or shortcodes for example. When these are located in different classes, they are easier to re-implement in your future WordPress sites.
In the end, your functions.php-file for your entire site could look something like this:

Now, that code worth looking at!

Pros

  • Leaner and cleaner code, each with their own scope.
  • Easier to maintain when working with multiple developers.
  • Re-usable code.

Cons

  • More difficult for those who are just starting with PHP.
  • Might require more time to setup (although the benefits are much greater)

In conclusion

In the end there are multiple ways to achieve the same goal. And although your client or your project manager will not see any difference in the frontend, for you as a developer it’s important that you keep your code lean and clean. After all, it’s not a job, it’s a profession.
I’m sure there are much more ways on how to setup your WordPress themes and if you have a 4th, 5th or 6th method worth sharing, please feel free to do so in the comments section below.

How would you rate this article?

Leave a Reply