Development

CodeIgniter and WordPress integration

Daylight uses our CodeIgniter CMS, FUEL, for almost all of our client’s websites. A frequent request by clients is to incorporate a blog. Before we created our blogging module in FUEL, we relied upon WordPress to do the job.

The Problem

The initial problem we had with using WordPress was wanting to incorporate CodeIgniter functionality into our WordPress theme files. For example, we often have a header and footer that we include in many of our CodeIgniter view files. That same header and footer also needed to be included in the WordPress theme. If the header and footer were static files, then it wouldn’t be a problem. However, a lot of times we include CodeIgniter specific logic to render such things as a menu structure. That logic may include library, helper and other view files and would be extra work to re-implement into a WordPress theme not to mention having two different code bases performing the same job. So our question became how can you have WordPress and CodeIgniter work side-by-side so we can keep our code DRY?

The Solution

Our answer was to create a WordPress include file that executes the CodeIgniter bootstrap within WordPress. This creates the $CI super object global variable that can be accessed within your WordPress themes. Essentially, this gives a developer the ability to ‘inject’ a CI (CodeIgniter) instance – constants, models, libraries etc – into any existing WordPress theme file.

The following code is all you need to include CI into a WordPress blog (index.php of your favorite theme):

/* WordPress include... this will also include the codeigniter.php file found in the download that does most of the work */
require('ci_include.php');

wp_ci_load_view('_blocks/blog_header'); // remove WordPress' header function

...... WordPress Post Loop

wp_ci_load_view('_blocks/blog_footer'); // remove WordPress' header function

The above WordPress implementation loads the header and footer view files from CodeIgniter into index.php of a WordPress theme. It interprets all of the variables within each view and renders it accordingly. The same views could be used for the header and footer of a site’s home page thus allowing for a one layout solution.

Furthermore, you can access CI functionality like this:

require('ci_include.php'); // WordPress include
$CI =& get_instance(); // get CI instance as usual
$load->library('SomeLibrary'); // load a CI library
$somelibrary->library_method(); // use CI library as usual
...... WordPress Code Goes Here

Once the the include is called within your WordPress theme file, the $CI super object is available.

You can pickup our latest release of the CodeIgniter and WordPress integration files at BitBucket.

UPDATE: This code does not work anymore with the new versions of CI 2 and WordPress. However, we’ve found an alternate way that should work for most. Replace your index.php WordPress file (not your CodeIgniter one), with the code below. This will run the CI bootstrap index.php (assuming that the path to the bootstrap file is correct). It also loads in the wordpress helper file (assuming it’s in your helper folder) and creates a global $CI variable that can be used in your theme files. The only issue with this version is that it always renders the same CI page to get the CI object and you can’t manipulate which page it renders. However, in most cases, you just need the CI object to load views, function etc.

<?php /**
* Front to the WordPress application. This file doesn't do anything, but loads
* wp-blog-header.php which does and tells WordPress to load the theme.
*
* @package WordPress
*/

/**
* Tells WordPress to load the WordPress theme and output it.
*
* @var bool
*/
define('WP_USE_THEMES', true);

/***************************************************************************
* CODEIGNITER INTEGRATION
* creates a global $CI object
* by default it loads the homepage and captures it in the variable
* $GLOBALS['wp_ci_output']
****************************************************************************/
define('STDIN', TRUE);
$_SERVER['argv'] = array();
ob_start();
require('../index.php');
$GLOBALS['wp_ci_output'] = ob_get_contents();
ob_end_clean();
$GLOBALS['CI'] = get_instance();
require_once(APPPATH.'helpers/wordpress_helper.php');

/***************************************************************************/

/** Loads the WordPress Environment and Template */
require(dirname(__FILE__).'/wp-blog-header.php');

Newsletter

Sign up for the Daylight Newsletter to receive helpful blog posts, insights and featured client projects.

More Articles

Daylight X OVO: Strategic agency partnership

Daylight News

The Connected History of Google, Mobile SEO, and Rankings

Insights

Aurea Worx: Transforming the Customer Experience from Puzzling to Personalized

Client Stories

Our Clients

Aurea Logo
Wacom Logo
Chateau Ste Michelle Logo
Ste. Michelle Logo
SheerID Logo
Hanzo Logo
Black and decker Logo
Zapproved Logo
Marchex Logo
Pacvue logo
Burley Logo
Nike Logo
Coca-Cola Logo
Intel Logo
Providence Logo
Webtrends Logo
Sutter Home Logo
Trinchero Logo
Dakine Logo
Newmans Own Logo
Brooks Logo
DeWALT Logo