Use Wordpress and Symfony together (English)

2013-12-29

  symfony    english    wordpress 

While working at Ekino, I had the opportunity to work on a sandbox to mix Wordpress and Symfony together and this work is now open-sourced and available on the Ekino's Github account.

With these tools, you will have the availability to:

  • Add custom Symfony routes to your Wordpress blog,
  • Manipulate Wordpress from Symfony,
  • Use Symfony services inside Wordpress,
  • Dispatch Symfony events from Wordpress hooks,
  • Do all other things that you can imagine.

This sandbox comes with a Symfony bundle and a Wordpress plugin.

Installation steps for both tools are explained on the Github project pages:

Use Symfony from Wordpress

Inside your Wordpress blog, you have the possibility to call any service registered in the Symfony dependency injection container by this way:


$service = symfony_service('my.custom.symfony.service');
$service->doSomething();

Pretty cool and easy, no? Wait, that's not finished.

You can also dispatch Symfony events via the EventDispatcher component from example while triggering Wordpress hooks by this way:


/**
 * User login hook
 */
add_action('wp_login', 'ekino_wordpress_symfony_hook_wp_login', 10, 2);

/**
 * Dispatch Wordpress user login hook on Symfony event dispatcher
 *
 * @param string   $user_login Username of user logged in
 * @param \\WP_User $user       Wordpress user object
 *
 * @see http://codex.wordpress.org/Plugin_API/Action_Reference/wp_login
 */
function ekino_wordpress_symfony_hook_wp_login($user_login, $user) {
    $event = new \\Ekino\\WordpressBundle\\Event\\WordpressEvent(array(
        'user_login' => $user_login,
        'user'       => $user
    ));

    symfony_event_dispatch('ekino.wordpress.user_login', $event);
}

Our Symfony event below will be fired each time a user logged in into our Wordpress blog.

Now, let's have a look to the other side.

Use Wordpress from Symfony

Before all, you can add custom routes with a custom Symfony action that will be catched before rendering your Wordpress blog, so you can have for instance a second application next to your Wordpress blog:


class AuthenticationController extends Controller
{
    /**
     * My custom route action
     *
     * @Route("/my-custom-route", name="custom_route")
     */
    public function customAction()
    {
        ...
        return $this->render('MyCustomBundle:Custom:template.html.twig');
    }

You can also manipulate Wordpress database from Symfony by using Doctrine managers available in the bundle:


$postManager = $this->get('ekino.wordpress.manager.post');
$userManager = $this->get('ekino.wordpress.manager.user');

$user = $userManager->find(2);

$post = $postManager->find(1);
$post->setAuthor($user);

$postManager->save($post);

In this example, we modified the author of article identified by id #1 with user identified by id #2.

To go further...

Please note that the Symfony WebProfiler is also available on development environments so you can use it easily to debug things.

I have built a demonstration instance available here: http://wordpress.composieux.fr.

For more information do not hesitate to contact me and/or contribute to Github projects.

Comments