A lot of people have written posts on integrating Yii and WordPress. This article combines goncin's approach with an article I read about integrating Symfony and WordPress and applies it all to Yii and WordPress.

When you're finished following this article, you'll have a website that integrates WordPress and Yii routes in under one path. For example, you can setup /widgets and /customers to point to Yii controllers and /about-us, /contact-us, /blog/* and /faq pointing to WordPress pages.

What we're going to do is place Yii in front of WordPress to act as a router/controller. We'll then load WordPress up partially underneath the hood (enough that Yii can use headers, etc) but not hand off to WordPress unless Yii can't resolve the route. If Yii can't resolve the route, we'll have WordPress act as a custom error handler (for 404s only). Having wrapped Yii around WordPress allows us to use Yii objects / models / databases within WordPress plugins, functions and themes. It's pretty cool!

The easiest way to set things up is to have WordPress in a separate folder within your webroot folder, so that you don't have to worry about WordPress upgrades overwriting your files (and especially your entry script). Once WordPress is installed and setup to run smoothly out of /wordpress (path is easy to change), make sure the General Settings for WordPress are setup as follows:

WordPress Address (URL): http://<domain info>/wordpress

Site Address (URL): http://<domain info>

Doing the above tells WordPress to set its links to all run through your Yii entry script but to set paths for images, stylesheets, js, etc. to your webroot/wordpress folder.

The following code handles 404 errors using WordPress instead of the Yii 404 page. If there is indeed a 404 (because WordPress can't handle it either), the 404 page will be shown in the 404 layout in WordPress.

<?phpclassExceptionHandler{publicfunction__construct(){define('YII_ENABLE_EXCEPTION_HANDLER',false);
set_exception_handler(array($this,'handleException'));
}publicfunctionhandleException($exception){// disable error capturing to avoid recursive errorsrestore_error_handler();
restore_exception_handler();
$event=newCExceptionEvent($this,$exception);
if($exceptioninstanceofCHttpException && $exception->statusCode == 404){try{Yii::app()->runController("wp/index");
}catch(Exception$e){}// if we throw an exception in Wordpress on a 404, we can use// our main error handler to handle the error}if(!$event->handled){Yii::app()->handleException($exception);
}}}?>

Create a new controller to handle WordPress views and make sure to disable Yii layouts, as WordPress will handle its own layouts.

<?phpclassWpControllerextendsController{publicfunctionactionIndex(){$this->layout = false; // note that we disable the layouttry{$this->render('index');
Yii::app()->end();
}// if we threw an exception in a WordPress functions.php// when we find a 404 header, we could use our main Yii// error handler to handle the error, log as desired// and then throw the exception on up the chain and// let Yii handle things from here// without the above, WordPress becomes our 404 error// handler for the entire Yii appcatch(Exception$e){throw$e;
}}}

In addition, because we're using $this->render, Yii::app()->clientScript css/js files will be loaded into the WordPress header automatically after WordPress renders its content.

Create a view/wp/index.php file for your WPController, which loads WordPress up all the way.

<?phpwp();
require_once(ABSPATH . WPINC . '/template-loader.php');

At this point, going to your main URL should display the Wordpress home page and (assuming you have logging on) the Yii log info beneath. You now have access to Yii models, are able to do renderPartials, run DAO commands, etc inside of Wordpress functions, plugins, page templates, etc.

// Error handler'errorHandler'=>array(// use 'site/error' action to display errors'errorAction'=>'site/error',
// the above is unused for 404 errors, as those// are handled by Wordpress using our exception handler),

Having WordPress handle 404 errors means you don't have to map every URL to WordPress (making adding new pages much easier for backend users). It also means we can prevent 404 headers from being sent by Yii (if you have WordPress handle errors as the errorHandler, things work properly, but a 404 header is sent for any WordPress page, which isn't ideal for SEO). NOTE: by having WordPress handle all errors, you don't ever see a 404 page inside of your Yii layout.

Thanks, it helped me understand which one is it and now more questions popped up :)
Here on this page you don't mention anything about index.php located into data folder as in github although inside you put

So wordpress folder and index.php are in the same folder (folder data as on github)
And you also said to set into WP config:
WordPress Address (URL): http:///wordpress
Site Address (URL): http://
So index.php and wordpress folder should be into root folder ...

I may be very tired and miss something... i'll check all the links inside scripts so they point proper path because in my case it still doesn't work.

I went through and followed all your instructions. It doesn't appear that WP passes $post to yii so you can get the title, other page content as well as sidebars in the page. Any ideas on how to overcome this?

i have a problem in my event table,
there is a description column in my event table
the description has text hello "this" is my name
after adding wordpress
the description text comes like hello \"this" is my name

@kiennguyen: regarding your CHtml problem, did you copy/paste in your code? Because you have it mis-capitalized (should be CHtml, not Chtml) in your example.

Haven't tried a Wordpress admin function yet myself, but I think that uses a whole different Wordpress entry script. So we may need to duplicate some of the WpController stuff to run it inside of our Yii controller system.

Do let me know if you get it working. Would love to update the instructions. :-)

There's no way to create an Web Application here as autoload will always generates a fatal regarding YiiBase class (maybe because of wrong config), so I created a normal application with CController here instead.
The CHtml class works here, but the controller cannot render any views/text/file because of getTheme() error:

@exien, thanks for the suggestions, those additions have been added in. I've also been doing some research on the problem @OceanWind had and believe I've got an optimization for that that I can post in a few weeks. I'll need to test it first. It's a bit complicated. :-/