Integrating Zend_View with WordPress

I just read a blog post about integrating Zend Framework and WordPress and figured I’d add some commentary of my own, since I’ve done something similar.  In my ESchrade plugin I use Zend_View to render some of my admin pages, though there is no reason why you couldn’t use it elsewhere, as long as the plugin has already been loaded.

Getting Zend Framework set up with WordPress is super simple.  Assuming you have Zend Framework already in your include path all you need to do is put the autoloader in your plugin file.

Here is a very slimmed down version of my plugin class that integrates with WordPress.  From the Zend_View perspective, all it really does create a new view object and set it up.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
namespace com\eschrade;
 
class Application
{
/**
*
* @var \Zend_View
*/
private $view;
public function __construct()
{
$this->view = new \Zend_View();
$this->view->setScriptPath(
realpath(__DIR__ . '/../../../templates')
);
$widget = new Popular($this->view);
wp_register_sidebar_widget('popular-posts', 'Popular Posts', array($widget, 'handle'));       
}
 
public function setView(\Zend_View_Interface $view)
{
$this->view = $view;
}
 
public function getView()
{
return $this->view;
}
 
public function handleJobs()
{
$this->view->jobs = new Jobs();
$this->view->jobs->setAction('/wp-admin/plugins.php?page=eschrade-config&action=Run Job');
$this->view->jobs->setMethod('POST');           
$this->view->jobs->setView($this->view);
// cut
echo $this->view->render('admin/runjobs.phtml');
}
}

Obviously you don’t have the view renderer set up, or some of the controller helpers that are so useful, but if you want access to anything that Zend_View has to offer it is thuper eathy to do.

1 comments
Matthew Weier O'Phinney
Matthew Weier O'Phinney

BTW, you can simplify your code a little using import statements. I'd suggest the following:

use Zend_View as ViewEngine,
Zend_View_Interface as View;

This will make the code slightly easier to read, document your dependencies, and allow for later substitutions (e.g., when you start using ZF2 in a few months). :)

Trackbacks

  1. […] Schroeder has a recent post to his blog showing how he integrated Zend_View into a WordPress install for his blog. It was as a part of a plugin he uses on some of his internal pages. I just read a […]

  2. […] Schroeder has a recent post to his blog showing how he integrated Zend_View into a WordPress install for his blog. It was as a part of a plugin he uses on some of his internal pages. I just read a […]

  3. abcphp.com says:

    Integrating Zend_View with WordPress…

    I just read a blog post about integrating Zend Framework and WordPress and figured I’d add some commentary of my own, since I’ve done something similar. In my ESchrade plugin I use Zend_View to render some of my admin pages, though there is no reason…

  4. […] Integrating Zend_View with WordPress […]

Post Navigation

Web Analytics