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');
}

Read Us

Designers Meet SEO Series: SEO vs. Content Marketing

Web designers and developers often work separately from those who focus more on strategy for…

Connect

Wanna talk?
Call us at 503.892.2808

Rather type?
Email us at

Hire Us

Seen enough and know what you want?
Or maybe just part of it?
We'll help with the rest.

Get Started
DWP