fullsix/wordpress-bundle

Let WordPress handle Symfony2's views

Installs: 156

Dependents: 0

Suggesters: 0

Security: 0

Stars: 3

Watchers: 0

Forks: 0

Open Issues: 0

Type:symfony-bundle

pkg:composer/fullsix/wordpress-bundle

dev-master 2013-03-16 17:26 UTC

This package is not auto-updated.

Last update: 2026-01-03 20:30:40 UTC


README

This is a work in progress

This bundle tries to integrate WordPress with Symfony2, in the way that WordPress is used to handle the views, and Symfony2 is used to handle the controller.

This bundle should be used when you want to add Symfony2 controllers to an existing WordPress website, for example. This is really basic but in fact it works quite well, and we have implemented this mechanism on various wordpress instances, one of them containing two blogs, using various languages (using the excellent WPML plugins).

It needs Symfony >= 2.2 in order to interpret Twig tags inside WordPress content.

Installation

Download and install the bundle

Add the following dependencies to your projects composer.json file:

"require": {
    # ..
    "fullsix/wordpress-bundle": "dev-master"
    # ..
}

Register the bundle in the kernel

<?php
// app/AppKernel.php

public function registerBundles() {
    $bundles = array(
        // ...
        new FullSIX\Bundle\WordPressBundle(),
        // ...
    );
}

Install WordPress

Install WordPress in your web directory and launch its configuration.

Install the WordPress plugin

A simple WordPress plugin is needed to interpret the Twig content inside WordPress. Install it from here.

Modify your app_dev.php

Edit your app_dev.php file and add this line at the beginning:

use FullSIX\Bundle\WordPressBundle\WordPressResponse;

And at the end, replace:

$kernel = new AppKernel('dev', true);
$kernel->loadClassCache();
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);

With:

$kernel = new AppKernel('dev', true);
$kernel->loadClassCache();
global $container, $response;
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$container = $kernel->getContainer();
if ($response instanceof WordPressResponse) {
    $container->enterScope('request');
    $container->set('request', $request, 'request');
    $targetUrl = $response->getTargetUrl();
    if (!empty($targetUrl)) {
        $_SERVER['REQUEST_URI'] = $targetUrl;
    }
    define('WP_USE_THEMES', true);
    require('./wp-blog-header.php');
} else {
    $response->send();
    $kernel->terminate($request, $response);
}

Once modified, replicate the changes to your app.php file and delete WordPress's index.php file. If you activate WordPress's url rewriting capabilities, you may need to comment modifications that WordPress automatically made to your .htaccess file.

Example of Symfony2 controller

/**
 * @Route("/test-page/")
 */
public function pageAction()
{
    return WordPressResponse::currentPage(array("var1" => "value1", "var2" => 2));
}

/**
 * @Route("/test-form/")
 */
public function formAction(Request $request)
{
    // Create form
    $builder = $this->createFormBuilder();
    $form = $builder
        ->add('var1', 'text', array("label" => "Variable 1", "required" => false, "constraints" => new NotBlank()))
        ->add('var2', 'text', array("label" => "Variable 2", "required" => false, "constraints" => new NotBlank()))
        ->getForm();
    $result = null;
    if ($request->getMethod() == 'POST') {
        $form->bind($request);
        $data = $form->getData();
        $result = $data["var1"]." ".$data["var2"];
    }
    return WordPressResponse::currentPage(array('form' => $form->createView(), "result" => $result));
}

This will create two routes, which will delegate their view to the respective WordPress page. For example, in the /test-page/ WordPress content, you can have:

This is my test page, with some really interesting content.
<ul>
<li>var1 = {{ var1 }}</li>
<li>var2 = {{ var2 }}</li>
</ul>

Which will display as:

This is my test page, with some really interesting content.
    * var1 = value1
    * var2 = 2

The /test-form/ page can have the following content:

Hi,

This is my test form.

<form method="post">
    {{ form_widget(form) }}
    <input type="submit" value="Ok" />
</form>
{% if result is not null %}
Result: {{ result }}
{% endif %}

This will display a basic form which will concatenate the two variables when submitted.