Can you create a global multisite cookie that can be accessed across the network?

by Ryan Palmer   Last Updated August 16, 2018 16:08 PM

In a multisite install, I have a request where the client wants to store the current blog ID in a cookie and be able to read the cookie data on the main site of the network. The use cases is something like this:

  • Main Site - blog ID 1
  • Subsite A - blog ID 2
  • Subsite B - blog ID 3

A site user views Subsite A of the network. This would store blog ID of 2 in a cookie. The site user goes back to the main site and a message would appear saying "You recently viewed Subsite A". Or if the user goes to Subsite B, the blog ID in the cookie would change to 3 and the main site message would change based on that, etc. etc.

But with it being Multisite, each site creates it's own cookie from what I'm seeing. I may be wrong there, but you can see the cookie set on each subsite, but you don't see the cookie when viewing the main site. So it doesn't appear to be a global kind of thing.

This is my basic set up to create the cookie now. The client isn't not tied to any one approach, so totally open to any way to achieve this.

function set_cookies() {
    if ( ! isset( $_COOKIE['site_id'] ) ) {

        $site_id = get_current_blog_id();

        if ( $site_id != 1 ) {
            setcookie( 'site_id', $site_id );

add_action( 'init', 'set_cookies', 1 );

The multisite install is not a subdomain install, so each subsite is would be like, if that helps.

Any help anyone can provide would be hugely appreciated. Thanks!

Related Questions

COOKIE_DOMAIN setting confusion

Updated February 24, 2017 02:08 AM

Programatically set cookies on multiple domains

Updated May 01, 2017 09:08 AM