Setting up CookieOfficer on WordPress
Ways to add the CookieOfficer code in WordPress: with a plugin or in the theme.
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.
- Go to Plugins > Add Plugin and install any plugin for inserting code into the page header.
- Open the plugin settings and find the field that corresponds to the
<head>section, most often labelled Header. - Paste the CookieOfficer code at the very beginning of the field, before any other scripts.
- 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.
- Go to Appearance > Theme File Editor.
- Make sure your child theme is selected at the top, not the parent theme.
- In the file list on the right, open Theme Header (
header.php). - Paste the CookieOfficer code right after the opening
<head>tag, before any other scripts and before thewp_head()call. - 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
cookieofficerfrom 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_scriptfunction, so you can't add the attribute in the page code. What you need is a change on the theme side, made with thescript_loader_tagfilter, which has to changetypetotext/plainand adddata-co-cat. The attribute alone won't hold anything back.