📜 ⬆️ ⬇️

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

Hello. I am Denis Melsky. I work PHP Developer in DataArt. My professional interests are Web Development and Linux. Today I would like to talk to you about "hedgehog crossing with a snake."

image

Intro


At first glance it may seem like madness and, in some way, an architectural crutch. But, if you look at it from the other side, this is one of the options for getting out of the frequently encountered deadlock: the customer wants WordPress and does not recognize anything else. Most likely, he heard something like this, googled, saw, coder John (well, or Vasya), advised the neighbor of the guru.
And programmers flatly refuse to pull on the blog engine, with, to put it mildly, not the best architecture, functionality, for example, an online store, forum, and in addition - REST API for partners of this store, and on top - another pack of kronas with business logic.
')
Let's set the task to find such a way out to satisfy all the participants in the problem, and to start our product and work normally.

One of the options that I found while conducting research on this topic is the crossover 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 the composer for such experiments is a bust - because of the customization of the paths and the architecture of the entire assembly, we will have more opportunities for experiments without a composer, i.e., we calmly download from our own site .

We make the same simple manipulation with WP .

Further we arm with auxiliary manuals:
Time
Two

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

Installing WP we place everything in this architecture:
image

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

define('WP_USE_THEMES', true); $wp_did_header = true; require_once('wordpress/wp-load.php'); 


Here we can observe the beginning of connecting WP into the overall picture of what is happening.

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

Making default .htaccess for Yii, which allows you to make 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 


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

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

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

I can not fail to mention an interesting point: in the main layout Yii we place the structure of the inside of our theme from WP.



We are already close to victory: we connect gii (who can’t read it here ).

In gii, we can both create a CRUD model and get what we really want at the output - the crossed code Yii and WP.



It's alive!

On this screen we can see a beautiful CRUD from Yii using a CGridView.

For authorization we will try this method everywhere.

At the root, create a wordpress folder and transfer the insides of the WP itself.

According to the results of my search, we get the following conclusions:



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 experimentation

PPS A few links on the topic:

Examples of similar approaches for the implementation of ideas in some also slightly different customizations are used yii.

www.yiiframework.com/wiki/382/integrating-wordpress-and-yii-working-out-the-details

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

www.yiiframework.com/wiki/213/integrating-wordpress-and-yii-yet-another-approach

www.yiiframework.com/wiki/202/integrating-yii-with-wordpress

www.yiiframework.com/wiki/144/run-an-yii-application-inside-an-wordpress-page

Source: https://habr.com/ru/post/236635/


All Articles