Welcome to the BlueLayerMedia Web Development Blog

We don't just build blogs, we write them too. Take a look at what's going on and our senseless ramblings!

WordPress and CodeIgniter Integration

By: on Jul 22, 2011 | 5 Comments

WordPress and CodeIgniter are two of our favorite systems to work with.  CodeIgniter is an absolutely fantastic framework to work with and WordPress is a great CMS out of the box.

Occasionally we are given a project which entails us integrating the two together. In one recent project, CodeIgniter was handling the front and back ends of the website and WordPress would strictly be handling the blog aspect.  There were instances on the site where we wanted to include some of WordPress’ posts in a sidebar.  However, simply calling WordPress’ The Loop wouldn’t work.  Simply requiring the blog header file to get the Loop to pull in data didn’t work either.  It simply threw the page in to an infinite loop.  Changing the require to require_once didn’t help matters.

Here’s the fix:

Open up CodeIgniter’s index.php file.  Scroll to the bottom of the file and you’ll see some code that looks like this:

/*
 * --------------------------------------------------------------------
 * LOAD THE BOOTSTRAP FILE
 * --------------------------------------------------------------------
 *
 * And away we go...
 *
 */
require_once BASEPATH.'core/CodeIgniter'.EXT;
 
/* End of file index.php */

Change the file to look like this:

/*
 * --------------------------------------------------------------------
 * LOAD THE BOOTSTRAP FILE
 * --------------------------------------------------------------------
 *
 * And away we go...
 *
 */
require_once 'blog/wp-load.php';
require_once BASEPATH.'core/CodeIgniter'.EXT;
 
/* End of file index.php */

Refresh the page you were looking on and you’ll see you’re not stuck in an infinite loop and you can begin pulling in WordPress data as well.


Related Posts:
  1. Adding CSS To WordPress Menu Item
  2. How To: Remove The WordPress Admin Bar
  3. How To: Reset WordPress Administrator Password via Cpanel
  4. How To: CSS Shorthand
  5. WordPress: Moving from WordPress.com to WordPress.org

Comments (5)

Frank

Posted at 9:32 pm on December 17th, 2011

Excellent, thanks very much for this. I was pulling my hair out trying to get codeigniter to pull in wordpress, and this solved it for me.

Jason Craig

Posted at 4:12 am on December 18th, 2011

You’re welcome!

Diyafury

Posted at 2:18 pm on January 10th, 2012

Hi there

I want to do exactly the same thing – I have an existing db drive site written in codeIgniter… I now need to add a WordPress blog element/s to this existing framework… eg. latest posts and the comment functionality embedded in an existing page.

Could you please advise me on what the directory structure should look like… ie. where should I install wordpress?

Jason Craig

Posted at 5:24 pm on January 10th, 2012

You can install it as you normally would in the root folder alongside the CI structure.

Peter Drinnan

Posted at 9:07 am on March 18th, 2012

Here is a quick fix for a bug you will find if using QTranslate (used for bilingual WP sites). QTranslate messes up the request_uri and can cause the CI router to fail.

$request_uri = $_SERVER['REQUEST_URI'];
require_once ‘blog/wp-load.php’;
// restore the original
$_SERVER['REQUEST_URI'] = $request_uri ;
require_once BASEPATH.’core/CodeIgniter’.EXT;

Post a Comment