Implementation

Setting up CookieOfficer on WordPress

Ways to add the CookieOfficer code in WordPress: with a plugin or in the theme.

4 min read

You can add the CookieOfficer code to a site running on WordPress in two ways: with a plugin for inserting code into <head>, or directly in the theme files. The first method doesn't require touching system files and will survive any changes to the theme. The second option gives you full control over where you place the script.

Before you start

Copy the code from the Installation page in the CookieOfficer panel. In the “Deployment method” section, choose “Code on the site”, then in the “Paste the code at the top of <head>” card click “Copy” next to the “CookieOfficer script” block.

You need administrator permissions in WordPress.

Method 1: a plugin for inserting code in the <head> section

This is the simplest method, and the only one that will survive any changes to the theme.

  1. Go to Plugins > Add Plugin and install any plugin for inserting code into the page header.
  2. Open the plugin settings and find the field that corresponds to the <head> section, most often labelled Header.
  3. Paste the CookieOfficer code at the very beginning of the field, before any other scripts.
  4. Save the changes.

If the plugin lets you set the insertion priority, choose the highest possible one. The code should be placed before the other tools that use cookies.

Method 2: the header.php file in the theme

This method requires a classic theme. If you don't see Theme File Editor in the Appearance menu, you have a block theme, which has no header.php file, or the editor has been disabled by your hosting provider. In both cases, use the first method.

  1. Go to Appearance > Theme File Editor.
  2. Make sure your child theme is selected at the top, not the parent theme.
  3. In the file list on the right, open Theme Header (header.php).
  4. Paste the CookieOfficer code right after the opening <head> tag, before any other scripts and before the wp_head() call.
  5. Click Update File.

How do I check the setup?

Go back to the Installation page in the CookieOfficer panel and click “Verify now”. If it succeeds, you'll see the heading “Script detected and active”.

This test only confirms that the banner is present on the domain. To check whether cookie blocking works, run a scan of your site and go to the Cookies page. There should be no message above the list of detected items about trackers running after the visitor has rejected consent.

Common mistakes to avoid

  • Don't edit the parent theme. The next time the theme is updated, your changes will be overwritten and the banner will disappear from the site. Use a child theme or a plugin.
  • Don't paste the code after other scripts. Anything that runs before our code stays out of reach of automatic blocking.
  • Don't forget about optimisation plugins. WP Rocket, LiteSpeed Cache or Autoptimize can move our script to the end of the page, which disables cookie blocking. Exclude scripts containing cookieofficer from optimisation; you'll find the details in the article Excluding CookieOfficer code from WordPress cache plugins.
  • Don't count on being able to manually mark a script loaded by a plugin. In WordPress it reaches the page through the wp_enqueue_script function, so you can't add the attribute in the page code. What you need is a change on the theme side, made with the script_loader_tag filter, which has to change type to text/plain and add data-co-cat. The attribute alone won't hold anything back.

Didn't find an answer?

Write to us. We reply on working days, usually the same day.

Contact us