How to Integrate CodeIgniter with WordPress

It comes as little surprise that many website owners and administrators still prefer WordPress as their primary Content Management System (CMS), and you can’t blame them considering the comprehensive features that WordPress has to offer. However, when it comes to creating customized applications, WordPress is on the back burner as it is not a suitable tool due to many reasons. But, the ease of management and updates on the CMS are too hard to give up, which is why many website owners today prefer to use CodeIgniter for application building.

CodeIgniter is a lightweight PHP framework that can help in developing applications that can be integrated with WordPress, which I will explain shortly. While some users prefer developing the CodeIgniter and WordPress elements separately, we will go through an example where the primary navigation and footer are similar in both platforms, because it doesn’t make much sense to update the navigation and footer twice.


As WordPress is our primary application here, we will use its functionality within CodeIgniter while keeping the CodeIgniter app in a sub-directory.

Here is a step by step guide to integrating CodeIgniter app with WordPress:

1: Add the WordPress Bootstrap File

We’ll take a look at the case study of Phil Palmieri to help us understand how to integrate CodeIgniter with WordPress. The wp-load.php file will immediately precede the CodeIgniter bootstrap file in the index.php.

We took a cue from Phil Palmieri and referenced WordPress’s wp-load.php file immediately preceding the call to CodeIgniter bootstrap file in CI’s index.php file:
* --------------------------------------------------------------------
* --------------------------------------------------------------------
* Here we go
//Load WordPress file.
require_once BASEPATH.'core/CodeIgniter.php';

Easy right?
Now before we add WordPress functionality to the CodeIgniter CI’s file, we will perform a test to make sure everything is alright. As you can see from the same above, all the links and URLs within the CodeIgniter app will link back to the root directory of WordPress, which results in a 404 error.
Since both CodeIgniter and WordPress have “site_url” functionality, we’ll still get the error even when the WordPress bootstrap file is loaded before CodeIgniter bootstrap file. However, by installing a Runkit PHP extension, we can overwrite the global function, provided that we have both site_url functions for WordPress and CodeIgniter in separate directories.

2: Extend Codeigniter URL Helper

We will now create a new namespaced function to ‘extend’ the URL helper of CodeIgniter. By using ci_site_url, we can resolve the site_url issue. The My_url_helper.php (helper) file should be in the application directory of CodeIgniter.

3: Change site_url to ci_site_url in CodeIgniter App.

Quick search your applications, controllers, models, and directories and replace all site_url file references with ci_site_url.

4: Stop WordPress from Contorting Cookies!

You might experience an issue where CodeIgniter will create cookies for every active database session when you log in. This can lead to WordPress bootstrap file rectifying the issue. This happens when WordPress loops via the $_COOKIE global and implements add_magic_quotes functionality to all elements, except the CodeIgniter cookie. Following is the code to resolve this issue.

Scroll to Line 520 and add this function:
(Let’s say you’re using CI's default ci_session key)
* Applies Magic Quotes to the $_COOKIE global but ignores Codeigniter's Cookie
* @param string $value Value passed by array_walk function
* @param string $key Key passed by array_walk function
function ci_ignore_magic_quotes($value,$key)
    if($key != "ci_session")
Comment out the following lines inside the wp_magic_quotes function and add a reference to the ci_ignore_magic_quotes function.
array_walk($_COOKIE, 'ci_ignore_magic_quotes');
//$_COOKIE = add_magic_quotes( $_COOKIE );
Additionally, you will have to set WordPress to not unset the $_COOKIE['ci_session']:
$no_unset = array( 'GLOBALS', '_GET', '_POST', '_COOKIE', '_REQUEST', \\
 '_SERVER', '_ENV', '_FILES', 'table_prefix','ci_session' );

Once all the steps are complete, you can start using WordPress with your CodeIgniter application like it was always there!

You may also like to read