如何将Wordpress集成到Kohana 3中 [英] How to integrate Wordpress into Kohana 3

查看:60
本文介绍了如何将Wordpress集成到Kohana 3中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在需要使Kohana 3网站上有一个Wordpress博客.

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

我看过Kerkness' Kohana for Wordpress ,但似乎与我想要的相反.

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

这是我想到的选项

  • 设置模板的样式使其看起来完全像Kohana网站(耗时,非DRY,并且可能无法正常工作)
  • 将博客包含在iframe中(丑陋的地狱)
  • 使用URL加载Wordpress页面.这当然意味着我将需要在评论发布等之间创建图层,这听起来工作量太大.
  • 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.

有什么办法可以在现有Kohana应用程序中包含Wordpress博客?你有什么建议吗?

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

我发现这篇文章详细介绍了Kohana for Wordpress插件,但是我仍然对它的工作方式感到困惑.

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

这是否意味着从Wordpress中可以调用Kohana控制器?在我的情况下这对我有用吗?

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

推荐答案

我很久以前(实际上是在去年年底)做到了.

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

  1. 您正在使用带有mod_rewrite或类似选项的Wordpress永久链接.
  2. 您没有打开register_globals().将其关闭以确保Kohana不会删除Wordpress的全局变量.

重命名

首先,您需要在Kohana中重命名__()函数.假设您将其重命名为__t().您需要将其替换为所有出现的地方,如果您使用Netbeans这样的编辑器来查找函数或方法的用法,则非常简单.

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.

您需要做出的下一个决定是要在Kohana中加载Wordpress还是在Wordpress中加载Kohana.我更喜欢后者,我将在下面进行记录.如果您愿意走那条路线,我可以记录下后者.

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.

我将kohana目录放在主题目录中.

I put the kohana directory in my theme directory.

只需在主题的functions.php文件中

In your functions.php file of your theme, simply

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

您的Kohana的index.php文件也需要一些工作.删除寻找install.php的行,因为它们将加载ABSPATH . WPINC . 'install.php',并在您的wordpress管理员中显示错误消息.您还需要更改error_reporting,因为Wordpress无法使E_STRICT失效.

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.

您很可能需要删除引导程序的最后几行(在Kohana中)以处理请求,并更改您的init:

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'   => '',
));

在您的Wordpress functions.php文件或引导程序中,添加以下行:

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

其中应用是您选择的一类.

我的 Application 类(没有类定义)的代码是:

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

可以让Wordpress进行重定向,以重定向可能已移动的任何页面,例如只要您没有定义 about 控制器和 calendar 操作,就可以将/about/calendar 更改为/calendar

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.

因此,您已经拥有了它.在Wordpress中未定义的所有URL都将属于您定义的控制器(或使用您主题的404模板).

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

这不是必需的,但是您可以将主题的header.php放在您的kohana views文件夹下(应用程序或模块中)以及任何主题文件中

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

您可以对页脚(或与此相关的任何其他文件)执行相同的操作.在header.php中,您也可以执行以下操作:

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

要保持网址的一致性,您可能必须从Wordpress永久链接的末尾删除/,以便/%year%/%monthnum%/%day%/%postname%/变为/%year%/%monthnum%/%day%/%postname%,等等

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

请让我知道您是否需要更多有关集成Wordpress和Kohana的帮助.

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

这篇关于如何将Wordpress集成到Kohana 3中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆