Using Fractal Templates Part 1: Class Based Templates in WordPress

Before I started writing websites with WordPress, I was writing code in Python. The last thing I wrote was a sudoku solver/creator, but that’s for a later post. Suffice it to say, I love Python’s mix of procedural, functional, and object oriented paradigms. When I first started working with WordPress, I was new to PHP and didn’t think it was an object oriented language. Then I realized that PHP had OOP (Object Oriented Programming) features, but the WordPress community just wasn’t taking advantage of this awesome aspect of PHP.

OOP basically enables a code block to be defined as a class and enables other classes to inherit from it. Each class is made up of attributes and methods (functions belonging to a class are referred to as a method, even though they have the keyword `function` in front of them). When class B inherits from class A, all of A’s methods and attributes are automatically part of class B. Class B can then change any methods it inherited, and can add new methods for additional functionality. For a good article explaining object oriented programming in PHP, read this.

I understand why people avoid using classes; like most things there is somewhat of a learning curve. But the advantages, in my humble opinion, well outweigh the time spent learning. The real advantage to OOP is it changes how how you write code. Using class inheritance in WordPress theme files allows you to focus solely on what is different from template to template, which means you write less code, and can reuse more of the code that you write. We at Rocket Lift have created a class standard that we use to greatly simplify the development of WordPress themes in this way. We call it Fractal.

The following is the basic structure of the index.php file in a WordPress theme using the Fractal system.


<?php
if ( ! defined( 'FRACTAL_LEAF' ) )
  define( 'FRACTAL_LEAF', __FILE__ );

class Fractal {
  function __construct() {?><!DOCTYPE html>
    <html>
      <head>
        <?php $this->html_head(); ?>
      </head>

      <body <?php $this->body_classes(); ?>’>
        <?php $this->body(); ?>
      </body>
    </html>
  <?php
  }

  function body_classes() { body_class(); }

  function html_head() {
    wp_head();
  }

  function body() { 
    // ...
  }

  // ...
}

if ( FRACTAL_LEAF === __FILE__ )
  new Fractal();

First, let’s look at the variable FRACTAL_LEAF. It is used to determine what template is actually being created. Notice how at the bottom we check for this value and create a new version of our class if is equal to the current file. This is needed because inheriting templates will set FRACTAL_LEAF before they include this parent index.php, preventing this parent index.php file from setting it, and creating the child’s version of the class. We set FRACTAL_LEAF to __FILE__ because __FILE__ is a simple and unique identifier to distinguish between template files.

If that doesn’t make sense right away, just know that is how we use the right version of the Fractal class when things get more complicated later because we have templates inheriting from each other.

Next, we have the class declaration: class Fractal { ... }. We have set index.php to be the root of the inheritance, meaning all other Fractal template classes will inherit from this class. I’ll go into more detail on this in later posts dealing with child template files (not to be confused with child theme templates).

__construct() is a special method. An object’s __construct() method is automatically called when it is created. We use it to echo out the HTML code. This way, when we create an object from this class in the line new Fractal();, the template will render. The contents of this method consist of the the HTML common to all files in our theme, as well as method calls for the sections of the page that change from template to template. The contents of this __construct() method will vary widely to accommodate your unique WordPress theme code.

In future posts, I will demonstrate how child templates inherit from this one, and discuss how this Fractal pattern makes several traditional WordPress theme files obsolete, like header.php, footer.php, and sidebar.php. We’ll also look at how Fractal can severely shrink your theme’s functions.php file.

Update: Continue on to Part 2 of this series.

Leave a Reply