Development,  WordPress

Let’s build WordPress theme – part 3

OK, boys and girls, we are here with the third part of our tutorial. How to build WordPress theme. Well, as we can see in our previous parts (part 1, part 2), it’s more than easy. Of course today we’ll get deeper.

New player

Our new player is functions.php file. Let’s create it in root theme folder (beside style.css and index.php). This file is very special for WordPress theme development, because it is always fired, before the rest of the theme. This file is optional, but it is very important for us. Here we will do all WordPress magic.

At this point, the content of functions.php will be very small. This will be enough:

What is the point of that code? It checks if ABSPATH constant is defined or not. It not, it will ends script with “No direct access.” message. Why? It can be potential security hole. We can’t let anyone execute files of the theme without WordPress running. ABSPATH is a WordPress constant so if it is defined, we can be sure that we are running in this environment.

Now we can add something like this:

as a second line, just to see what will happen. And what? We can see Hello! message on the screen so we are sure that functions.php is running. Of coruse we should delete that line – it is bad idea to print anything in functions.php. You can see source of the page – Hello! message is located before doc type declaration and it is bad thing to do.

So, why do we need functions.php? Let’s create our first class.

 Singleton pattern

If you are familiar with object oriented programming I have nothing to say. But if you are not – let me explain this a bit. Singleton pattern means that we can use one and only instance of the class. The class is build using some techniques which prevents PHP from creating another instance of the class. As we have one theme – we need one class to handle it. Of course it doesn’t mean we won’t create other classes. We will, we will… Because some of other classes will be written using singleton pattern too, we can create abstract singleton class for future use.

This code will allow us to use class Name extends BooyahSingleton later. And we will do! Now we are creating main theme class:

Very easy, we do not need anything else now. How to call this class? Well, it is easy:

Do not write it to the functions.php file, it’s just an example.

Now, two things:

Variable scopes

To avoid problems with variable scopes, we will create special function, just to get us instance of the theme class. Let’s do this:

And now it is extremal easy to use anywhere in the theme. Of course we need to run this class, before use (I mean constants for example), so we have to simply put BT(); in our code. This will also automatically execute Init method (only once, of course, as it is singleton) if

PHP Version

The Singleton Pattern class code contains two things – get_called_class function and static keyword (in new declaration context). Those two features requires at least PHP version 5.3. Version 5.3 is already marked as old and is known since 2009. So if you do not have access to PHP 5.3… Change your provider or update your server. Really.

What’s next?

OK, we’ve got pretty nice theme class but why do we need it for? Well, we can start with some constants. It is always faster to use constant than to use WordPress functions. And if we have to use some value in more that one places, it is worth it. Let’s add SetConstants method to our BooyahTheme class and call it from within Init method..

What does it mean? We are setting BOOYAH_URI as our template URL address and BOOYAH_DIR as our template directory. We do not need to call WP functions on that, it’s no longer necessary and it is faster. Additionally – we’ve got directory separator at the very end of each constant. We’ve got to remember that difference.

This is a good start, let’s go into

Automate classes loading

Now you know that we will be using classes. Do we really need to include files with classes? Of course not. Class auto loading is implemented by the PHP and we will use this mechanism.

More informations about this soon!

Leave a Reply

Your email address will not be published. Required fields are marked *