Hack Wordpress with Symfony Framework

Simple tutorial to inject Symfony Framework in your Wordpress site.

Download Symfony Installer

sudo curl -LsS https://symfony.com/installer -o /usr/local/bin/symfony  
sudo chmod a+x /usr/local/bin/symfony  

Install and move all wordpress files in web symfony directory.

symfony new wordpress-hack  
mv {WordpressDir}/* wordpress-hack/web  

Now configure your webserver. Do not forget to serve only app.php|app_dev.php, but any php file

Nginx

server {  
    server_name domain.tld www.domain.tld;
    root /var/www/project/web;

    #Add $uri/ :) 
    location / {
       try_files $uri $uri/ /app.php$is_args$args;
    }

    location ~ ^/app\.php(/|$) {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        internal;
    }

    # Yea :) I need to run Wordpress
    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    error_log /var/log/nginx/project_error.log;
    access_log /var/log/nginx/project_access.log;
}

Create App.php in app/

<?php

use Symfony\Component\Debug\Debug;  
use Symfony\Component\HttpFoundation\Request;

/**
 * Class App
 */
class App  
{
    /**
     * @var AppKernel
     */
    protected $kernel;

    /**
     * @return App
     */
    public static function boot()
    {
        static $instance = null;

        if ($instance === null) {
            $instance = new static();
        }

        return $instance;
    }

    /**
     * App constructor.
     */
    protected function __construct()
    {
        $this->kernel = $this->loadKernel(
            APP_ENV,
            (APP_ENV !== 'prod')
        );
    }

    /**
     * @param $env
     * @param $debug
     * @return AppKernel
     */
    public function loadKernel($env, $debug)
    {
        $kernel = new AppKernel($env, $debug);

        //Enable symfony debug
        if ($debug) {
            Debug::enable();
        }

        //Boot symfony kernel
        $kernel->boot();

        //Push master request
        $kernel->getContainer()->get('request_stack')->push(
            Request::createFromGlobals()
        );

        //Remove router listener
        $kernel->getContainer()->get('event_dispatcher')->removeSubscriber(
            $kernel->getContainer()->get('router_listener')
        );

        return $kernel;
    }

    /**
     * @return \Symfony\Component\DependencyInjection\ContainerInterface
     */
    public function getContainer()
    {
        return $this->kernel->getContainer();
    }
}

Edit your composer.json

"classmap": [
.....
"app/App.php"
.....
]

Edit web/wp-load.php

require_once __DIR__. '/../vendor/autoload.php';  
require_once __DIR__. '/../var/bootstrap.php.cache';

define('APP_ENV', 'dev');

$app = App::boot();


/** Wordpress legacy **/
/** Define ABSPATH as this file's directory */
......
......

Usage


Get Symfony service in your Wordpress code!!

$app->getContainer()->get('..');

//Twig :)?
$app->getContainer()->get('twig')->renderView('view.html.twig', []);

//Dispatcher :)?
$app->getContainer()->get('event_dispatcher')->dispatch(.., ..);

Fun with Event Listner

<?php

namespace AppBundle\EventListener;

use Symfony\Component\HttpFoundation\RedirectResponse;  
use Symfony\Component\HttpKernel\Event\GetResponseEvent;

/**
 * Class RequestListener
 * @package AppBundle\EventListener
 */
class RequestListener  
{
    /**
     * @param GetResponseEvent $event
     * @return GetResponseEvent
     */
    public function onKernelRequest(GetResponseEvent $event)
    {
        $event->setResponse(
            new RedirectResponse('http://www.google.it')
        );

        return $event;
    }
}

Update services.yml

services:  
   app.request_listener:
        class: AppBundle\EventListener\RequestListener
        tags:
            - { name: kernel.event_listener, event: kernel.request }

Edit wp-load.php

...
use Symfony\Component\HttpFoundation\RedirectResponse;

$app = App::boot();

//Kernel request event
$responseEvent = new \Symfony\Component\HttpKernel\Event\GetResponseEvent(
    $app->getContainer()->get('http_kernel'),
    $app->getContainer()->get('request_stack')->getMasterRequest(),
    \Symfony\Component\HttpKernel\HttpKernelInterface::MASTER_REQUEST
);

$event = $app->getContainer()->get('event_dispatcher')->dispatch(
    \Symfony\Component\HttpKernel\KernelEvents::REQUEST,
    $responseEvent
);


if ($event->getResponse() instanceof RedirectResponse) {  
    $event->getResponse()->send();
}

Now all pages will redirect to google!! Yea :)


Symfony debugging is enabled on all your wordpress code.

throw new \Exception('hello');

hello

and... have fun with symfony and wordpress!!