kayue/WordpressBundle

Make bootstrapping Wordpress optional (if already loaded)

kayue opened this issue · 6 comments

Currently WordpressBundle will force the Symfony app to load wp-load.php to make wp_get_current_user() available, however this is not needed if Symfony app is loaded within a Wordpress.

I have already figured out a way to initiate a Symfony app within a Wordpress theme (or plugin). Simply place the following code to the top of a Wordpress theme's functions.php :

<?php

require_once '/path/to/symfony/app/bootstrap.php.cache';
require_once '/path/to/symfony/app/AppKernel.php';

use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpFoundation\Request;

/**
 * Send response from Silex if available and page not found in Wordpress.
 *
 * @see /wp-includes/functions.php
 */
add_filter('status_header', function($status){  
    $kernel = new AppKernel('dev', true);
    $kernel->loadClassCache();

    if('Not Found' === substr($status,strlen('Not Found') * -1)) {
        $response = $kernel->handle(Request::createFromGlobals());

        if(404 !== $response->getStatusCode()) {
            // Send the page from Silex.
            $response->send();

            // We have got what we want. Exit from Wordpress.
            exit();
        }
    }

    return $status;
});

This way we can use most of the template function (if not all) in Symfony.

I have created a Wordpress Twig extension (https://github.com/kayue/WordpressBundle/blob/master/Extensions/WordpressTwigExtension.php).

Created a branch in here: https://github.com/kayue/WordpressBundle/tree/feature/%234-bootstrapping

Already completed the following:

  • A short_init option, false by default.
  • Won't load Wordpress again if already loaded.

You said:
"This way we can use most of the template function (if not all) in Symfony."

But, the code sample is the other way around (Symfony2 in Wordpress). Is there an overview of the features that this bundle adds to a basic Symfony2 installation? How can I execute the Wordpress get_footer() function in my Symfony Twig template?

Another comment to the bootstrapping; is the bootstrapping as it happens here similar to what is described in http://inchoo.net/wordpress/twig-wordpress-part2/

Have you tried {{ wp.getFooter() }} ?

On Saturday, January 19, 2013, Rvanlaak wrote:

You said:
"This way we can use most of the template function (if not all) in
Symfony."

But, the code sample is the other way around (Symfony2 in Wordpress). Is
there an overview of the features that this bundle adds to a basic Symfony2
installation? How can I execute the Wordpress get_footer() function in my
Symfony Twig template?


Reply to this email directly or view it on GitHubhttps://github.com//issues/4#issuecomment-12431484.

The installation from Packagist is not working correct yet, I get the following error:

[Symfony\Component\Config\Definition\Exception\InvalidConfigurationException]
The child node "wordpress_path" at path "hypebeast_wordpress" must be configured.

Script Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache handling the post-install-cmd event terminated with an exception

[RuntimeException]
An error occurred when executing the "'cache:clear --no-warmup'" comma
nd.

After a succesfull deployment and configuration I now get the following error:

Fatal error: Call to a member function add_shortcode() on a non-object in /home/scribbr/sites/scribbr/web/wp-content/plugins/contact-form-7/includes/shortcodes.php on line 174

I've added Wordpress to the /web folder and I'm adding all classes to the Symfony autoloader:

"autoload": {
"psr-0": { "": "src/" },
"classmap": ["web/wp-content/"]
},

Any clue?

SOLVED: I've found the answer on http://wordpress.org/support/topic/plugin-contact-form-7-cf7-shortcode-error
It is a terrible solution, I had to add a global parameter. Maybe this bundle should contain an extra config parameter, similar to the "short_init" param, but something like a "init_plugins" to whether or not load the plugins.