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.

36 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 -

  5. You can get this at the official Notre Dame store
    in navy blue, green, or white for $65. Think about the
    personality of the person who you are giving the birthday gift
    to, and you’ll come up with a fantastic birthday gift idea.
    Educators receive dozens of gifts for just about every gift-giving occasion.

  6. My programmer is trying to persuade me to move to .net from PHP.
    I have always disliked the idea because of the costs. But he’s tryiong none the less.
    I’ve been using WordPress on several websites for about a year
    and am worried about switching to another platform.

    I have heard good things about blogengine.net.
    Is there a way I can import all my wordpress posts into it?
    Any kind of help would be really appreciated!

    my page :: cheap car insurance (http://Shortfoyer97547791.Pen.io)

  7. An outstanding share! I’ve just forwarded this onto a colleague who was conducting a little homework on this.
    And he actually ordered me lunch because I stumbled upon it for him…

    lol. So allow me to reword this…. Thanks for the meal!!
    But yeah, thanks for spending some time to talk about this
    topic here on your web site.

  8. Every weekend i used to pay a visit this web site, because i want enjoyment, since this this
    website conations truly pleasant funny data too.

    Look into my weblog :: descargar whatsapp gratis para iphone;
    Benito,

  9. I’m gone to tell my little brother, that he should also visit this weblog on regular basis to obtain updated from newest news update.

    Stop by my website – best mobile security app for iphone 5, Bart,

  10. I loved as much as you’ll receive carried out right here.
    The sketch is attractive, your authored material stylish.
    nonetheless, you command get bought an nervousness over that you wish be delivering the following.
    unwell unquestionably come further formerly again since exactly
    the same nearly a lot often inside case
    you shield this hike.

    my webpage :: build html5 mobile apps for iphone android and blackberry

  11. Relax your back including your chinese culture spine.
    Accessed November 1, 200 scientists that the number of veterinarians who hold membership in the
    world.

  12. I like reading through an article that will make men and women think.

    Also, thanks for allowing me to comment!

    Have a look at my blog post :: descargar whatsapp gratis para android lg – Jayne -

  13. You’re so awesome! I don’t suppose I’ve truly read something like this before.
    So good to discover somebody with unique thoughts
    on this issue. Seriously.. thank you for starting this up.

    This website is one thing that is needed on the internet, someone with a little originality!

    Also visit my web page; aplicaciones para celular samsung
    galaxy, Lora,

  14. Stickng tto txt only on your squeeze page is thhe best wayy to load faster
    and provide all the information needed byy the visitor. Afrer five long
    yearss of struggles and I just putt thhe pills that
    could help me fel better on the shelf. Chances are that competitor are not going to
    bee willung to “share” thewir secrets.

    Feel free to visit my blog: Sukanto Tanotto (Thesukantotanoto.Wordpress.Com)

  15. Appreciating the commitment you put into your site and detailed information you
    provide. It’s awesome to come across a blog every once in a
    while that isn’t the same unwanted rehashed information.
    Fantastic read! I’ve bookmarked your site and I’m including your RSS feeds to my Google
    account.

    My blog post – finance first home (Marcel)

  16. I’ve made another video specially demonstrating how I germinate my marijuana seeds, that we’ll be publishing fairly
    soon around the THC Digest Facebook funnel just in case
    you aren’t sure how to proceed through that approach. Since the seeds germinated, I set up
    my grow closet and prepared it for when the vegetables were willing to be rooted.
    For this kind of develop, this included constructing my own scrog (or screen of green) for
    your first-time. After the seeds were germinated, I permit
    them start doing their factor and acquired them
    within their new home inside my hydro setup. You’ll need a grownup female plant to
    clone.

  17. Greetings from Los angeles! I’m bored to death at work so I decided to
    check out your site on my iphone during lunch break.
    I really like the knowledge you provide here and can’t wait
    to take a look when I get home. I’m shocked at how quick your blog loaded on my mobile
    .. I’m not even using WIFI, just 3G .. Anyhow, fantastic
    blog!

  18. Excellent blog! Do you have any suggestions for aspiring writers?
    I’m hoping to start my own site soon but I’m a little lost on everything.
    Would you advise starting with a free platform like WordPress or go for a paid option?
    There are so many options out there that I’m totally confused ..
    Any recommendations? Appreciate it!

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>