如何将 WordPress 集成到 Kohana 3 中

发布于 2024-09-01 06:32:42 字数 664 浏览 7 评论 0原文

我现在需要让 Kohana 3 网站拥有一个 Wordpress 博客。

我看过 Kerkness 的 Kohana For Wordpress,但似乎恰恰相反我想要的。

以下是我想到的选项:

  • 设计一个模板,使其看起来与 Kohana 网站一模一样(耗时、非 DRY 并且可能不起作用)
  • 将博客包含在 iframe 中(丑陋极了)
  • cURL WordPress 页面。这当然意味着我需要在评论发布等之间创建图层,这听起来工作量太大了。

有什么方法可以将 Wordpress 博客包含在现有的 Kohana 应用程序中吗?您有什么建议吗?

我发现这篇文章详细介绍了 Kohana for Wordpress 插件,但我仍然对其如何工作感到困惑。

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

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?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

锦欢 2024-09-08 06:32:42

哦,我很久以前就这样做了(实际上是去年年底)。

假设

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

重命名

首先,您需要重命名 Kohana 中的 __() 函数。比如说,您将其重命名为 __t()。您需要在它出现的所有地方替换它,如果您使用像 Netbeans 这样可以找到函数或方法的用法的编辑器,那么这非常容易。

层次结构

您需要做出的下一个决定是是否要将 Wordpress 加载到 Kohana 中,或者将 Kohana 加载到 Wordpress 中。我更喜欢后者,我在下面记录了这一点。如果您愿意走那条路,我可以记录后者。

我将 kohana 目录放在我的主题目录中。

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

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

Kohana 配置

您的 Kohana 的 index.php 文件还需要一些工作。删除查找 install.php 的行,因为它们将加载 ABSPATH 。 WPINC。 'install.php' 而是在您的 WordPress 管理员中显示错误消息。您还需要更改 error_reporting,因为 Wordpress 失败时 E_STRICT。

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

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

在您的 Wordpress Functions.php 文件或引导程序中,添加这些行:

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

其中 Application 是您选择的一个类。

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

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/calendar 只要您没有定义 about 控制器和 calendar 操作。

这样你就得到了。任何未在 WordPress 中定义的 url 将落入您定义的控制器(或使用主题的 404 模板)。

其他

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

echo View::factory('header')

您可以对页脚(或任何其他文件)执行相同的操作那件事)。在 header.php 中,您也可以这样做:

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

这样您就可以在控制器中

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

为了保持 url 一致,您可能必须从 Wordpress 永久链接末尾删除 /,因此 /%year%/%monthnum% /%day%/%postname%/ 变为 /%year%/%monthnum%/%day%/%postname%


如果您需要更多集成帮助,请告诉我WordPress 和 Kohana。

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.

情绪失控 2024-09-08 06:32:42

我实际上已经使用 wordpress 作为 code igniter 网站的 CMS。这是我用来提取页面内容而不是博客内容的方法,但也许您可以对其进行一些更改以满足您的需求。

在我的前端控制器中,我添加了 wordpress 头文件

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

这使您可以访问

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

获取页面数据

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

所需的 2 个函数如果您收到此错误:

致命错误:只能是变量
通过引用传递...

您必须这样做,

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

因为 bug某些版本的 php

然后在视图中

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

希望这有帮助


EDIT


我在文件系统中的 /blog 安装了 wordpress。所以wordpress实际上是作为一个博客正常运行的。我只是用这个方法来抓取页面

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

如梦 2024-09-08 06:32:42

由于 WordPress 的工作方式,这将非常困难。具体来说,它到处都使用全局变量,并且因为 Kohana 是有作用域的,所以您将无法访问这些变量。

长话短说:你想要的几乎是不可能的。然而,如果你让它工作(不破解 WP),我真的很想看看你是如何做到的。

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.

壹場煙雨 2024-09-08 06:32:42

另一个解决方案是将 Wordpress 和 Kohana 安装完全分开。然后,您创建一个自定义 Wordpress 主题,该主题将从 Kohana 中提取页眉和页脚(您可以为此创建一个 Kohana 控制器)。

一旦您输入了页眉和页脚,博客看起来就会集成到您的网站中,尽管它仍然是完全独立的安装。优点是无需破解 WordPress 或 Kohana 即可使其正常工作。

此博客文章中有有关此方法的更多详细信息: 将 WordPress 集成到 Kohana 应用程序中

Another solution is to keep both Wordpress and Kohana installations completely separate. Then you create a custom Wordpress theme that will pull the header and footer from Kohana (you can create a Kohana controller for that).

Once you have the header and footer in, the blog looks integrated to your website even though it's still a completely separate installation. The advantage is that there's nothing to hack to either Wordpress or Kohana to get it working.

There's some more details about this method in this blog post: Integrating Wordpress into a Kohana application

逆光下的微笑 2024-09-08 06:32:42

我一直认为这会相对容易。也就是说,使用 WordPress 作为网站的后端(至少对于博客部分)并使用 Kohana 来提供帖子和页面。如果我没记错的话,您需要做的就是设置模型(帖子、评论、页面)以从 WordPress 数据库(带或不带 ORM)收集数据,而不是从新数据库收集数据。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文