How to: Integrate WordPress in Kohana
There are many projects trying to integrate Kohana into WordPress, like here, but I wanted the exact opposite. I wanted seamless integration of WordPress in my Kohana website, with clean urls and all.
Yesterday, I finally managed to get the integration of wordpress inside the great Kohana framework working, after three days. Here’s how I did it.
My wordpress installation (v3.x) is located in the directory of my Kohana project (v2.x) so it looks like this:
- application
- system
- modules
- wordpress
- (… etc …)
The Code
I have a Kohana view called ‘blog’ which contains the standard WordPress program loop, but also contains the inclusion of the WordPress environment. This view loads the wordpress environment. It looks something like this:
<?php require_once('wordpress/wp-blog-header.php'); ?>
<h2>Blog</h2>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
*wordpress loop*
<?php endwhile; else: ?>
<?php _e('Sorry, no posts matched your criteria.'); ?>
<?php endif; ?>
*wordpress loop* is the place where ‘The Loop’ of WordPress should reside which loads the post as explained here: codex.wordpress.org/The_Loop
Next, we need a Kohana controller for the blog view.
My Blog Controller looks like this (also shortened):
class Blog_Controller extends Page_Controller {
public $template = 'template/page';
public function __construct()
{
parent::__construct();
}
public function params($param, $param1='', $param2='', $param3='', $param4='', $param5='')
{
$this->template->content = new View('blog');
}
}
And I have added a single routing rule:
$config['blog/([A-Za-z0-9-_/=]+)'] = 'blog/params/$1';
That’s it for the code. Easy no?
The Explanation
What all this does:
- when someone visits the /blog page (controller), the blog view is rendered.
- the blog view includes the WordPress environment and loads the posts
when a page within wordpress is loaded, for example, /blog/archives/13/14/stuff, the following happens:
- the routing rule routes everything to the ‘params’ function within the blog controller.
because the wordpress clean urls feature is turned on, the ‘/archives/13/14/stuff’ part gets interpreted as arguments by Kohana, so we need enough defined parameters in the params function in the controller.
- the controller does absolutely nothing with the parameters, it merely accepts them and prevents generating a 404.
- the view gets loaded
- wordpress processes the url and loads the corresponding page.
That’s it… accept for one thing. WordPress expects to be loaded as a single package. This means that it expects to have its own index.php file, from where the environment is loaded. However, this is not the case in our installation, as the wordpress environment gets loaded by our view.
Our view gets loaded by Kohana’s index.php. You might notice the problem here…
As wordpress processes the url, it thinks the index.php is its own, and thinks /blog is the name of a page within wordpress, which is not the case. /blog is actually the document root for WordPress, but it doesn’t know this. The solution is to fake the document root from the perspective of WordPress. This may be done by the following:
The wordpress environment contains a file called ‘classes.php’. This file contains a function called
'parse_request($extra_query_vars = '')'
.
Within this function, add the following line:
$pathinfo = str_ireplace('/blog', '', $pathinfo); // /blog is the name of your blog controller
Below the following code block:
if ( isset($_SERVER['PATH_INFO']) ) $pathinfo = $_SERVER['PATH_INFO']; else $pathinfo = '';
This fakes the document root for WordPress. It’s a bit hacky, and if a cleaner solution is possible (I guess, something with .htaccess), please let me know
This last bit works by removing your controller name from the pathinfo. Pathinfo is the path that comes after the index.php, but before the query string.
The PHP documentation (http://php.net/manual/en/reserved.variables.server.php) says this about pathinfo:
Contains any client-provided pathname information trailing the actual script filename but preceding the query string, if available. For instance, if the current script was accessed via the URL http://www.example.com/php/path_info.php/some/stuff?foo=bar, then $_SERVER['PATH_INFO'] would contain /some/stuff.
Now, the wordpress blog should feel like it’s just a Kohana page or module.
Note: because the complete wordpress installation is still in /wordpress, the official template version of the blog can still be accessed from localhost/wordpress (for example), because it still contains its own index.php
Disclaimer: I haven’t tested this thoroughly.
Very interesting… thanks for sharing.
Does it support blog comments?
It does. In theory, it should support everything you throw at it, as long as there are enough parameters defined in the controller, and no url conflicts between WordPress and Kohana.
When I did my own site, I implemented the commenting myself.
Maybe I could of done it better
[...] This post was mentioned on Twitter by Kenneth Ellis McCall. Kenneth Ellis McCall said: RT @alexdickson: How to: Integrate WordPress in Kohana http://bit.ly/d6uHRP #kohana #wordpress #php [...]
[...] integrating a blog in my Kohana install, I wanted a [...]
hi, thanks dude for sharing but when I visit /blog page I have error establishing a database connection. Setup is good beacause when I visit /wordpress blog starts normaly.
Same problem for me. Should I need a model for database connection of wordpress?
Try inserting the following line in your kohana index.php on the very top: require_once(‘wordpress/wp-load.php’);
Apparently, wordpress is unable to connect to the database via Kohana. Is Kohana able to use the database?
Wow! Thank you! I always wanted to write in my site something like that. Can I take part of your post to my blog?
Sure!
Another option which I’ve looked into is use the XML-RPC functionality to pull the post and related information from the wordpress site instead of including the wordpress ‘loop’ code explicitly. The benefit is being able to pull and include post content anywhere you want to (not just a fixed URL pattern) and ability to host your WP site anywhere you want–even on a different server.
The slight futz is you need to create some new XML-RPC functionality, such as findPostByURL()–wordpress XML-RPC for inserting/updating into wordpress is great, pulling from it is lacking. The good thing is WordPress has some excellent and easy to implement hooks to register new XML-RPC methods; this can be done simply via a basic plugin.
I’ve made a mockup of this, but have yet to document it fully–there are a number of posts on the web explaining this approach.
That’s not a bad idea. Another method I figured out later is just making some Kohana models for the WordPress database objects and just use that. The downside of this is not being able to use dynamic menu and sidebar code.
I just can not imagine with strong your blog greatly that helped me. Thank you “I don’t know the key to success, but the key to failure is trying to please everybody.” – Bill Cosby
Could you message me with some tips on how you made this website look this awesome , I would be thankful.