TWIG with WordPress part1

Featured Image

Making TWIG autoload from WP plugin

Maybe someone will say: this is a crazy idea. Why would someone want to do that?
Maybe it really is crazy, but I would say: why not?

What is TWIG? TWIG is new generation php templating engine made by Sensio Labs. It is used in their new project Symfony2 as main templating engine provided with framework.
As official page says:

Fast: Twig compiles templates down to plain optimized PHP code. The overhead compared to regular PHP code was reduced to the very minimum.
Secure: Twig has a sandbox mode to evaluate untrusted template code. This allows Twig to be used as a template language for applications where users may modify the template design.
Flexible: Twig is powered by a flexible lexer and parser. This allows the developer to define its own custom tags and filters, and create its own DSL.
It is not necessary to explain in detail why will someone (me) want to use that as wordpress templating engine, but one of the main reasons is that I want to rewrite some messed up php template for WP to better code organized one, and at the and I want it to be with cleaner code as possible.
I don’t like very much when is necessary to mix up php tags and functions inside HTML code to make it work.
After some thinking about TWIG and how to implement it in my WP installation I got idea:
I will make plug in for WP that will provide all necessary stuff to start using TWIG with WP.

First I created the plugin folder named wp_twig_engine.I put folder named ‘lib‘ downloaded from twig-project site in there. So I had the twig library almost prepared for using.
Now I had to find some appropriate wordpress hook to inject my code into wordpress application logic. After some investigating and testing, I found that it will be possible to use the ‘init‘ hook and register autoloaders for TWIG library and my custom class in the same function, so I can reach my code from inside template folder later.

Main plugin file: wp-twig.php

  Plugin Name: WordPress Twig templating engine
  Version: 1.0
  Plugin URI:
  Description: Engine for creating twig templates for wordpress
  Author: Darko Goleš
  Author URI:
//Main twig library autoloader file
require_once dirname(__FILE__) . '/lib/Twig/Autoloader.php';
//My custom class made, I also want it to be autoloaded
require_once dirname(__FILE__) . '/Wp_TwigEngine.php';
function twigAutoLoad() {
add_action('init', 'twigAutoLoad');

Just to show you how to autoload my custom class with that (of course I got idea from original twig autoloader and just changed the code to fit my needs:

  Author: Darko Goleš
  Author URI:
class Wp_TwigEngine_Autoloader {
    static public function register() {
        ini_set('unserialize_callback_func', 'spl_autoload_call');
        spl_autoload_register(array(new self, 'autoload'));
    static public function autoload($class) {
        if (0 !== strpos($class, 'Wp_TwigEngine')) {
        if (file_exists($file = dirname(__FILE__) . '/../' . str_replace(array('_', "\0"), array('/', ''), $class) . '.php')) {
            require $file;

And that’s it from the plugin side for now. Just have to implement it in the template folder. That is additional theme for another article. :-)


Care to rate this post?


Darko Goles

Ex Inchooer

Darko worked at Inchoo as Backend Developer from March 2011 to January 2014

Other posts from this author

Discussion 0 Comments

Add Comment

Add Your Comment

Please wrap all source codes with [code][/code] tags.
  • Wordpress with E-Commerce?

    Wordpress with E-Commerce?

    It is a neat idea for a small businesses. Especially if you want to test your market. However, if you want to switch to an enterprise solution, Magento is the best choice. Take a look at our services and find out what we can do to help.

    Learn more Get a quote
  • Hire Magento Developers

    Magento Certified Developers

    Teams of certified Magento developers are available to work on your next big thing!

    Hire Magento Developers Get a quote
  • Our Latest Project

    Viv & Lou

    Viv & Lou

    When an ordinary gift just won’t do.

    See our work
  • The Inchooers

    Meet The Inchooers, the funny bunch of magento developers.