Can I integrate a Zend-Framework powered web application into a wordpress site?

I have a project in which I want to be able to call wp_list_pages() on a page that also uses the Zend Framework to power some complex interfaces manages custom data outside of wordpress.

This page should also redirect the user to the wordpress login screen if they’re not already logged in with the appropriate level of authorization.

Read More

How would this work at a high level, i.e. do I need to edit the wordpress bootstrap file to conditionally implement the custom interface based on a specific URL or something, but still include certain files to be able to call wp_list_pages() on that custom interface?

Related posts

Leave a Reply

6 comments

  1. I’ve developed a couple of WordPress plugins, and I’ve found it’s really easy to extend. Haven’t worked with Zend though.

    You should check the WordPress plugin api. Mostly the part about actions, filters and hooks: http://codex.wordpress.org/Plugin_API

    You can even override some functions (not sure if wp_list_pages() is overridable).

    It’s pretty well documented, and there’s a large developer community behind it on IRC, forums, etc.

  2. Thanks Fernando.

    I just read this thread which suggests that you can use Zend in any script by just including:

    require_once('Zend/Loader.php');
    Zend_Loader::registerAutoload();
    

    So given that all I need to use Zend for is on one page, can I just include that code in a custom template file that I assign to the appropriate page in the navigation? If I used javascript to submit the form via XHR, the requested URL would take the form ‘/controller/action’ – but Zend wouldn’t know the controller directory.

    Could I put Zend code into the wordpress bootstrap, i.e. the above code plus the frontController configuration, and then use Zend wherever however?

  3. So I’ve created a page in WordPress and a custom template for that page, in which I’ve placed the following Zend Framework code:

    require_once('Zend/Loader.php');
    Zend_Loader::registerAutoload();
    
    $db = Zend_Db::factory('Pdo_Mysql', array(
        'host'      => 'localhost',
        'username'  => 'username',
        'password'  => 'password',
        'dbname'    => 'dbname'
    ));
    Zend_Db_Table::setDefaultAdapter($db);
    
    class Users extends Zend_Db_Table_Abstract {
        protected $_name = 'wp_users';
    }
    
    $users = new Users();
    $users = $users->fetchAll()->toArray();
    
    print_r($users[0]['user_login']);
    

    This all works fine, so it’s clearly possible to use Zend in conjuction with WordPress at least to some extent.

    It’s becoming apparant that the problem is about who controls the URL rewriting, or the routing, or the bootstrapping (not sure of the correct terminology). If I were to put the end of the above code, starting $users = new Users();, into a controller as follows:

    class UsersController extends Zend_Controller_Action {
        function getUserAction() {
            $this->_helper->viewRenderer->setNoRender();
    
            $users = new Users();
            $users = $users->fetchAll()->toArray();
    
            echo $users[0]['user_login'];
        }
    }
    

    How would I then call that function? My intention would be to call it from javascript via an XHR request in response to an event on the page, but requesting the URL ‘index.php/Users/getUser/’ returns ‘No input file selected’. Trying to access the URL http://www.domain.com/Users/getUser/ produces a WordPress 404 page.

    Is there a way around this? It doesn’t just apply to wordpress, of course – I expect it applies to any existing application that rewrites/routes requests via a bootstrap.

  4. I guess you could do that, just import the framework into the one page you need it for. I don’t know how Zend works, but check the paths as to where to put your directories so that Zend finds them.As I said I guess you could do that, just experiment and tell us how it went!

    Beware of name conflicts for functions and/or variables, this shouldn’t be much of a problem coming from such popular products as WordPress and Zend though… (which should be theoretically well coded)

  5. I guess you could do that, just import the framework into the one page you need it for. I don’t know how Zend works, but check the paths as to where to put your directories so that Zend finds them.As I said I guess you could do that, just experiment and tell us how it went!

    Beware of name conflicts for functions and/or variables, this shouldn’t be much of a problem coming from such popular products as WordPress and Zend though… (which should be theoretically well coded)

  6. I’ve built a plugin for wordpress that has a similar goal to yours, more modeled on CodeIgniter though. Not knowing Zend terribly well, I think this should help:

    Make a file named routes.php in your plugins directory with the following code:

    add_action( 'init', 'add_custom_urls' );
    
    function add_custom_urls(){
        global $wp, $wp_rewrite;
    
        $wp_rewrite->add_rule( '(.*)$', 'index.php?&cPath=$matches[1]', 'top' );
        $wp->add_query_var( 'cPath' );
    }
    

    Be sure to activate both plugins in your admin. These two files will allow you to catch the url before WordPress tries to figure out what to do with it. You can use regular expressions to have finer control over which pages to catch. You may have to delete the record in your _options db table where option_name = ‘rewrite_rules’ before this works.

    Next, make another plugin with the following code:

    add_action( 'template_redirect', 'bootstrap' );
    
    function bootstrap(){
        global $cPath;
        echo( "cPath : $cPath" );
        if( $cPath ){
            dosomethingwith( $cPath ); 
        }
    }
    

    Put all your code in the dosomethingwith() function. You’ll need to figure out if the url requested can me mapped to a zend controller, etc. http://www.domain.com/Users/getUser/ would give you $cPath = Users/getUser/ If successful, you’ll also probably want to die(), so once it is completed WordPress won’t try and take over again.