WordPress Symfony2 Powered: The Ultimate WP-Plugin

Update:
There is a correction in terms that the integration is done with twig firstly at least for now and twig is not really part of symfony2 as a component. However the door is open for such integration so more work is required to integrate some of the more powerful components.

There was some talk earlier about getting WordPress to do the right thing with Symfony2.
Today I had thoughts of giving PowerPress or I shall say WordPress the right powers with Symfony2 Components.
This effort is the seed effort that will unfold an unprecedented number of plugins with great capability for wordpress. In addition to this blog post, there has been some pioneer work with Symfony2 Components integration into Drupal 8. We hope to work further and further with integration to make the sf2 community able to harness great projects from any platform. This shows how sf2 was designed to harness really an important portion of the future of web development.

As we strive to be programmers of few words, onto the meat.
Oh but before we proceed, let me thank Darko Goles and Andy Leon in whose shoulders I am standing. And I thank my God for the grace and enthusiasm of His enabling to a sinner as we all are and a n00b as few still are in sf2, like me.

Now onto the meat:

<?php
/*
  Plugin Name: Wordpress Symfony2 Wedlock
  Version: 1.0
  Plugin URI: http://www.craftitonline.com
  Description: Wedlock your apps with Symfony2 :)
  Author: Luis Cordova - cordoval@gmail.com http://www.craftitonline.com
  Author URI: http://www.craftitonline.com
 */
use Symfony\Component\ClassLoader\UniversalClassLoader;
require_once __DIR__.'/lib/ClassLoader/UniversalClassLoader.php';
 
function sf2AutoLoad() {
 
    $loader = new UniversalClassLoader();
 
    $loader->registerNamespaces(array(
        'Sf2Plugins'           => __DIR__.'/src',
        // add more libraries here to power wordpress even more
        // namespace based loader
    ));
 
    $loader->registerPrefixes(array(
        'Twig_Extensions_' => __DIR__.'/lib/Twig/Twig-extensions/lib',
        'Twig_'            => __DIR__.'/lib/Twig/lib',
        // add more libraries here to power wordpress even more
        // prefix based loader
    ));
    $loader->register();
 
}
 
add_action('init', 'sf2AutoLoad');
?>

Very Important: Here we will just illustrate how we hook Twig and WordPress however the work is not limited to this integration. Rather if we desire to include other components we are free to do so by just adding the loading lines above and getting our libraries straight. This is very powerful way of integrating into worpdress plugins that will deliver extreme power to wordpress sites.

Moving on then, we define our plugin and right from the start we make use of the Symfony2 Autoloader component. In particular we use the Universal Autoloader which is great to develop. We load Twig and its extension set and also we allow for our project plugin folder classes which we will place by convention under the src folder from where they can start building namespaces according to their location in the document directory tree. Then we just add the function we defined into an `init` hook which is how worpdress works its horizonal development.

In order to install the plugin one should do the initialization of the three libraries we use. The libraries will fall under src folder automatically provided you install the git repo as follows:

cd wp-content/plugins/
git clone git://github.com/cordoval/wp-sf2.git
git submodule init
git submodule update

These two commands above will do the job. I just tested it.
The following is adapted from Darko Goles’ work and idea which basically allows to find the functions in wordpress and calling them through an object in twig. This is very useful and we have just arranged the class for better structure and reuse.

<?php
namespace Sf2Plugins;
 
class TwigPlugin {
 
    public function __call($function, $arguments) {
 
        if (!function_exists($function)) {
            trigger_error('call to unexisting function ' . $function, E_USER_ERROR);
            return NULL;
        }
        return call_user_func_array($function, $arguments);
    }
}

Now on the side of the theme we are using this theme. The theme has a file `twig.php` which is loaded by `functions.php`. It basically hooks our code into rendering PowerPress [Wordpress] templates. In there we are basically going to load our special class above and also we ready our template engine to which we now pass the $wpTemplate which is the output from the wp router. We don’t then need to label the top of our template files with calls to php but rather use pure twig in those wp files.

    $site = new TwigPlugin();
    $loader = new Twig_Loader_Filesystem(__DIR__);
    $twig = new Twig_Environment($loader, array(
            'cache' => false
        ));
    $template = $twig->loadTemplate(basename($wpTemplate));
    $template->display(array('site' => $site));
}

Our base template now would look something like:

<!DOCTYPE html>
<html {{ site.language_attributes }}>
 
<head>
<meta charset="{{ site.bloginfo('charset') }}" />
<title>{% block title %}{% endblock %}</title>
<link rel="profile" href="http://gmpg.org/xfn/11" />
<link rel="stylesheet" type="text/css" media="all" href="{{ site.bloginfo('stylesheet_url') }}" />
<link rel="pingback" href="{{ site.bloginfo('pingback_url') }}" />
<!-- 
<?php

The code for the theme is under development but it is pretty straightforward to update. I will do it for my projects for better templates. However this far should give you already unprecedented power to integrate Symfony2 Components into WordPress. To check the repo for the theme you can go here. To make it even easier to contribute for twig wordpress templates you can clone repo here by doing:

cd wp-content/themes/
git clone https://github.com/cordoval/Twig-for-WordPress.git sf2theme

Make sure you fork the repo on github first and then add remote url to git so you can push and then do a PR.

Thanks and if you find this useful please donate!

I could have done a demo right here in this blog on this plugin but my hosting still is working on soon delivering php5.3 support.

11 thoughts on “WordPress Symfony2 Powered: The Ultimate WP-Plugin

  1. I don’t get it.
    Where do I have to put those lines?

    $site = new TwigPlugin();
    $loader = new Twig_Loader_Filesystem(__DIR__);
    $twig = new Twig_Environment($loader, array(
    ‘cache’ => false
    ));
    $template = $twig->loadTemplate(basename($wpTemplate));
    $template->display(array(‘site’ => $site));
    }

  2. Pingback: Wordpress Symfony2 Wedlock | Best Plugins - wordpress – widgets – plugin 2012

  3. I’m very happy to find this page. I wanted to thank you for ones time just for this fantastic read!! I definitely liked every bit of it and I have you book marked to check out new things on your site.

  4. There are so many more ways that you can advertise your special event such as
    using:. Everyone needs to work together and these people can help you.
    Lapel pins can be made in a variety of different styles to fit
    specific needs.

    Look into my website; religious lapel pins
    - advertisetruck.com -

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>