如何在 WordPress 插件中包含 CodeIgniter?

发布于 2024-11-06 19:36:58 字数 829 浏览 0 评论 0原文

我有一个 WordPress 网站。我正在为 WordPress 编写一个插件,我想在插件中使用 CodeIgniter 框架。我遇到了这个问题,因为 CodeIgniter 引导文件“index.php”期望在全局范围内运行,而不是在函数内运行。

Right now, within my plugin, I do the following:
// BEGIN: plugin code
function classifieds_template_redirect()
{
  global $wp;
  $plugin_path = plugin_dir_path(__FILE__);
  if (preg_match('/^CI/', $wp->request))  // Use CodeIgniter to handle the request
  {
    include($plugin_path."/index.php";  // Include the CodeIgniter bootstrap file
    die();
  }

}
add_action( 'template_redirect', 'classifieds_template_redirect' );
// END: plugin code

抛出以下错误:

致命错误:在第 47 行 /usr/local/apache/htdocs/site.utils/htdocs/CodeIgniter/system/core/Utf8.php 中的非对象上调用成员函数 item()

我相信这是由于CodeIgniter 期望其引导程序 index.php 文件在全局范围内运行。

有什么想法吗?

I have a WordPress website. I am writing a plugin for WordPress where I want to use the CodeIgniter framework within the plugin. I am having trouble with this because the CodeIgniter bootstrap file "index.php" expects to be run in the global scope, not within a function.

Right now, within my plugin, I do the following:
// BEGIN: plugin code
function classifieds_template_redirect()
{
  global $wp;
  $plugin_path = plugin_dir_path(__FILE__);
  if (preg_match('/^CI/', $wp->request))  // Use CodeIgniter to handle the request
  {
    include($plugin_path."/index.php";  // Include the CodeIgniter bootstrap file
    die();
  }

}
add_action( 'template_redirect', 'classifieds_template_redirect' );
// END: plugin code

The following error is being throw:

Fatal error: Call to a member function item() on a non-object in /usr/local/apache/htdocs/site.utils/htdocs/CodeIgniter/system/core/Utf8.php on line 47

I believe this is due to CodeIgniter expecting its bootstrap index.php file to be run in global scope.

Any ideas?

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

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

发布评论

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

评论(1

小梨窩很甜 2024-11-13 19:36:58

以下是您的选择:

  1. 重写以特定标识符开头的 URL(使用您的网络服务器)以发送到 CodeIgniter 的引导程序
  2. 重写 CodeIgniter 的路由样板以调整为不在全局范围内通过引导程序运行
  3. 使用小的 MVC WordPress 中的插件而不是 CodeIgniter

我将介绍第一个,因为我认为如果您顽固地坚持使用 CodeIgniter,那么这是最有意义的。我将向您提供一些概念验证代码,使用 Apache 作为网络服务器和 mod_rewrite 来完成此任务。

基本上,我们将实现的只是您向我们展示的代码,但我们将让 Apache 为我们做这件事,使 CodeIgniter 通过其引导程序在全局范围内运行。

您所要做的就是向 WordPress .htaccess 文件添加更多规则。像这样:

<IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /

        # Here's the code which rewrites the URL to your CodeIgniter plugin.
        RewriteRule ^CI/(?.*)$ wp-content/plugins/ci-plugin/index.php/$1 [QSA,L]

        RewriteRule ^index\.php$ - [L]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . /index.php [L]

</IfModule>

现在所有对 www.example.com/CI/... 的请求都将在全局范围内传递到 CodeIgniter 的引导程序。

Here are your options:

  1. Rewrite URLs (using your webserver) which starts with a certain identifier to get sent to CodeIgniter's bootstrap
  2. Rewrite CodeIgniter's routing boiler plate to adjust to not being run through bootstrap over global scope
  3. Use a small MVC plugin in WordPress instead of CodeIgniter

I am going to cover the first one, because I think that it makes the most sense if you are going to be stubborn about sticking with CodeIgniter. And I am going to give you some proof of concept code using Apache as the webserver and mod_rewrite to accomplish this.

Basically all we will achieve is your code you have shown us, but instead we will make Apache do it for us, making CodeIgniter being run though its bootstrap over global scope.

All you do is to add more rules to the WordPress .htaccess-file. Like this:

<IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /

        # Here's the code which rewrites the URL to your CodeIgniter plugin.
        RewriteRule ^CI/(?.*)$ wp-content/plugins/ci-plugin/index.php/$1 [QSA,L]

        RewriteRule ^index\.php$ - [L]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . /index.php [L]

</IfModule>

Now all request to www.example.com/CI/... will be passed to CodeIgniter's bootstrap over global scope.

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