WordPress network login auth loop when using different domains

We have WordPress network setup for a client with the sites using different top level domain names. This is causing issue with the login redirect loop and administrator not being able to access control panel of the sites with domain name not matching main site’s domain.

Issues seems to be related to the cookies, WordPress uses main domain name when sends session cookie, so, if your site in the network has different domain, cookie is not set.

There is simple fix to this issue: in wp-config.php we are checking HTTP_HOST and setting cookie domain constant to the correct domain. I can see two options:

  1. Set cookie domain to match HTTP_HOST, this will work for all sites, but you won’t be able to jump between sites, which may be an inconvenience.
  2. If that’s one specific site which is causing issue, just add conditional statement to change cookie domain for that site. That worked well for us, because all sites were just using subdomains except one. This way admin can switch between almost all sites and has to re-login to the site with different domain name.

Here is a code we used:

if($_SERVER['HTTP_HOST']==="www.domain-name.com") {
define( 'COOKIE_DOMAIN', 'www.domain-name.com' );