Search code examples
phpwordpresszend-frameworkwordpress-themingzend-layout

How to use a Zend Layout as Wordpress Theme?


I have written a Zend application and have included Wordpress for blogging. When I first installed Wordpress I themed it so that it used the same header etc as the main application. I have since redone the main theme twice and had to redo the Wordpress theme to match. Is there a way for Wordpress to use my Zend layout? My first thoughts are to break up my layout into header/footer files and include them from Wordpress using the full paths. Although it would work, it is far from ideal (I would prefer to keep the layout file in one piece).


Solution

  • For many websites you may need to install Wordpress for blogging integration. When it comes to skinning the blog you are pretty much limited to copying the html from your Zend_Layout into the Wordpress header.php and footer.php files. This is duplication, and if you make any changes to your layout, you will need to update the blog theme as well. Well, there is another way!

    Alter Your Zend Application

    Make a separate bootstrap file for your Zend application (for example by following this guide: Access Zend Application Resouces from Other Applications).

    <?php
    // Define path to application directory
    defined('APPLICATION_PATH')
        || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
    
    // Define application environment
    defined('APPLICATION_ENV')
        || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
    
    // Ensure library/ is on include_path
    set_include_path(implode(PATH_SEPARATOR, array(
            realpath(APPLICATION_PATH . '/../library'),
            get_include_path(),
        )));
    
    /** Zend_Application */
    require_once 'Zend/Application.php';
    
    // Create application, bootstrap, and run
    $application = new Zend_Application(
            APPLICATION_ENV,
            APPLICATION_PATH . '/configs/application.ini'
    );
    $application->bootstrap();
    

    In your index.php file, to run your application:

    include('bootstrap.php'); //where bootstrap.php is the file of the new bootstrap file
    $application->run();
    

    Wordpress Magic

    Now comes the Wordpress magic.

    Get Zend Layout

    In your new Wordpress theme folder, create a file named 'Zend_Layout.php' and copy/paste this code into it:

    <?php
    //load Zend_Layout
    $layout = new Zend_Layout();
    
    //add the blog's stylesheet to the header
    $view = $layout->getView();
    $view->headLink()->appendStylesheet(get_bloginfo( 'stylesheet_url' ));
    
    // Set a layout script path:
    $layout->setLayoutPath(APPLICATION_PATH . "/modules/default/views/scripts");
    $layout = $layout->render();
    

    Header

    Change the header.php file to:

    <?php
    include('zend_layout.php');
    echo substr($layout, 0, strpos($layout, '<div id="container">'));
    ?>
        <div id="container">
    

    This will load the $layout variable from the previous script and echo everything up to your main container div. Footer

    The footer.php is similar:

        </div><!-- #main -->
    <?php
    
    include('zend_layout.php');
    echo substr($layout, strpos($layout, '<div id="footer">'));