Posted by & filed under PHP/CodeIgniter, WordPress.

It is possible to use CodeIgniter in conjunction with WordPress. This article details how to integrate CodeIgniter with WordPress, including an important a caveat involving 404 headers and its workaround.

Update 12/08/2011

A Github repos of recent versions of WordPress with these changes can be found here

Original Article

It is possible to use CodeIgniter in conjunction with WordPress. In fact, we do this on a regular basis! To do this you simply need to include the following in your site’s index.php file (that is, the CodeIgniter index.php in your document root), preferably at the top:

if (is_dir($_SERVER['DOCUMENT_ROOT'] . "/wordpress")) {
        require $_SERVER['DOCUMENT_ROOT'] . "/wordpress/wp-blog-header.php";

Once this has been included all of the usual WordPress template tags and other functions are available to you. You can make the post object available within your loops by declaring it global (global $post), and with this configuration the WordPress template you define for a particular page is irrelevant, as is its theme – this is controlled by WordPress. In essence, this allows you to utilize the WordPress dashboard and all of the WP metadata (tags, custom fields, etc.) and functions available to you within your themes while having your site controlled by CodeIgniter, making heavy use of the query_posts() WordPress function to call upon the content you wish to display.

The one caveat is if you have PHP’s output_buffering enabled, this will allow WordPress to send 404 headers when it thinks that your page does not exist, thereby getting Apache to log this request as a 404. The fix is either to create a dummy WordPress page that corresponds with the URL you are accessing (even if this page is blank, it doesn’t matter, CodeIgniter is driving the site and not WordPress), or else rewrite $_SERVER['REQUEST_URI'] within your theme’s functions.php to match a URL that WordPress considers valid.

Edit: we’ve up with another alternative… The following change to WordPress in: wp-includes/functions.php:

add the following to the top of the status_header function:

if (file_exists($_SERVER['DOCUMENT_ROOT'] . "/system")) {
// CodeIgniter site, WordPress should not set headers

  • NetMusician Support

    Added workaround involving a WordPress code modification

  • NetMusician Support

    Added link to Github repos

  • Kahlil

    Great job, very helpful. It’s a complement to the structure of both frameworks that they would be so easy to integrate.

  • Tony

    I have been searching for an answer to my specific problem and I found it! I invested at least 24 hrs on this problem. I had a CI base and created a WP blog subfolder and installed WP. I wanted to use WP functions on my CI page, but couldn’t figure out how to do it. Your answer really helped me out! Thank you!

  • Pingback: Success! | Anthony Morelle's Portfolio Blog

  • NetMusician Support

    I’m glad this worked, Tony!

    I will document another technique I like even better that is also compatible with WordPress Network installs. This technique also works with WordPress navigation menu supported themes.

  • Pingback: Portfolio Site Codeigniter + Wordpress hybrid | Anthony Morelle's Portfolio Blog

  • Peter Drinnan
  • Joe Auty

    Nice Peter! Your approach sounds much cleaner than mine in not requiring direct modifications. I’m using a multisite WordPress setup now and have come up with a better approach that I will post about shortly (it should work with single WordPress sites as well). It basically entails making WordPress see your CI site as just another WordPress theme. This approach has an added benefit of supporting the built-in WordPress navigation menu scheme, with an added CI hook. Look for this post soon, and thanks again for this link and your contribution here!