WordPress and Zend FrameWork Integration

    Yes, yes, that happens.
    Let's say you work in a distributed team. The designer sits somewhere in a distant mysterious country, and all you can get from him is templates made for WordPress. And you need to add far from the trivial functionality to the site. You can’t touch the core of WordPress, and the capabilities of the plugins do not suit you at all (maybe you lack the standard set of hooks, or maybe you just don’t like this engine, anything can happen).
    The first impulse is to take a suitable engine or framework (for example ZF), transfer the design to it and work quietly. But here's the catch, you will make new functionality and leave the project, and the designer and support will remain. And they do not know how and do not want to be able to do anything on ZF.

    If you are interested in how to get out in a similar situation, I invite you to cut

    The concept is this. All non-standard functionality is performed by ZF, and WP only displays the result, just as for example the contents of a single page or post. T.O. the designer can freely change the frame (header, footer, etc.) for any pages, the new frame will also apply to pages from ZF. Support will, as if nothing had happened, edit static pages and news, inserting where you need links to the ZF functionality.

    First of all, you need to make WP ask ZF for content of a certain type, for example / do / account / signup /
    Of course, we use the plugin system. Only here there is no hook that will make it possible not to pull the post from wordPressa, but to call the content through some function. If someone finds it, then tell me, please :) So we will treacherously use the hook that exists and is called before the content is prepared.

    Fortunately, http requests are parsed by the WP_Query class, and the contents of the page are prepared by the same class. WP uses one global object of this class. Without any remorse, we replace this object with our fake instance of the WP_Query class heir. Feel what I'm leading to? Correctly, the get_posts () function will be overridden.
    Personally, I first call the standard method, it will fill all global objects with standard data, pull out the default page from the database for display, in general it’s great, it would be difficult for me without it.
    Then I replace the default page content with the result from ZF. And of course the rule for .haccess: It is possible without it, then the urls for the ZF functional will be of the form /? Do = account / signup / Also, it is necessary to change ZF, it should not display its response, but return it. The returned HTML is needed without tags, and so turn off the layout or make it primitive, like That's just about it.

    define('ZF_MARKER', 'do');

    add_action('parse_request', 'zf_query_controller');

    function zf_query_controller() {
    parse_str($_SERVER['QUERY_STRING'], $params);

    if(isset($params[ZF_MARKER])) {

    global $wp_the_query;
    $q = new ZF_Query();
    $zf_uri = trim($params[ ZF_MARKER ], "/ ");
    $q->req_uri_zf = empty($zf_uri)? '/' : '/' . $zf_uri . '/';
    $wp_the_query = $q;

    class ZF_Query extends WP_Query
    public $req_uri_original = '';
    public $req_uri_zf = '';

    function copyFrom(WP_Query $wp_query) {
    $vars = get_object_vars($wp_query);
    foreach($vars as $name => $value) {
    $this->$name = $value;

    function &get_posts() {

    $post_zf = array(
    "ID" => 1,
    "post_author" => 1,
    "post_date" => '',
    "post_date_gmt" => '',
    "post_content" => '',
    "post_title" => '',
    "post_excerpt" => "",
    "post_status" => "publish",
    "comment_status"=> "open",
    "ping_status" => "open",
    "post_password" => "",
    "post_name" => "",
    "to_ping" => "",
    "pinged" => "",
    "post_modified" => "",
    "post_modified_gmt"=> "",
    "post_content_filtered"=> "",
    "post_parent" => 0,
    "guid" => "",
    "menu_order" => 1,
    "post_type" => ZF_MARKER,
    "post_mime_type"=> "",
    "comment_count" => "0",
    "ancestors" => array(),
    "filter" => "",

    $tmp = $_SERVER['REQUEST_URI'];
    $_SERVER['REQUEST_URI'] = $this->req_uri_zf;
    $post_zf['post_content'] = require_once ABSPATH . 'zf-app/public/index.php';
    $_SERVER['REQUEST_URI'] = $tmp;

    global $post;
    $post = (object)$post_zf;
    $this->posts = array($post);
    $this->post = $post;
    $this->post_count = count($this->posts);
    $this->current_post = 0;

    $this->is_single = 1;
    $this->is_page = 0;
    $this->is_404 = 0;
    $this->is_archive = 0;
    $this->is_home = 0;

    // эти 2 строки нужны, чтобы wordPress (особеннно в версии 3.x) не думал, что у него запросили нечто некорректное.
    global $wp_filter;

    return $this->posts;

    RewriteRule /?do/(.*) /index.php?do=$1 [L]

    $resp = $application->bootstrap()->getBootstrap()->run();
    return $resp->getBody();

    layout()->content ?>

    PS: Another point. if you need to teach WP how to deal with ZF content especially, then you can go to the Single.php template of the theme used and register it in the right place. For this, we needed the lines:
    post_type) : ?>


    $this->is_single = 1;
    $this->is_page = 0;
    $this->is_404 = 0;
    $this->is_archive = 0;
    $this->is_home = 0;

    Also popular now: