Wordpress in CodeIgniter Wordpress in CodeIgniter codeigniter codeigniter

Wordpress in CodeIgniter


I did the following to get this to work (I have a Code Igniter application in a separate directory within a Wordpress directory) -- what I obviously don't like about it is that I had to modify a core file within Wordpress.

First, I added my Code Igniter cookie name to the $no_unset array in wp-includes/load.php In my case it was ci_session:

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

Second, I figured out that Wordpress's add_magic_quotes function was mangling the $_COOKIE global. This was causing CodeIgniter to re-create the cookie on each page load or redirect and thereby breaking any continuity. So, I commented this line out in wp-includes.load.php (around line 545)

//$_COOKIE = add_magic_quotes( $_COOKIE );

Next, to keep this function in tact for all other Wordpress related cookies, I created an array_walk function to loop over the $_COOKIE global and apply add_magic_quotes to all cookies except for mine within the wp-includes/load.php 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);    }}//Put this line in place of the commented out line above...array_walk($_COOKIE, 'ci_ignore_magic_quotes');

After I did this, I was no longer having multiple cookies stored in my ci_sessions table and sessions were successfully retained.

I hope this helps!


There's an explanation here of how WordPress unsets session variables, and a possible solution. Unfortunately it seems to require modifying core files - there don't seem to be any hooks in wp_unregister_GLOBALS that would help.