I first did this with CongreGATE.

Assumptions

1. You are using WordPress permalinks with mod_rewrite or a similar option.
2. You don’t have register_globals() turned on. Turn it off to ensure WordPress’s global variables don’t get removed by Kohana.

Renaming

First, you need to rename the __() function in Kohana. Say, you rename it to __t(). You’d need to replace it everywhere it appears, which if you use an editor like Netbeans that can find usages of a function or method is pretty easy.

Hierarchy

The next decision you need to make is whether you want to load WordPress inside Kohana or Kohana inside WordPress. I prefer the latter, which I’m documenting below.

I put the kohana directory in my theme directory.

In your functions.php file of your theme, simply

include TEMPLATEPATH . '/kohana/index.php';

Kohana Configuration

Your Kohana’s index.php file also needs some work. Remove the lines that look for install.php as they will load ABSPATH . WPINC . 'install.php' (usually wp-includes/install.php relative to your WordPress installation directory) instead and display an error message in your wordpress admin. You also need to change the error_reporting as at the moment WordPress fails E_STRICT.

You will very likely need to remove the last few lines of your bootstrap (in Kohana) that process the request, and change your init:

Kohana::init(array(
	'base_url'   => get_bloginfo('home') . '/',
	'index_file'   => '',
));

In either your WordPress functions.php file or in your bootstrap, add these lines:

remove_filter('template_redirect', 'redirect_canonical');
add_filter('template_redirect', 'Application::redirect_canonical');

where Application is a class of your choosing.

My code for the Application class (without the class definition) is:

public static function redirect_canonical($requested_url=null, $do_redirect=true)
{
    if (is_404() && self::test_url())
    {
        echo Request::instance()->execute()->send_headers()->response;
        exit;
    }

    redirect_canonical($requested_url, $do_redirect);
}

public static function test_url($url = NULL)
{
    if ($url === NULL)
    {
        $url = str_replace('?'.$_SERVER['QUERY_STRING'], '', $_SERVER['REQUEST_URI']);

        $url = trim($url, '/');
    }

    foreach (Route::all() as $route)
    {
        /* @var $route Route */
        if ($params = $route->matches($url))
        {
            $controller = 'controller_';

            if (isset($params['directory']))
            {
                // Controllers are in a sub-directory
                $controller .= strtolower(str_replace('/', '_', $params['directory'])).'_';
            }

            // Store the controller
            $controller .= $params['controller'];

            $action = Route::$default_action;

            if (isset($params['action']))
            {
                $action = $params['action'];
            }

            if (!class_exists($controller))
                return false;
            if (!(method_exists($controller, 'action_' . $action) || method_exists($controller, '__call')))
                return false;
            return true;
        }
    }

    return false;
}

which lets WordPress do it’s redirect for any page that may have moved e.g. /about/calendar to /calendar as long as you don’t have an about controller and calendar action defined.

So there you have it. Any urls not defined within WordPress will fall to your defined controller (or use your theme’s 404 template).

Additional

This isn’t required, but you could put your theme’s header.php under your kohana views folder (application or in a module) and from any of your theme files

echo View::factory('header')

You could do the same thing with your footer (or any other files for that matter). In your header.php, you could also do this:

if (isset($title)) echo $title; else wp_title(YOUR_OPTIONS);

That way you could in your controller

echo View::factory('header')->set('title', 'YOUR_TITLE');

To keep urls consistent, you may have to take off the / from the end of WordPress permalinks so /%year%/%monthnum%/%day%/%postname%/ becomes /%year%/%monthnum%/%day%/%postname%, etc

6 Comment so far

  • September 4, 2011 at 2:00 pm

    [...] ????????, ?? ??????? ? ?????, ????????? ?????????? ko3 ? wordpress. [...]

  • sannoble
    March 31, 2012 at 1:27 am

    Thank you very much for this nice tutorial!

    Could you please give some background information why the __() function has to be renamed and more important if it is sufficiant to rename it in my kohana application folder or if it has to be renamed as well in the kohana modules and system folder. The latter one would complicate to update kohana.

    Thanks in advance!

  • March 31, 2012 at 2:29 am

    Hello Sannoble.

    It would be a lot easier to update Kohana and rename __() than to do the same for WordPress. Pretty much every plugin and theme in WordPress uses the __() function, and it’s used everywhere in the WordPress code itself. When trying to run both together, one has to give way, and for now it seems to be Kohana.

    I hope that answers your question?

  • [...] Integrating Kohana 3 with WordPress [...]

  • February 7, 2013 at 2:10 pm

    I had to make one change to redirect_canonical to get this to work with Kohana 3.3, which has to do with how the request is processed. Change:

    Request::instance()->execute()->send_headers()->response;

    To this:

    Request::factory()->execute()->send_headers()->body();

    Thanks for posting this!

  • February 7, 2013 at 2:22 pm

    [...] Then check out Azuka Okuleye’s super helpful article on how to integrate Kohana 3 with WordPress. [...]

Add Your Comment

* Your Name

* Your Email

Your Website