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:
1 |
<?php defined( 'ABSPATH' ) or exit('No direct access.'); |
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:
1 |
echo 'Hello!'; |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
class BooyahSingleton { private static $instances = array(); protected function __construct() {} protected function __clone() { throw new Exception( 'Class ' . get_called_class() . ' cannot be cloned!' ); } protected function __wakeup() { throw new Exception( 'Class ' . get_called_class() . ' cannot be unserialized!' ); } public static function getInstance() { $c = get_called_class(); if ( !isset( self::$instances[ $c ] ) ) { self::$instances[ $c ] = new static; if ( method_exists( self::$instances[ $c ], 'Init' ) ) { self::$instances[ $c ]->Init(); } } return self::$instances[ $c ]; } } |
This code will allow us to use class Name extends BooyahSingleton later. And we will do! Now we are creating main theme class:
1 2 |
class BooyahTheme extends BooyahSingleton { } |
Very easy, we do not need anything else now. How to call this class? Well, it is easy:
1 |
$theme = BooyahTheme::getInstance(); |
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:
1 2 3 |
function BT() { return BooyahTheme::getInstance(); } |
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..
1 2 3 4 5 6 7 |
public function SetConstants() { define( 'BOOYAH_URI', get_template_directory_uri() . '/' ); define( 'BOOYAH_DIR', get_template_directory() . DIRECTORY_SEPARATOR ); } public function Init() { $this->SetConstants(); } |
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!