branko ajzele, senior developer / project manager

Posts / Articles

Zend components in WordPress (or any other CMS)

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.

  • http://condomunity.com/ Simon

    This is interesting Branko. It seems I am going to need this so I am going to give it a try. Btw. Just came across Magento also looks like a very promising piece of software. :)
    Cheers to Osijek.

  • http://condomunity.com Simon

    This is interesting Branko. It seems I am going to need this so I am going to give it a try. Btw. Just came across Magento also looks like a very promising piece of software. :)
    Cheers to Osijek.

  • patrick

    Hi Branko,

    Interesting posts here , thank you!

    Regarding WordPress & ZF , do you think there’s a way to do the opposite , namely add WordPress into ZF. As you mentioned in another post, clients love WordPress , that’s the good part… personally I always feel like I’m stepping back when learning WP. It seems that the knowledge I get from it is just so specific to the platform that I won’t be able to use it on anything other than WP.

    Now, if I could create ZF controllers that I would customize by using some of WP main functions, that would be a lot more fun! For this , I would just need a way to access the main functions called in the index.page of the default theme for instance…

    I could of course access WP’s database directly , but then the idea is not to rewrite the whole programme :)

    It’d be nice to hear if you have any ideas on the matter.

    Patrick

  • patrick

    Hi Branko,

    Interesting posts here , thank you!

    Regarding WordPress & ZF , do you think there’s a way to do the opposite , namely add WordPress into ZF. As you mentioned in another post, clients love WordPress , that’s the good part… personally I always feel like I’m stepping back when learning WP. It seems that the knowledge I get from it is just so specific to the platform that I won’t be able to use it on anything other than WP.

    Now, if I could create ZF controllers that I would customize by using some of WP main functions, that would be a lot more fun! For this , I would just need a way to access the main functions called in the index.page of the default theme for instance…

    I could of course access WP’s database directly , but then the idea is not to rewrite the whole programme :)

    It’d be nice to hear if you have any ideas on the matter.

    Patrick

  • Tinashe B Chipomho

    Hi this is a good article.
    Like the previous post, I am loooking for a way of intergrating my ZF application which comes with all the controllers and view scripts into WordPress or the other way round which ever way work.
    I have a website that is supposed to be running wordpress but also run this ZF application…

    any ideas on how to do that would be great.

  • Tinashe B Chipomho

    Hi this is a good article.
    Like the previous post, I am loooking for a way of intergrating my ZF application which comes with all the controllers and view scripts into WordPress or the other way round which ever way work.
    I have a website that is supposed to be running wordpress but also run this ZF application…

    any ideas on how to do that would be great.

blog comments powered by Disqus