开发者

How do I include CodeIgniter within a WordPress plugin?

开发者 https://www.devze.com 2023-03-05 19:03 出处:网络
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 \"

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 e开发者_运维问答rror 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消