Platform guides
How to add a cookie banner to WordPress
Put your consent banner's script in the head of every page, using a header plugin such as WPCode or a small functions.php snippet, then make sure analytics and marketing scripts wait for consent. The blocking step and your caching plugin are where most WordPress setups go wrong.
- Plain English
- Written for website owners
- Free to read
On this page
- Before you start: find out what your site loads
- Step 1: add the banner script to the head
- Step 2: block scripts until consent
- Step 3: make caching and optimization plugins play nicely
- Step 4: link your policies and a settings link
- Step 5: test like a visitor
- WordPress cookie banner checklist
- Frequently asked questions
To add a cookie banner to WordPress, paste your consent tool's script tag into the <head> of every page, either with a header and footer plugin like WPCode or with a wp_head hook in your child theme. Then change your analytics and marketing scripts so they only run after consent, and exclude the banner script from your caching plugin's JavaScript optimizations. A banner that shows up while Google Analytics or the Meta Pixel already loaded in the background doesn't meet EU or UK consent rules.
This guide uses PrivacyQuick's snippet as the example, but the same steps apply to most hosted consent platforms.
Before you start: find out what your site loads
WordPress sites collect scripts over time. Themes, page builders, form plugins, SEO plugins, embedded videos and analytics plugins can all set cookies or contact third parties. Before you configure anything, get a list:
- Run a free cookie scan of your site, logged out.
- List your active plugins and note which ones add tracking: analytics, pixels, chat, maps, video, A/B testing.
- Check your theme's settings for fields where someone pasted header scripts.
Sort what you find into categories. Our guide to cookie categories explains how to decide between necessary, analytics, functional and marketing.
Step 1: add the banner script to the head
The banner script should load on every page, as early in the <head> as possible, and before any analytics or ad tags. There are two common ways to do this in WordPress.
Option A: a header and footer plugin (WPCode)
WPCode (formerly "Insert Headers and Footers") is a widely used free plugin for adding code to your site. After installing it:
- Go to Code Snippets → Header & Footer in the WordPress admin. Menu names may differ slightly between plugin versions.
- Paste the snippet into the Header box.
- Save your changes.
<script src="https://cdn.privacyquick.com/v1/privacy.js" data-site-id="pq_xxxxxxxxxx" async></script>
Plugins like this output their header code through WordPress's wp_head action. If another plugin prints its tracking script earlier in the same hook, the banner may end up after it. Check the page source once you're done, and if ordering matters, use option B with an early priority.
Option B: a functions.php hook in a child theme
If you're comfortable editing code, add this to your child theme's functions.php or a small custom plugin. Don't edit the parent theme directly, because updates will overwrite your changes.
add_action( 'wp_head', function () {
echo '<script src="https://cdn.privacyquick.com/v1/privacy.js" data-site-id="pq_xxxxxxxxxx" async></script>' . "\n";
}, 1 );
The priority of 1 runs the hook before most plugins, which use the default priority of 10. This works with both classic and block themes, because block themes still call wp_head even though they don't have a header.php file.
Adding the banner only shows a banner. It doesn't stop any script from running. Step 2 is what actually holds back tracking until the visitor agrees.
Step 2: block scripts until consent
Prior blocking means non-essential scripts don't run, and don't even download, until the visitor allows their category. With PrivacyQuick, you change a script's type to text/plain and add a data-category. The banner swaps in a working script once the category is allowed. The script blocking docs have full examples for GA4, the Meta Pixel, YouTube and Hotjar.
Scripts you pasted yourself
For code in WPCode, a theme options panel or a Custom HTML block, edit the tag directly:
<!-- before -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"></script>
<!-- after -->
<script type="text/plain" data-category="analytics" async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"></script>
Do the same for the inline gtag() configuration block that follows it.
Scripts that plugins enqueue
Plugins usually load scripts with wp_enqueue_script(), so you can't edit the tag by hand. WordPress lets you change the final HTML with the script_loader_tag filter. First find the script's handle: view the page source, and look for the id attribute, which WordPress sets to the handle followed by -js. Then map the handles to categories:
add_filter( 'script_loader_tag', function ( $tag, $handle ) {
$categories = array(
'example-analytics' => 'analytics', // replace with real handles
'example-pixel' => 'marketing',
);
if ( ! isset( $categories[ $handle ] ) ) {
return $tag;
}
// Drop any existing type attribute from the opening tags.
$tag = preg_replace( '/(<script\b[^>]*?)\stype=([\'"])[^\'"]*\2/', '$1', $tag );
// Mark every script tag for this handle, including its inline helpers.
return str_replace(
'<script',
'<script type="text/plain" data-category="' . esc_attr( $categories[ $handle ] ) . '"',
$tag
);
}, 10, 2 );
The filter receives the script tag together with any inline code the plugin attached to it, so the inline setup code is blocked too. Test carefully: if a plugin's other code expects the blocked script to exist, it may throw errors until consent. Guard such calls, for example with if (window.fbq).
Plugins that print scripts directly
Some plugins echo their tracking code straight into the page rather than enqueueing it, so the filter above never sees it. Your choices are:
- use the plugin's own consent setting, if it has one that works with external consent tools,
- switch off the plugin's tracking feature and add the tracking code yourself in a tagged form, or
- use Google Tag Manager with consent settings, covered in our GTM consent guide.
Some plugins check consent through the WP Consent API, a WordPress feature plugin that passes consent categories between consent tools and other plugins. If you rely on a plugin that uses it, check whether your consent tool supports it.
Embeds
YouTube, Vimeo, Google Maps and social embeds load third-party content that can set cookies. For iframes, move the URL from src to data-src and add a data-category. The block editor's embed blocks generate their own iframe markup, so for embeds that need consent, use a Custom HTML block instead. Our guide on blocking cookies before consent compares the blocking techniques.
Step 3: make caching and optimization plugins play nicely
Caching and performance plugins are the most common reason a WordPress banner misbehaves. Page caching itself is usually fine, because a hosted banner makes its consent decisions in the visitor's browser and on its own server, not in the cached HTML. The problems come from JavaScript optimizations.
| Feature | Problem | Fix |
|---|---|---|
| Delay JavaScript until user interaction (e.g. WP Rocket's "Delay JavaScript execution") | The banner doesn't appear until the visitor scrolls or clicks | Add cdn.privacyquick.com or privacy.js to the plugin's delay exclusions |
| Combine or minify JavaScript | External or tagged scripts may be merged into one file, which breaks blocking | Exclude the banner script and any text/plain scripts |
| Defer all JavaScript | Tracking scripts may run before the banner is ready | Exclude the banner script, and confirm tagged scripts keep their type |
| Cloudflare Rocket Loader | Rewrites script tags and changes loading order | Add data-cfasync="false" to the banner tag, or turn Rocket Loader off |
Exclusion settings live in different places in each plugin, such as WP Rocket's File Optimization tab, LiteSpeed Cache's JS settings or Autoptimize's JavaScript options, and labels change between versions. After any change, purge every cache layer: the plugin, your host's server cache and your CDN. The guide to cookie banners and page speed explains how to keep the banner fast without these conflicts.
Step 4: link your policies and a settings link
WordPress has a built-in setting for your privacy policy page under Settings → Privacy. Some themes show it in the footer and on login and registration screens. Make sure that page describes your cookies, or link a separate cookie policy. PrivacyQuick's policy generator can embed a policy with a cookie table that updates from your scans.
Visitors also need a way to change their choice later. PrivacyQuick shows a floating cookie button by default. You can also add a footer link in a Custom HTML block or menu item:
<a href="#" onclick="window.PrivacyQuick && PrivacyQuick.open(); return false;">Cookie settings</a>
Step 5: test like a visitor
- Log out, or use a private window. Many analytics plugins skip tracking for administrators, which can hide problems.
- Load the home page and a few templates, such as a post, a page, a product page and a contact page.
- Open DevTools. Before you click anything, the Network tab shouldn't show requests to analytics or ad domains, and the Application tab shouldn't show their cookies. Our Chrome DevTools guide walks through this.
- Click Reject all and browse a few pages. Nothing non-essential should load.
- Change your choice to Accept all and confirm the tagged scripts load.
- Check the browser console for errors from plugins that expected a blocked script.
Running a store or need more than a banner? See cookie consent for WooCommerce and WordPress GDPR compliance beyond the cookie banner.
Comparing plugins? See how to choose a WordPress cookie consent plugin, and what to change on the WordPress privacy policy page.
WordPress cookie banner checklist
- The banner script is in the head of every page, before tracking scripts.
- Every analytics, marketing and functional script is blocked until its category is allowed, including plugin scripts and embeds.
- The banner script is excluded from delay, combine and Rocket Loader features, and all caches are purged.
- Your privacy or cookie policy lists the cookies you found, and a "Cookie settings" link or button is always available.
- You tested logged out, in a private window, with both Reject all and Accept all.
- You re-scan after adding plugins. PrivacyQuick's paid plans run scheduled scans and email you when a new tracker appears. You can start on the free plan.
Frequently asked questions
Does WordPress have a built-in cookie banner?
No. WordPress core sets cookies for logged-in users and, if commenters tick the box, for comment details, but it has no consent banner. You need a plugin or an external consent platform.
Should I use a cookie consent plugin or a hosted script?
Either can work. Plugins integrate with the WordPress admin, while a hosted script works the same way on WordPress and any other platform you run. What matters most is that it blocks non-essential scripts until consent and records consent.
Why doesn't my cookie banner appear after I install it?
The most common causes are a caching or optimization plugin serving an old page or delaying the banner script, and the snippet being added to only some templates. Clear all caches, exclude the banner script from JavaScript delay and combine features, and test in a private window.
Do I need a cookie banner if I only use Jetpack or Site Kit stats?
If the analytics tool sets cookies or similar identifiers on visitors' devices and you have visitors in the EU or UK, you generally need consent for it. Check what each tool stores with a cookie scan before deciding.
Check your own website in 30 seconds
Our free scanner lists every cookie and tracker your site sets, sorted into categories, before you add a banner.
This guide is general information, not legal advice. Laws and regulator guidance change; check the current rules for your situation or ask a lawyer.