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/
Incoming search terms:
- belajar yii
- yii dalam wordpress
- aplikasi yii
- tidak bisa buat login warning pada yiibase php
- sistem informasi yii
- share aplikasi yii
- pengertian gii(yii)
- abspath adalah
- pengertian BASEPATH di php
- merancang web dengan Yii
Tulisan Menarik Lainnya
- How to Enable Debugging on WordPress
- Types of WordPress Plugin
- How Plugins Interact with WordPress
- Penampil Error dengan WP Admin Error Handler
- Rilis Akismet plugin 2.5.6!
- Update Jetpack Plugin versi 1.3
- Yang baru di WordPress 3.0.2
- WordPress 3.3.2 Dirilis
- How to Cancelling Wordpress 3.0 Maintenance Mode?
- Belajar WordPress: Pagination Halaman tanpa Plugin
- WordPress 3.4 Beta Is Released! Mari di Uji
- Wordpress 3.0 User Features
- Wordpress Version 3.0 Highlights
- Belajar WordPress: Optimasi Robot.txt untuk SEO
- Belajar WordPress: Memfasilitasi Contributor untuk Upload File
3 Comments. Posted by admin on Wednesday, June 8, 2011 at 3:06 pm.
Filed under Wordpress, Yii, Integrating Yii + WordPress, wordpress, Yii.
3 Comments on “Belajar WordPress: Integrasi aplikasi Yii dalam WordPress”
You can track this conversation through its atom feed.

hello, thank you so much for your great code!
It’s working fine on my set up, but I can’t use Active Record.
Error code:
PHP Fatal error: Call to a member function getDb() on a non-object in /home/dominick/www/yii/framework/db/ar/CActiveRecord.php on line 615
I have a MySQL ‘db’ component setup in my config/main.php.
Do you see why?
Posted on March 30, 2012 at 12:43 pm.
do you use WAMP? try another package or use different version of apache/mysql
Posted on March 30, 2012 at 2:22 pm.
after a few tweak I managed to make it work. Thank u so much!
Posted on March 30, 2012 at 2:31 pm.