I recently migrated all of my sites to a new VPS and whilst migrating the WordPress sites I decided to move the WordPress core files into their own directory. This is great for an uncluttered site root directory but during one of my migrations I came across an issue with cookies.
intagrate.io is the marketing site for my premium Instagram WordPress plugin and during its migration I created a new local WordPress install from scratch using a custom initialisation script. The migration went well and it went live without a hitch. However, last week I installed Affiliates Pro to open up an Affiliate program to help promote Intagrate. The affiliate plugin runs off cookies being set on your site when someone visits with a referral URL. When I was testing the plugin I noticed that the cookies weren’t being recognised on “http://intagrate.io/” but were being seen in the browser on “http://www.instagrate.co.uk/wordpress/” (this being the directory I had moved WordPress to).
Now the WordPress address (URL) siteurl
should be updated in the database (via the Settings page) during the process of moving the core files to it’s own directory. But due to an issue with my initialisation script I ended up defining the URL in the wp-config.php
file:
define('WP_SITEURL', 'http://www.instagrate.co.uk/wordpress' );
define('WP_HOME', 'http://www.instagrate.co.uk/' );
The cookie was being set in the affiliate plugin using the WordPress constants SITECOOKIEPATH
and COOKIE_DOMAIN
, but for my install SITECOOKIEPATH
was returning “/wordpress/”.
SITECOOKIEPATH
is set in /wp-includes/default-constants.php
as:
/**
* @since 1.5.0
*/
if ( !defined('SITECOOKIEPATH') )
define('SITECOOKIEPATH', preg_replace('|https?://[^/]+|i', '', get_option('siteurl') . '/' ) );
So there was my problem. It is using the database version of siteurl
in the preg_replace
not the one defined in my config file. I solved the issue by adding this to my config file:
define( 'SITECOOKIEPATH', '/');
The problem actually wasn’t with WordPress at all but caused by my hacks to wp-config.php
. However, in finding the issue and solving it I learnt much more about PHP cookies and the constants used by WordPress.