Yii yang tenar sebagai framework pengembangan aplikasi web dengan konsep MVC dan WordPress sebagai sebuah framework CMS/Blogging terpopuler ternyata bisa disatukan. Contoh kasus, misalnya WordPress digunakan sebagai frontpage sebuah Sistem Informasi, dan aplikasi berbasis Yii untuk menangani berbagai kebutuhan sistem informasi. Aplikasi Yii tersebut dikembangkan sebagai plugin WordPress, dan cukup menangani kebutuhan khusus saja, untuk urusan penerbitan konten, berita, user dan lain-lain serahkan saja pada core milik WordPress.

Setelah mencari dan memahami beberapa referensi yang saya dapat, berikut langkah untuk mengintegrasikan aplikasi Yii ke dalam WordPress sebagai sebuah plugin.

1. Install Yii ke dalam direktori plugin WordPress, /wp-content/plugins/pluginyii

2. Edit konfigurasi Yii, dan ubah beberapa baris seperti basepath, nama aplikasi, preload dll seperti saya contohkan berikut:

<?php

// uncomment the following to define a path alias
//Yii::setPathOfAlias('wp-includes', ABSPATH . 'wp-includes');

// This is the main Web application configuration. Any writable
// CWebApplication properties can be configured here.
$plugins = parse_url(WP_PLUGIN_URL);
return array(
        'basePath'=>dirname(__FILE__) . DIRECTORY_SEPARATOR.'..',
        //'baseUrl'=> WP_PLUGIN_URL,
        'name'=>'Plugin Yii untuk WordPress',
        //'aliases'=>array(
         //  'wp-plugins'=> ABSPATH . 'wp-content' . DIRECTORY_SEPARATOR . 'plugins',
   // ),
        // preloading 'log' component
        'preload'=>array('log'),

        // autoloading model and component classes
        'import'=>array(
                'application.models.*',
                'application.components.*',
                //'wordpress.*',
        ),
        'defaultController'=>'site',
        'modules'=>array(
                // uncomment the following to enable the Gii tool
                /*
                'gii'=>array(
                        'class'=>'system.gii.GiiModule',
                        'password'=>'Enter Your Password Here',
                ),
                */
        ),

        // application components
        'components'=>array(
                'user'=>array(
                        // enable cookie-based authentication
                        'allowAutoLogin'=>true,
                ),
                'request'=>array(
                        //'class'=>'WPHttpRequest',
                        'baseUrl'=> WP_PLUGIN_URL . '/pluginyii',
                        'scriptUrl'=>  $plugins['path']  . '/pluginyii/yii.php',
                ),
                // uncomment the following to enable URLs in path-format
                'assetManager'=>array(
                'basePath'=>dirname(__FILE__) . DIRECTORY_SEPARATOR.'..\..\assets',
                        'baseUrl'=>$plugins['path'] . '/pluginyii/assets',
                        ),
                'urlManager'=>array(
                        //'urlFormat'=>'path',
                        'routeVar'=>'page',

                        /*'rules'=>array(
                                '<controller:\w+>/<id:\d+>'=>'<controller>/view',
                                '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
                                '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
                        ),*/
                ),
                /*
                'db'=>array(
                        'connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/testdrive.db',
                ),*/
                // uncomment the following to use a MySQL database
                /*
                'db'=>array(
                        'connectionString' => 'mysql:host=localhost;dbname=testdrive',
                        'emulatePrepare' => true,
                        'username' => 'root',
                        'password' => '',
                        'charset' => 'utf8',
                ),
                */
                'errorHandler'=>array(
                        // use 'site/error' action to display errors
            'errorAction'=>'site/error',
        ),
                'log'=>array(
                        'class'=>'CLogRouter',
                        'routes'=>array(
                                array(
                                        'class'=>'CFileLogRoute',
                                        'levels'=>'error, warning',
                                ),
                                // uncomment the following to show log messages on web pages
                                /*
                                array(
                                        'class'=>'CWebLogRoute',
                                ),
                                */
                        ),
                ),
        ),

        // application-level parameters that can be accessed
        // using Yii::app()->params['paramName']
        'params'=>array(
                // this is used in contact page
                'adminEmail'=>'luthfi@emka.web.id',
        ),
);

3. Pada file index.php milik Yii, coba tulis:

<?php
/**
 * @package Yii Test Plugin
 */
/*
Plugin Name: Yii Test Plugin
*/
// change the following paths if necessary

function yiiapp_init(){
        global $yii_app;

        $config = dirname(__FILE__).'/protected/config/main.php';
        $yii_app = Yii::createWebApplication($config);
        //$yii_app = Yii::createApplication('SiteController',$config);
        //Yii::setApplication($yii_app);

}

function yiiapp_admin_actions()
{
        global  $yii_app;
        add_menu_page("Yii", "Home", 1, 'site/', array(&$yii_app,"run"));
        add_submenu_page('site/', 'Yii', 'About', 1, 'site/page/view/about', array(&$yii_app,"run"));
        add_submenu_page('site/', 'Yii', 'Contact', 1, 'site/contact', array(&$yii_app,"run"));
        //add_submenu_page(basename(__FILE__), "Yii", "Contact", 3, __FILE__, array(&$yii_app,"run"));

}

global $yii_app;
$yii = dirname(__FILE__).'/../../../../yii/framework/yii.php';
// remove the following lines when in production mode
defined('YII_DEBUG') or define('YII_DEBUG',true);
// specify how many levels of call stack should be shown in each log message
defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',6);
require_once($yii);

if ( !defined( 'WP_AUTOLOAD_CLASSES' ) ) {
    define(
        'WP_AUTOLOAD_CLASSES',
        function_exists( 'spl_autoload_register' )
    );
}

if ( WP_AUTOLOAD_CLASSES ) {

        function wp_autoload( $class, $path = null ) {
                $classes = array('SimplePie' => ABSPATH . 'wp-includes/class-simplepie.php', 'WP_User_Search' => ABSPATH . 'wp-admin/includes/user.php');

                // Being called by PHP's autoloader
                if ( is_null( $path ) ) {

                        if ( isset( $classes[$class] ) && !class_exists($class,false)) {
                                include_once( $classes[$class] );
                        } else {

                        }
                        return;
                }

                // Being called by us
                // $classes[$class] = $path;
                // spl_autoload_unregister( 'wp_autoload' );
        }

    // Register it
   spl_autoload_register( 'wp_autoload' );
} else {
        function wp_autoload( $class, $path ) {
                require_once( $path );
        }
}

class YiiAutoLoad extends YiiBase {

         public static function autoload($className)
         {
                if(!class_exists($className,false))
                        parent::autoload($className);
         }
}

Yii::registerAutoloader(wp_autoload);

add_action('admin_init', 'yiiapp_init');
add_action('admin_menu', 'yiiapp_admin_actions');
//add_action('');
spl_autoload_unregister(array('YiiBase', 'autoload'));
spl_autoload_register(array('YiiAutoLoad','autoload'));

4. Buat file yii.php sebagai aplikasi utama. Contoh:

<?php
/**
 * @package Yii Test Plugin
 */
/*
Plugin Name: Yii Test Plugin
*/
// change the following paths if necessary
$yii=dirname(__FILE__).'/../../../../yii/framework/yii.php';
$config=dirname(__FILE__).'/protected/config/main.php';

// remove the following line when in production mode
defined('YII_DEBUG') or define('YII_DEBUG',true);

require_once($yii);
Yii::createWebApplication($config)->run();

Referensi:

– YiiFramework : www.yiiframework.com
– Integrating Yii with WordPress: http://www.yiiframework.com/wiki/202/integrating-yii-with-wordpress/