我现在需要让 Kohana 3 站点拥有一个 Wordpress 博客。
我看过 Kerkness 的 Kohana For Wordpress ,但这似乎与我想要的相反。
这是我想到的选项
- 将模板样式设置为与 Kohana 网站完全一样(耗时、非 DRY 并且可能无法正常工作)
- 将博客包含在
iframe
中(非常丑陋) - 将 Wordpress 页面卷入其中。这当然意味着我需要在评论发布等之间创建层,这听起来工作量太大。
有什么方法可以在现有的 Kohana 应用程序中包含 Wordpress 博客?你有什么建议吗?
我找到了 this post detailing the Kohana for Wordpress plugin ,但我仍然对它的工作原理感到困惑。
这是否意味着我可以在 Wordpress 中调用 Kohana Controller ?这对我的情况有用吗?
最佳答案
哦,我很久以前就这样做了(实际上是在去年年底)。
假设
- 您正在使用带有 mod_rewrite 或类似选项的 Wordpress 永久链接。
- 您没有打开 register_globals()。将其关闭以确保 Wordpress 的全局变量不会被 Kohana 删除。
重命名
首先,您需要重命名Kohana 中的__()
函数。比如,您将其重命名为 __t()
。您需要在它出现的任何地方替换它,如果您使用像 Netbeans 这样可以找到函数或方法用法的编辑器,这将非常容易。
层次结构
您需要做出的下一个决定是要在 Kohana 中加载 Wordpress 还是在 Wordpress 中加载 Kohana。我更喜欢后者,我在下面记录了这一点。如果您愿意走那条路,我可以记录后者。
我把kohana目录放在我的主题目录下。
在主题的 functions.php 文件中,只需
包含 TEMPLATEPATH 。 '/kohana/index.php';
Kohana 配置
您的 Kohana 的 index.php 文件也需要一些工作。删除查找 install.php 的行,因为它们将加载 ABSPATH 。 WPINC 。 'install.php'
而不是并在您的 wordpress 管理员中显示一条错误消息。您还需要在 Wordpress 失败 E_STRICT 时更改 error_reporting。
您很可能需要删除处理请求的 Bootstrap (在 Kohana 中)的最后几行,并更改您的 init:
Kohana::init(array(
'base_url' => get_bloginfo('home') . '/',
'index_file' => '',
));
在您的 Wordpress functions.php 文件或 Bootstrap 中,添加以下行:
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 Controller 和calendar 操作.
就是这样。任何未在 Wordpress 中定义的 url 都将落入您定义的 Controller (或使用您主题的 404 模板)。
附加
这不是必需的,但您可以将主题的 header.php 放在您的 kohana views 文件夹(应用程序或模块中)和任何主题文件中
echo View::factory('header')
您可以对页脚(或与此相关的任何其他文件)做同样的事情。在您的 header.php 中,您也可以这样做:
if (isset($title)) echo $title; else wp_title(YOUR_OPTIONS);
这样你就可以在你的 Controller 中
echo View::factory('header')->set('title', 'YOUR_TITLE');
为了保持 url 的一致性,您可能需要去掉 Wordpress 永久链接末尾的/这样 /%year%/%monthnum%/%day%/%postname%/ 就变成了 >/%year%/%monthnum%/%day%/%postname%等
如果您在集成 Wordpress 和 Kohana 时需要更多帮助,请告诉我。
关于php - 如何将 Wordpress 集成到 Kohana 3 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2827238/