Yii + WordPress = <3, or The Fascinating Experiment of Getting Frankenstein

  • Tutorial
Hello. I am Denis Melsky. I work with PHP Developer in DataArt. My area of ​​expertise is Web Development and Linux. Today I would like to talk with you about "crossing a hedgehog with a snake."



At first glance, it might seem like crazy and, in a way, an architectural crutch. But, if you look from the other side, this is one of the ways out of the often encountered deadlock situation: the customer wants WordPress and does not recognize anything else. Most likely, he heard something like this, google it, saw it, coder John (well, or Vasya) advised the neighbor to the guru.
And programmers flatly refuse to pull on the blog engine, with, to put it mildly, not the best architecture, the functionality of, say, an online store, a forum, and in addition - a REST API for partners of this store, and on top - a bunch of crowns with business logic.

Let's set the task to find such a way out to satisfy all participants of the problem, and so that our product starts up and works normally.

One of the options I found doing research on this topic is the crossbreeding of WordPress and Yii.

Let the party begin

Well, let's try to master some of this in the code.

Let's start by downloading the Yii framework. I think that composer for such experiments is a bust - because of the customization of the paths and the architecture of the entire assembly, without a composer, we will have more opportunities for experiments, that is, we can safely download from our own site .

We produce the same simple manipulation with WP .

Next, we arm ourselves with auxiliary manuals:

We go along the first manual, to which I want to add a few comments and comments.

We install WP, we place everything in such an architecture:

First, consider the key lines of our index.php.

define(‘WP_USE_THEMES’, true);
$wp_did_header = true;

Here we can observe the beginning of the WP connection in the general picture of what is happening.

We create the Exception Handler class, the essence of which on 404 exception yii framework is to transfer WP power.

We make default .htaccess for Yii which gives the chance to do beautiful URLs.

RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php

We write in the Yii config 'showScriptName' => false and uncomment the default stock delivery rules.

Next, according to the above manual, create a WpController and View to it.

Another interesting point that cannot be ignored: in wordpress configs at the DB level there is a setting in the `wp_options` -“ siteurl ”label, it needs to be changed to yourdomain / wordpress . This can also be changed in the admin panel WP
Settings -> General -> WordPress Address (URL).

I cannot but mention an interesting point: in the main layout of Yii we place the structure of the interior of our theme from WP.

We are already close to victory: we connect gii (anyone who is not in the know can be read here ).

In gii, we can create a CRUD model and get exactly what we wanted - the crossed code of Yii and WP.

It's alive!

In this screenshot, we can see the beautiful Yii CRUD using CGridView.

For authorization, we’ll try this method everywhere .

In the root, create the wordpress folder and transfer the insides of WP itself there.

As a result of my reception on the topic, we obtain the following conclusions:

  • Frontend WP can be passed through Yii with a fine-tuning of the theme, and as a result, we can get custom modules on Yii that will live harmoniously with WP. In addition, it is possible to synchronize users from WP to Yii.
  • I failed to connect Backend WP to Yii harmoniously, that is, the general admin panel for standard WP modules and custom Yii modules is not the best implementation option. This is due to the fact that in backend-e WP a very non-standard architecture is not very suitable for integration. But these are the results of my review, not pretending to be true in the last resort.
  • As a result, we can develop an application where frontend WP will be harmoniously combined with custom Yii modules. In backend WP, ​​we can develop custom plugins and load Yii as one implementation option. And even, I’m not afraid of this option, to render some Yii modules through an iframe inside the WP plugin.

PS I want to wish everyone the right choice of tools for solving a certain range of tasks for which they were created, but also not to lose the excitement of

PPS experimentation. A few links on the topic:

Examples of similar approaches for implementing the idea in some also use slightly different yii customizations.


www.yiiframework.com/wiki/322/integrating-wordpress-and-yii-still-another-approach-using -yii-as-the-router-controller




Also popular now: