Close this search box.


In this article


Recent articles

How-to: set cookies on the root domain with a MU Plugin

You may want to change an existing feature in Complianz to fit your specific use case. Of course, you can reach out to us to implement it, but it is entirely possible (and easier than you might think) to add the desired functionality yourself. The recommended way to add or change functionality is by using so-called Must Use Plugins.

Introduction to Must Use Plugins

As explained in our earlier article about WordPress filters, WordPress provides filter hooks that allow plugins to modify data at runtime. Plugins can modify data by adding a callback to a filter hook. When this filter is applied, every callback gets executed based on its priority and given a chance to modify the value by adding a new value.

So, why use MU-Plugins instead of adding your filter to the “functions.php” file? There are a number of reasons why MU-Plugins are the recommended method to add filters. When updating your theme, the current “functions.php” will be replaced with an updated version, removing any code that you have added (unless a child theme is used). The functions.php file is specific to the theme, so changing your theme will also result in your custom code not getting executed.

In contrast to “functions.php”, WordPress always checks if the /mu-plugins/ folder exists, and runs the scripts that it contains. It does this right after the WordPress core code has executed, meaning that code added as MU-Plugin will execute before code that is placed within the /plugins/ and /themes/ folders.

The /mu-plugins/ folder does not exist by default. If this is the case, you can simply open your FTP client and create the folder in the /wp-content/ directory.

Creating and adding a Must Use Plugin

We will add an often requested functionality as our first MU-Plugin, which is the possibility to force the Cookie Path to the root folder (or a subdomain) of your website. This is mostly used for domains with multiple WordPress Installations, where consent management should be centralized.

We already have a wide array of integrations ready to be used in your projects. Find them on our GitHub integrations repository.

  • Open your favourite text editor, create a new file and start the file with <?php  
Creating a new MU plugin in your Text Editor
  • Copy and paste the “force-cookies-on-root” code below to your newly created file. If you want to change the path to another directory/subfolder, place it within the quotation marks on line 6.
  • When you are done editing, save the file ending with .php
    • Tip: Always use descriptive file names, so you can easily remember the function of a particular MU-Plugin. Especially helpful if you have multiple MU-Plugins installed.
  • Upload force-cookies-on-root.php to /wp-content/mu-plugins/ with a FTP client (such as FileZilla or CyberDuck)

  • Check your site if the plugin has applied the desired functionality. Certain filters might require clearing your cache, so it is advisable to test in a private/incognito browser window (CTRL+Shift+N on Windows). If any errors appear, simply deleting the file from the mu-plugins folder will restore the site.

Join 900.000 users and install The Privacy Suite for WordPress locally, automated or fully customized, and access our awesome support if you need any help!

Complianz has received its Google CMP Certification to conform to requirements for publishers using Google advertising products.