Recently I had to develop bunch of complex features on WordPress based website, and the time was running out very quickly. Because I use Zend Framework on regular basis, I decided to incorporate it to my WordPress instance to speed up development.

The task is really simple and can be achieved by creating small plugin.

Solution
Firstly, we have to create zf-integration directory (of course you can name it as you wish) inside WordPress plugins directory. After that, we have to create lib directory , and copy Zend library into it. (remember to copy Zend directory from Zend Framework package)

Next we need to create zf-integration.php.

<?php
/*
Plugin Name: Zend_Framework integration
Description: Zend_Framework integration
Author: author
Version: 1.0
Text Domain: Zend_Framework integration
*/
set_include_path(
	implode(PATH_SEPARATOR, array(
		get_include_path(),
		realpath(__DIR__.'/lib')
	)));
 
require_once realpath(__DIR__.'/lib/Zend/Loader/Autoloader.php');
 
$autoloader = Zend_Loader_Autoloader::getInstance();

As you can see above, everything is about including proper library path and activating Zend Framework autoloading.

Now we can activate plugin in WordPress admin page, and use Zend Framework classes inside WordPress themes.

16 Responses to “Integrating Zend Framework with WordPress”

  1. Alan Seiden says:

    Thank you for this tip. I didn’t know that WordPress and Zend Framework could work so well together.

  2. Tobias says:

    Amazing that it is so easy. Thanks for that thoughts!

  3. Nice.

    Having the ZF lib available certainly makes WP work a lot more palatable. Nice to see that it’s as easy as creating and installing a a plugin that sets an include path and initiates the autoloader.

    Thanks!

  4. Daniel says:

    That the way how the ZF works :-)

  5. Kevin Schroeder says:

    It really is easy. I recently moved my website from a custom written blog to WordPress. But there was a lot of stuff I needed to do that WordPress doesn’t do (http://www.eschrade.com/page/added-php-5-3-job-queuing-to-my-wordpress-instance/). I was able to copy over most of my ZF stuff, including some PHP 5.3 code that I had written and integrate it as a plugin for WordPress. Part of what I had done, too, was integrate Zend_View in with my own template scripts which also tied into the WordPress engine.

  6. I use ZF and WP, but NEVER thought of using the 2 together.

    I like do like the concept of having it as a plugin.

    Can you provide a scenario where you leveraged ZF to do things that either weren’t possible or troublesome to do in WordPress?

  7. Dave Shaffer says:

    First let me say thank you for this article. I am new to Zend Framework. I have been working with Worpress for a couple years and am familiar with its structure but have not written a plug in. I followed your example but when I activate the plug in the sidebar shows with whitespace between the items and the middle shows after the last sidebar item. I deactivate it and all is normal.
    Here’s what I did.
    I created the plugin directory and copied the /lib/Zend to it.
    I created a new file and copied the plug in code to it.
    I added the tags. and save the plug in to the name you suggested.

    I am testing this on my local xampplite instance.
    My WordPress is 3.1.3
    PHP is 5.1.3

    In looking at what is sent to the browser, the plug in code is before the <!DOCTYPE statement and the rest of the admin page code.

    Do you have any suggestions as to what I am doing wrong?

    Thanks for your help

  8. Tristanmin says:

    Only this code worked for me.

    set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__) );
    require_once ‘Zend/Loader/Autoloader.php’;

    $autoloader = Zend_Loader_Autoloader::getInstance();

  9. I have experience with Zend Framework and all I can say is that it does its job really, really well. If you encounter problems, the solution is a quick Google search away.

  10. Ruben says:

    Greetings!

    I tried your sample and am getting the following error when I try to active the plugin:

    Plugin could not be activated because it triggered a fatal error.

    Parse error: syntax error, unexpected T_REQUIRE_ONCE in /home/mbba2005/public_html/teamneweden/wp-content/plugins/zf-integration/zf-integration.php on line 15

    Any ideas?

  11. If you’re interested in even more integration, like Sharing DB connections, caches, authentication, configuration, etc. out of the box — you might be interested in my open source Vulnero plugin. Basically you have a fully functional Zend Application with bootstrapped resources for rapid development. Source is on GitHub or vulnero [dot] com.

Leave a Reply →