If you are involved in active web application development, then chances are you have heard about both WordPress and Zend Framework. Learning Zend Framework can be tedious and time consuming task (but it sure pays of, this goes for all other PHP frameworks out there). Anyhow, lot of our projects use WordPress. Not because we love it so much, but because our clients love it. Since we do ecommerce development, our prime tool when it comes to online stores is… MAGENTO. Magento, on the other hand comes with Zend Framework implemented. So, if you are working on a site that uses both Magento and WordPress then it seems logical to try and use Zend Framework components (classes) inside your WordPress templates (in case you are working on something more than just styling the WordPress).
In this article, I’ll show you how easy is to implement Zend Framework into WordPress and use its components across WordPress templates (or even plugins).
To use the Zend Framework components inside any of your WordPress templates we need to set autoloading of entire Zend Framework. This will help us avoid using unnecessary includes and/or requires. To set the autoloading you simply open index.php file from root of your WordPress installation and write down the following:
<?php /** * Front to the WordPress application. This file doesn't do anything, but loads * wp-blog-header.php which does and tells WordPress to load the theme. * * @package WordPress */ /** * Tells WordPress to load the WordPress theme and output it. * * @var bool */ define('WP_USE_THEMES', true); /** * Include and auto-load Zend Framework into the WordPress CMS */ set_include_path('.' . PATH_SEPARATOR . './library' . PATH_SEPARATOR . get_include_path()); require_once 'library/Zend/Loader.php'; Zend_Loader::registerAutoload(); /** Loads the WordPress Environment and Template */ require('./wp-blog-header.php'); ?>
Notice the /library? In my case, /library folder is located inside the root of WordPress installation. This is it. You can now call any of Zend Framework classes inside your WordPress template. Here is an example of single.php template file:
<?php $frontendOptions = array( 'lifetime' => 45, // in seconds 'automatic_serialization' => true ); $backendOptions = array( 'cache_dir' => '/home/branko/Public/articles/cache/' // Directory where to put the cache files ); // getting a Zend_Cache_Core object $cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions); ?>
You must admit, this was really easy.
