Yii, WordPress Integration.

This is a quick post about a simple way to integration a WordPress blog with the Yii Framework. First we need a working installation of the Yii Framework. There is a lot of documentation here around this so I will not go into this today.

The following steps will provide a easy way to combine headers and the like between the two systems without having to duplicate a lot of code and maintaining those changes, it also gives an easy way to make use of the cool functionality provided in the Yii Framework and  the easy administration of WordPress. You will need to have a working knowledge of Yii and WordPress to following this post.

Wordpress Yii

Step 1

We will start with downloading the placing the WordPress files within the Yii Framework project . You can get the latest copy here.  Rename your installation to the desired name.

 Step 2

Next we need to install wordpress. I will not run through this installation as there is plenty of other documentation on this. After Installation we will need to create a new theme with the basics index.php, comments.php, functions.php, single.php, style.css and content.css which I copied from the twentythirteen default theme. Activate your new theme.

Step 3

Within index.php of the wordpress installation you will need to define a couple of new constants. This installation presumes that the yii installation is in your docroot folder.

define('DW_YII_BASE',dirname(__FILE__).'/../'); // This is used in the function we will create in wordpress
define('DW_YII_CONFIG',dirname(__FILE__).'/../protected/config/main.php'); // This is used in the config
define('DW_BASE_PATH',dirname(__FILE__)); // This is used in the config.

Step 4

Within the config file of the Yii installation add the following:

defined('DW_BASE_PATH') or define('DW_BASE_PATH',dirname(__FILE__).DIRECTORY_SEPARATOR.'..');
defined('DW_RUNTIME_PATH') or define('DW_RUNTIME_PATH',dirname(__FILE__).DIRECTORY_SEPARATOR.'../runtime'); 
return array(
    ...
    'basePath'=>DW_BASE_PATH . '/../protected',
    'runtime'=>DW_RUNTIME_PATH,
    ...
    'components'=>array(
        ...
        'assetManager' => array(
            ...
            'basePath' => DW_BASE_PATH.'/../assets',
	    'baseUrl' => '/assets',
            ...
         ),
         'themeManager' => array(
             ...
             'basePath' => DW_BASE_PATH.'/../themes',
             'baseUrl' => '/themes',
             ...
         ),
         ...
    )
);

Step 5

Create a controller in your yii installation called WpController. Add a redirect to stop this controller being access directly.

class WpController extends Controller {
    public $layout = '//layouts/column2';
    public function init() {
        if (!function_exists('wp')) {
            $this->redirect('/blog');
        }
    }
    public function actionIndex() {
        $this->render('index');
    }
    public function actionSingle() {
        $this->render('single');
    }
}

Step 6

Create the following new functions in your WordPress theme that will bootstrap the Yii Framework.

if (!function_exists('boostrap_yii')) {
    function bootstrap_yii() {
        require_once(DW_YII_BASE);
        Yii::createWebApplication(DW_YII_CONFIG);
    }
}
if (!function_exists('load_yii_controller')) {
    function load_yii_controller() {
        if (!class_exists('Yii')) {
            bootstrap_yii();
        }
        Yii::import('application.controllers.WpController');
        Yii::app()->controller = new WpController('wp');
    }
}

if (!function_exists('load_yii_view')) {

    function load_yii_view($id, $params = array()) {
        if (!class_exists('Yii') || !isset(Yii::app()->controller)) {
            load_yii_controller();
        }
        Yii::app()->controller->action = new CInlineAction(Yii::app()->controller,$id);
        $action = "action".ucfirst($id);
        Yii::app()->controller->$action($params);
    }
}

Step 7

now to load a view in your Controller you can do the following from any of your template files

load_yii_view('index', array('data'=>'test'));

you can also call any functions from within wordpress files as well after rendering a view or boostraping using the yii_boostrap() function.

Yii::app()->controller->name;
Yii::app()->params->adminEmail;

Also any wordpress functions can be accessed within the views.

I hope this helps with integrating your blog into your existing system, if you notice any bugs or issues with this method please send us a message through our contact us form.

Leave a Reply

Your email address will not be published. Required fields are marked *