2

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?

6
  • 3
    What parts of codeigniter do you want to use for this wordpress plugin? This seems way overly complicated. Commented May 12, 2011 at 0:15
  • I want to use the url feature in codeigniter which will automaticly call the controller/method based on the url ... and this will be contained within a wordpress plugin. Commented May 12, 2011 at 0:30
  • 4
    I dunno, sounds like you're trying to fit a round peg in a square hole to me. Commented May 12, 2011 at 0:36
  • 2
    Doesn't Wordpress already have built-in routing? Commented May 12, 2011 at 1:58
  • i agree with gclaghorn. you sound like want to reinventing the wheel. Commented May 16, 2011 at 12:58

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

The mod-rewrite solution mentioned above does not solve the solve the problem. This doesn't include codeigniter within the wordpress context. Doing the mod-rewrite solution is just as if I was not running wordpress at all. The solution I am looking for is one where I can be within wordpress and have codeigniter be my plugin framework.
Why did you show us code saying that you wanted this exact thing then? You need to explain better. Do you want to use wordpress theme engine and then have plugin functions run in the theme? And then use plugin to generate the output of the plugin functions?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.