views:

499

answers:

6

I now need to make a Kohana 3 site have a Wordpress blog.

I've seen Kerkness' Kohana For Wordpress, but it seems to be the opposite of what I want.

Here are the options I have thought of

  • Style a template to look exactly like the Kohana site (time consuming, non DRY and may not work)
  • Include the blog within an iframe (ugly as all hell)
  • cURL the Wordpress pages in. This of course means I will need to create layers between comment posting, etc, which sounds like too much work.

Is there any way I can include a Wordpress blog within an existing Kohana application? Do you have any suggestions?

Thanks

Update

Forgot to mention I have already programmed a model in Kohana to open pages based on their slug. Now I want to put blog content in there, and have the comments and other bits work.

Another Update

I found this post detailing the Kohana for Wordpress plugin, but I am still confused as to how it works.

Does it mean from within Wordpress, I can call a Kohana controller? Is this useful to me in my situation?

+3  A: 

I've actually used wordpress for the CMS of a code igniter site. This is the method i used to pull page content, not blog content, but maybe you can change it up a little to fit your needs.

In my front controller I added the wordpress header file

require('/path/to/wp-blog-header.php');

This gives you access to the 2 functions you'll need

get_page()  – Get the page data from the database
wpautop() – Automatically add paragraph tags to page content

To get page data

$page_data = get_page( 4 ); // Where 4 is the page ID in wordpress

If you get this error:

Fatal error: Only variables can be passed by reference…

You have to do it like this

$page_id = 4;
$page_data = get_page( $page_id );

because of a bug in certain versions of php

Then in the view

<?= wpautop($page_data->post_content) ?>

Hope this helps


EDIT


I installed wordpress at /blog in the filesystem. So wordpress actually runs as a blog normally. I just use this method to grab the pages

Galen
Thanks Galen, however I have already tackled this part of it (the pages). I was wondering if I could do something similar for the blog content too.
alex
updated my answer
Galen
@Galen Thanks, so do you point your users to /blog when they want to view the blog? Does it use CI at any point once displaying the blog? I want to maintain the same header and functionality of my Kohana app, so I'd rather not point to a Wordpress install, but rather get Wordpress' content to appear from within one of my views.
alex
in my wordpress file i actually include my header/footer file from CI. It takes a little fanagling.
Galen
+2  A: 

This is going to be extremely difficult, because of the way WordPress works. Specifically, it uses global variables all over the place, and because Kohana is scoped, you will not be able to access those variables.

Long story short: what you want is nearly impossible. However, if you get it working (without hacking WP), I would be really interested to see how you did it.

shadowhand
Thanks, I thought it may be difficult. Last time I tried it (with my personal blog) I wrote the entire front end in Kohana 2.3. If I do get it working I'll let you know.
alex
A: 

I always thought this would be relatively easy. That is, to use WordPress as your site's back-end (for the blog part, at least) and use Kohana for serving up posts and pages. If I'm not mistaking, all you would need to do is set up your models (post, comment, page) to gather their data from the WordPress database (with or without ORM) instead of a new one.

Hoss
This is the easy way to do it (which I have done for my own blog). But you need to create wrappers for any of the functionality (like posting comments for example).
alex
+1  A: 

See here: http://www.intuitivity.org/archives/8 I figured it out yesterday :)

Tails
Thanks for the link. +1
alex
A: 

I tried it like Luuk de Waal. Luuk used kohana 2.x. I'm using kohana 3.x. kohana declares a function __ and wordpress seems to have also a function __ How can I solve this problem? Use a different context? Is there a different function context in php?

Controller:

  <?php
defined('SYSPATH') or die('No direct script access.');

class Controller_Blog extends Controller_DefaultTemplate {
    //public function action_view($p1 = "",$p2 = "", $p3 = "", $p4 = "", $p5 = ""){
    public function action_index(){
        $this->template->content = View::factory("blog");
    }
}

View:

<?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; ?>

Error msg:

ErrorException [ 64 ]: Cannot redeclare __() (previously declared in /var/www/system/base.php:31)

seems there is only one solution: renaming

I restarted the conversation in the community forum of kohana: http://forum.kohanaframework.org/comments.php?DiscussionID=6566&amp;page=1#Item_1

Sebo
If you are not doing i18n, you could probably remove one of the `__()` functions.
alex
But I'm doing it. Any other suggestions?
Sebo
@Sebo [Zahymaka's answer](http://stackoverflow.com/questions/2827238/incorporate-wordpress-into-kohana-3/3593080#3593080) suggests renaming it in Kohana.
alex
Sebo
+2  A: 

Oh, I did this a long time ago (actually towards the end of last year).

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 could document the latter if you'd prefer to go that route.

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' 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


Please let me know if you need any more help integrating Wordpress and Kohana.

Zahymaka
+1 this is great! Many thanks. I ended up skipping Kohana for the project and using WordPress alone, and I did learn a lot of how WordPress works (and some questionable coding...). I'll definitely look at this if I decide to use these projects together in the future.
alex
Thanks. Good luck with your project.
Zahymaka