WordPress

Content Security Policy for WordPress: Report-Only First, Then Enforce

By , Lead SEO & Co-founder 9 min read
A security control room at night with monitors showing abstract allow and block bars, a rack where only cyan-lit cables are allowed through a filter panel, one crimson report light blinking and a laptop on the desk

Most content security policy WordPress tutorials skip the part that costs money: the first thing a strict policy usually breaks is your tracking. CSP is the strongest security header you can add and the easiest one to get wrong. The site looks fine. Forms submit. And for three weeks, Google Ads records no conversions because the browser quietly refused to load the tag.

The fix is the order you do it in: run the policy in report-only mode first, collect what it would have blocked, then enforce. This guide gives you that rollout plus a starter allowlist for GA4, Google Tag Manager and Google Ads, built from Google's own CSP guidance rather than a forum post.

If your site runs paid ads and you'd rather not experiment on it, send me the details in the maintenance questionnaire. I reply with a plan and a fixed quote within one business day.

How a content security policy for WordPress works

A CSP (Content Security Policy) is a header that lists where your pages are allowed to load things from: scripts, styles, images, fonts, frames, and the addresses scripts can send data to. Anything not on the list gets blocked by the browser. MDN's CSP guide covers the full spec. For a WordPress site, these directives (the named rules inside a policy) do most of the work:

Directive Controls Typical WordPress entries
default-src The fallback for anything not listed 'self'
script-src JavaScript files and inline scripts 'self', Google tag domains, sometimes 'unsafe-inline'
style-src CSS 'self', 'unsafe-inline', Google Fonts
img-src Images and tracking pixels 'self', data:, Google measurement domains
connect-src Where scripts can send data 'self', GA4 and Ads collection endpoints
frame-src What your page can embed YouTube, maps, GTM
frame-ancestors Who can embed your page 'self'

The report-only version, Content-Security-Policy-Report-Only, uses the same syntax but blocks nothing. MDN's report-only reference describes it as a way to monitor violations without enforcing the policy, so you can fix problems before you switch it on. It also notes the header doesn't work inside a <meta> tag, so it has to come from the server. Every would-be block shows up as a warning in the browser console instead. That's your safety net.

Why WordPress makes CSP harder than it looks

Three things make WordPress awkward:

  1. Inline scripts everywhere. WordPress core, page builders, form plugins and the Tag Manager snippet itself all print small scripts straight into the HTML. A policy without 'unsafe-inline' blocks every one of them unless each carries a nonce.
  2. Nonces and page caching don't mix. A nonce (a random value that must change on every page load) only works if each visitor gets fresh HTML. Full-page caching, which almost every fast WordPress site uses, serves the same HTML, with the same old nonce, to everyone.
  3. The dashboard is a different animal. The admin area and page builder editors load far more scripts than the public site. Applying your front-end policy there tends to break the editor first.

So the practical goal for most small business sites is not a perfect policy. It's a policy that locks down which outside domains can run code, who can frame you, and where forms can post, while accepting 'unsafe-inline' for scripts at first. That still blocks a big share of real attacks, like injected scripts loading from an attacker's domain.

Step 1: Add the CSP header to WordPress in report-only mode

Add this to .htaccess above # BEGIN WordPress. It applies the policy to the public site only and leaves /wp-admin/ and the login page alone, using Apache's <If> block (allowed in .htaccess, per the Apache core docs).

.htaccess / Apache
# CSP in report-only mode for the public site. Blocks nothing, logs violations to the console.
# Lines ending in "\" continue on the next line: no spaces after the backslash, or Apache errors.
# Could break: nothing in report-only mode. If the header never appears, your server may not
# support <If> in .htaccess. Ask your host.
<IfModule mod_headers.c>
<If "%{REQUEST_URI} !~ m#^/wp-(admin/|login\.php)#">
Header always set Content-Security-Policy-Report-Only "default-src 'self'; \
script-src 'self' 'unsafe-inline' https://www.googletagmanager.com https://tagmanager.google.com https://www.googleadservices.com https://www.google.com; \
style-src 'self' 'unsafe-inline' https://www.googletagmanager.com https://tagmanager.google.com https://fonts.googleapis.com; \
img-src 'self' data: https://www.googletagmanager.com https://ssl.gstatic.com https://www.gstatic.com https://*.google-analytics.com https://*.google.com https://*.g.doubleclick.net https://www.googleadservices.com https://pagead2.googlesyndication.com https://www.google.ca; \
connect-src 'self' https://www.googletagmanager.com https://*.google-analytics.com https://*.google.com https://*.g.doubleclick.net https://www.googleadservices.com https://pagead2.googlesyndication.com https://ad.doubleclick.net https://www.google.ca; \
font-src 'self' data: https://fonts.gstatic.com; \
frame-src 'self' https://www.googletagmanager.com; \
object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'self'"
</If>
</IfModule>

Two notes on that policy. 'self' means your own domain. And form-action 'self' will flag any form that posts to another domain, such as a newsletter form that sends straight to an email platform. That's exactly the kind of thing report-only mode exists to catch.

The GA4, Tag Manager and Google Ads allowlist

Here's where each domain in the starter policy comes from, per Google's tag security guide. Google lists most of these under script-src-elem (the part of script-src that covers script tags). If you only set script-src, browsers use it for both, which keeps the WordPress version simpler.

Tool script-src img-src connect-src frame-src
Google Tag Manager (incl. preview mode) www.googletagmanager.com, tagmanager.google.com www.googletagmanager.com, ssl.gstatic.com, www.gstatic.com - www.googletagmanager.com
GA4 (with Ads features and Google signals) www.googletagmanager.com *.google-analytics.com, *.google.com, *.g.doubleclick.net, www.google.<TLD> *.google-analytics.com, *.google.com, *.g.doubleclick.net, pagead2.googlesyndication.com www.googletagmanager.com
Google Ads conversions and remarketing www.googleadservices.com, www.google.com www.googleadservices.com, googleads.g.doubleclick.net, pagead2.googlesyndication.com, www.google.<TLD> adds ad.doubleclick.net www.googletagmanager.com

Three details that trip people up:

  • The <TLD> part is literal. CSP doesn't allow a wildcard at the end of a domain, so google.* is invalid. Google says to list each regional domain you need, and publishes the full list of Google domains. For a business with Canadian visitors, www.google.ca belongs in img-src and connect-src, which is why it's in the starter policy. Serve the UK or Australia too? Add www.google.co.uk or www.google.com.au.
  • Custom JavaScript variables in GTM need 'unsafe-eval'. Google's guide says they evaluate to undefined under a CSP without it. Before you loosen the policy, check whether a GTM custom template can do the same job, because templates don't need it.
  • The GTM snippet itself is an inline script. It's covered by 'unsafe-inline' in this policy. If you later move to nonces, Google's guide shows a nonce-aware version of the snippet that passes the nonce on to the tags it loads.

The table covers Google only. Meta Pixel, LinkedIn Insight, Microsoft Clarity, CallRail, a chat widget, YouTube embeds: each brings its own domains, and report-only mode will name them for you.

Step 2: Collect violations for a week

Open your site in Chrome or Firefox, press F12, and watch the Console tab. Every would-be block appears as a warning naming the directive and the blocked address. Then work through the pages that matter, in this order:

  1. Home page and one page of each template (service page, blog post, landing page)
  2. Every form, including a real test submission
  3. The thank-you or confirmation page, because that's where conversion tags fire
  4. Checkout and payment pages, if you sell online
  5. Tag Manager preview mode, with the container in debug

For each warning, decide: is this something I use? If yes, add the domain to the right directive. If you've never heard of it, find out why your site loads it before allowing anything. Browser extensions also cause console noise. A warning about a chrome-extension:// address is the visitor's add-on, not your site.

Clicking around only covers your own browser. To catch what real visitors hit, add a reporting endpoint: a URL that receives violation reports as JSON. MDN's report-to reference shows the modern setup (a Reporting-Endpoints header plus report-to in the policy) and recommends also keeping the older report-uri directive for browsers that haven't caught up. Report collection services exist for this, Report URI being the best known. Check their current plan limits before you send a busy site's traffic at one.

When you think the list is complete, paste the policy into Google's CSP Evaluator, which flags weak spots like overly broad wildcards.

Step 3: Enforce, then test the money pages again

Change one word in the header name, Content-Security-Policy-Report-Only to Content-Security-Policy, and save. Then repeat the checks from Step 2 with special attention to tracking:

  • Submit a test form and confirm the conversion shows in GA4's real-time report
  • Use Google Tag Assistant on the thank-you page and confirm the Google Ads conversion tag fired
  • Load the site on your phone over mobile data, not just office Wi-Fi

Keep the reporting endpoint running after you enforce. New plugins, a GTM change by your ads person, or a new chat widget will show up there first instead of as a mystery drop in leads. That's why I treat the CSP as part of the conversion tracking setup, not just a security setting.

Vlad's take on 'unsafe-inline'

Purists will tell you a CSP with 'unsafe-inline' in script-src is pointless. For a custom-built app, they're right. For a cached WordPress site with a page builder and five marketing tags, the realistic choice is not "perfect CSP or 'unsafe-inline'". It's "'unsafe-inline' with a tight domain list, or no CSP at all".

The tight list still matters. It stops injected code from loading a payload from a stranger's server, stops your pages being framed by other sites, kills old plugin content with object-src 'none', and makes forms post only to you. That's real protection, not a grade on a scanner.

Final thoughts

Report-only first, a week of real traffic, then enforce. Skip the first step and you'll find out what you broke from the Google Ads dashboard instead of the console.

This header pairs with the basics in my post on the WordPress security headers worth keeping, and the rest of the file lives in the .htaccess hardening template. If the tracking side is the part that worries you, my GA4 and Tag Manager setup service covers conversions end to end. For checks after every plugin and tag change, there's my monthly site care plan.

Content security policy WordPress FAQs

Will a content security policy break Google Analytics or Google Ads?

It can, and quietly. If GA4 or Google Ads domains are missing from script-src, img-src or connect-src, the browser blocks the tag and nothing appears on the page. Conversions just stop recording. Running the policy in report-only mode first, with Google's documented domains included, shows every would-be block in the console before anything is enforced.

What CSP do I need for Google Tag Manager?

At minimum, allow https://www.googletagmanager.com in script-src and frame-src, plus 'unsafe-inline' or a nonce for the inline container snippet. Preview mode also needs tagmanager.google.com, Google Fonts and two gstatic image domains. Every tag GTM fires (GA4, Ads, Meta and so on) then needs its own domains added on top, which is why report-only mode comes first.

Should I use a WordPress plugin to add a CSP header?

You can. Some security plugins set headers, and that's fine if you don't edit server files. Just make sure only one place sets the CSP. A plugin header plus an .htaccess header plus a CDN header means the browser enforces all of them at once, and debugging which one blocked something becomes miserable. Report-only mode is still step one either way.

Is 'unsafe-inline' safe to use in a CSP?

It weakens the policy, because injected inline scripts are allowed to run. On most cached WordPress sites it's still the realistic starting point, since core, plugins and the Tag Manager snippet all use inline scripts. A tight list of allowed domains, object-src 'none', frame-ancestors and form-action still block a lot. Nonces are the upgrade path for sites without full-page caching.

How long should I run CSP in report-only mode?

At least a week on a site with steady traffic, and long enough to cover every page type, form, checkout and thank-you page. Run longer if you have monthly campaigns, rarely used landing pages or a seasonal booking flow. The goal is that a full cycle of real visits produces no new violations you don't recognize.

Want a second pair of eyes on your policy before you enforce it? Tell me about your site and tags, and I'll come back with what I'd allow and what I'd question.

Want a second pair of eyes on your site?

Tell me what's going on in the wordpress maintenance questionnaire. I reply with a plan and a fixed quote within one business day.

Starting at $250/mo Start the questionnaire