/Wordpress_CI

Place Codeigniter in a subdirectory of wordpress.

Wordpress/Codeigniter Integration

Version 0.1, February 3, 2013

This repository will allow you to place a CodeIgniter Application within a Wordpress directory and use Wordpress functions in your CodeIgniter app. A common use is to have consistent navigation within the CI app and Wordpress site. The helper file “namespaces” CIs site_url function (ci_site_url) to avoid collisions with Wordpress’ site_url function.

If CI sessions are used, you can use the wp-load.php file so that Wordpress doesn’t mangle CodeIgniter’s cookies.

Most other solutions require you to place the application and system directories in the site root; this allows you to keep Wordpress and Codeigniter separate!

Setup

  • Include the Wordpress load file (wp-load.php) immediately before calling the CI bootstrap file in your CI/index.php directory (see below)
  • Add the application/helpers/MY_url_helper.php to your CodeIgniter App.
  • Important!: Change all instances of site_url() in your application directory to ci_site_url; do NOT update anything in the system directory
    • In general, updating application/views, application/controllers, application/models will do.
  • (OPTIONAL) If using CodeIgniter’s Session Library, update Wordpress per the instructions under the Sessions Section.

Add Wordpress Load file to CIs index.php

/*
* --------------------------------------------------------------------
* LOAD THE BOOTSTRAP FILE
* --------------------------------------------------------------------
*
* And away we go...
*
*/
//Load Wordpress file.
require_once('/Server/path/to/wp-load.php');
require_once BASEPATH.'core/CodeIgniter.php';
/* End of file index.php */

Sessions

Unfortunately, Wordpress loops through your $COOKIE global and applies its "add_magicquotes" function, which renders CI’s COOKIE useless; one or more CI cookies will be created on each page load.

KLUDGE: At this point, my solution is to edit the core file wp-includes/load.php, but there is probably a more elegant solution.

In wp-includes/load.php

On about line 26, add your CI cookie to the no_unset array. I’m using the default ci_session key created by CI, but replace this with your own.

$no_unset = array( 'GLOBALS', '_GET', '_POST', '_COOKIE', '_REQUEST', '_SERVER', '_ENV', '_FILES', 'table_prefix','ci_session' );

To prevent Wordpress from mangling your CI cookie, scroll down to about Line 520 and add this function

/**
* 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")
{
stripslashes_deep($value);
}
}

Finally, comment out the following lines inside the wp_magic_quotes function and add a reference to the above ci_ignore_magic_quotes function.

array_walk($_COOKIE, 'ci_ignore_magic_quotes');
//$_COOKIE = add_magic_quotes( $_COOKIE );