Security · Guide
How to Add a Content Security Policy to WordPress
Short answer
To add a Content Security Policy to WordPress, first send it as a Content-Security-Policy-Report-Only header so the browser logs what it would block without breaking your site. Then add the sources your theme and plugins need, and switch the same policy to the enforcing Content-Security-Policy header. You can set the header with a CSP plugin, a few lines of PHP, your server settings, or Cloudflare Transform Rules.
Key takeaways
- A Content Security Policy is an HTTP header that lists which sources may load scripts, styles, images, and frames.
- Start in report-only mode so the browser logs violations without blocking anything on your live site.
- A strict policy uses a fresh nonce or a hash for each script instead of a long list of trusted hosts.
- WordPress core and many plugins use inline scripts, so a strict policy needs care before you enforce it.
- Check the header with curl, the browser console, and Google's CSP Evaluator before and after you enforce it.
- Send the header on every page, not only the home page, because OWASP recommends it on all responses.
What is a Content Security Policy?
A Content Security Policy is an HTTP response header that tells the browser which sources may load scripts, styles, images, and other resources on a page.
The policy is a list of directives separated by semicolons. Each directive names one resource type and the sources allowed for it.
MDN's Content-Security-Policy reference lists fetch directives such as default-src, script-src, style-src, img-src, and connect-src. A source that matches no allowed entry is blocked, and the browser logs a violation in the console.
CSP is defined by the W3C in Content Security Policy Level 3. OWASP's Content Security Policy cheat sheet notes that the header is supported by Firefox 23 and later, Chrome 25 and later, and Opera 19 and later, as of November 2026.
What does a Content Security Policy protect WordPress from?
A Content Security Policy limits the damage from cross-site scripting by stopping injected scripts from running in your visitors' browsers.
OWASP's Content Security Policy cheat sheet lists five ways CSP limits cross-site scripting: it blocks inline scripts, remote scripts, eval-style code, form posts to other sites, and embedded objects.
It also blocks clickjacking through the frame-ancestors directive, which OWASP notes has made the older X-Frame-Options header obsolete.
A firewall and a CSP do different jobs. Cloudflare WAF rules for WordPress filter bad requests before they reach WordPress, while a CSP runs in the visitor's browser.
A CSP does not fix the flaw itself, so treat it as a second layer of defense, not a replacement for updates and safe coding. OWASP states plainly that CSP should not be relied on as the only defense against cross-site scripting.
What do you need before you add a CSP to WordPress?
You need a backup you can restore, access to your hosting or Cloudflare dashboard, and a list of the third-party services your site loads.
- A backup you can restore: see how to restore a backup with UpdraftPlus before you change any header.
- Admin access, plus server, host panel, or Cloudflare access for the method you choose.
- A list of services that load scripts or frames: analytics, fonts, video embeds, maps, chat, and payment forms.
- A staging copy or a quiet hour, since a wrong policy can break visible parts of the site.
- A browser with developer tools, so you can read the console messages a policy produces.
Install plugins only from WordPress.org or the vendor's own site, and skip nulled copies. Nulled WordPress plugins can hide added code and receive no updates, so they are a poor base for security work.
How do you add a CSP with a WordPress plugin?
A CSP plugin is the easiest route for most WordPress sites because it scans your pages and builds the policy for you.
- Go to Plugins → Add New in wp-admin, search for “fepo CSP & Security Headers”, then click Install Now and Activate.
- Open “fepo CSP & Headers” in the admin menu and click “Start scan”.
- Wait for the scan to finish and leave the policy in Report-Only mode.
- Watch the Reports tab for a few days and add any missing sources the reports show.
- Switch the mode to Enforce on the Policy tab once no new reports come in.
The plugin opens up to 500 pages during its scan and records the sources they load. Its reports endpoint stores reports without an IP address or user agent and deletes them after 30 days, according to its WordPress.org plugin page.
The plugin sends the policy as an HTTP header, never as a meta tag. Other plugins take the same approach: Codo Content Security Policy and BoundaryGuard Headers both start in report-only mode and add enforcement later.
How do you add a CSP with code?
You can send a CSP from WordPress itself with the wp_headers filter or the send_headers action, added in a child theme or a small custom plugin.
The wp_headers filter lets you change the headers before WordPress sends them, and it has existed since WordPress 2.8.0.
The send_headers action fires after WordPress has sent its own headers. Since WordPress 6.1 it runs late enough that conditional tags such as is_single work inside it.
add_filter( 'wp_headers', function( $headers ) {
$headers['Content-Security-Policy-Report-Only'] = "default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; font-src 'self'; connect-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'self';";
return $headers;
} );
Put this code in a child theme's functions.php file or a site-specific plugin, never in the parent theme, because a theme update overwrites parent theme files.
The wp_headers reference warns that the filter will not change headers on pages served from a cache plugin or a third-party cache, because the cached page is served before the filter runs.
Change the header name to Content-Security-Policy when your report-only logs are clean, and keep the rest of the value the same.
How do you add a CSP in .htaccess?
On Apache or LiteSpeed hosting, you can set the header in the .htaccess file in your site's root folder, so it applies to every response the server sends.
Header always set Content-Security-Policy-Report-Only "default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; font-src 'self'; connect-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'self';"
Wrap that line in an IfModule block for mod_headers if your host needs it, and use Header always set so the header is sent even on error pages. If your host runs Nginx, there is no .htaccess file, so use the PHP method or Cloudflare instead.
A typo in .htaccess can return a 500 error on every page, so keep a copy of the file and check the site right after you save it.
How do you add a CSP through Cloudflare?
If your site already runs through Cloudflare, you can add the header at the edge with a Response Header Transform Rule and no code.
- In the Cloudflare dashboard, go to the Rules Overview page, select Create rule, then Response Header Transform Rule.
- Set the operation to Set static, enter Content-Security-Policy-Report-Only as the header name, and paste your policy as the value.
- Click Deploy and load your site to confirm the header arrives.
Cloudflare's documentation states that a single rule can modify up to 30 HTTP response headers, so one rule can carry your CSP and your other security headers together.
A Transform Rule sets a fixed value, so it cannot generate a fresh nonce for every response. web.dev's strict CSP guide requires a nonce to be newly generated for every response, so use the PHP method for nonce-based policies.
Set up HTTPS before you tighten a policy, since mixed content and redirect problems hide CSP errors. See the Cloudflare SSL settings for WordPress if your site is not fully on HTTPS yet.
What should your first CSP policy contain?
Start with a short policy that allows your own origin, blocks embedded objects, and limits framing, then add only the hosts your site truly loads.
| Directive | What it controls | Starting value |
|---|---|---|
| default-src | Fallback for fetch directives you do not set | 'self' |
| script-src | Where JavaScript may load and run | 'self' |
| style-src | Where stylesheets and inline styles may load | 'self' |
| img-src | Where images and favicons may load | 'self' data: |
| font-src | Where fonts may load | 'self' |
| connect-src | Where fetch, XHR, and WebSocket calls may go | 'self' |
| object-src | Where plugins and embedded objects may load | 'none' |
| base-uri | Which URLs the base element may use | 'self' |
| frame-ancestors | Which parents may embed the page | 'self' |
| form-action | Where forms may submit | 'self' |
MDN's Content-Security-Policy reference notes that default-src is the fallback for the other fetch directives, so an img-src you omit takes its rule from default-src.
MDN also notes that a directive set to the single value 'none' blocks that resource type completely, which is why object-src 'none' is a safe start.
MDN warns that developers should avoid 'unsafe-inline' because it defeats much of the purpose of having a CSP. WordPress core and many plugins emit inline scripts and styles, so a first policy often keeps 'unsafe-inline' in style-src while you work toward nonces for scripts.
How do you test a CSP in report-only mode?
Report-only mode is the safe way to test a policy because the browser logs what it would block but blocks nothing.
Send the same policy with the header name Content-Security-Policy-Report-Only instead of Content-Security-Policy. OWASP's Content Security Policy cheat sheet describes this header as non-blocking, and notes that reports go to the endpoint named by report-to or report-uri.
MDN notes that report-to is the current reporting directive and report-uri is superseded, so declare both until report-to support is broad enough to rely on alone.
- Deploy the policy in report-only mode on staging or on live.
- Visit your key pages: the home page, a post, a form page, and checkout if you sell online.
- Open the browser console and note each violation, including the blocked source and the directive.
- Add missing sources that belong to your theme, plugins, or services, and repeat the visit.
- Run report-only for several days of real traffic before you enforce the policy.
Browsers support using both headers together, so you can keep a stricter draft in report-only mode after you enforce a looser policy. OWASP notes this pattern works without issues.
How do you check that your CSP is working?
Check the header in three places: the raw response, the browser console, and a policy evaluator.
- Run
curl -sI https://yoursite.comand check that a content-security-policy line appears in the output. - Open the site in your browser, press F12, open the Console tab, and look for CSP violation messages.
- Paste the policy into Google's CSP Evaluator and read its findings.
web.dev notes that Lighthouse version 7.3.0 and higher can check whether your site has a CSP and whether it is strict enough, using the Best Practices audit with the experimental preset flag.
If the header is missing, check your cache first. The wp_headers reference warns that cached pages are served before the filter runs, so a cache plugin can hide your header.
How do you fix a CSP that breaks your site?
When a page breaks after you enforce a policy, the browser console names the blocked source and the directive that blocked it, so start there.
- Open the console, copy the blocked address and the directive name.
- Decide if the source belongs to your theme, a plugin, or a service you use.
- Add hosts you trust to the matching directive, and remove or replace the rest.
- Clear every cache, reload the page, and check the console again.
Do not fix breakage by adding a long list of hosts. web.dev's strict CSP guide warns that host-allowlist policies can be bypassed in most configurations, so a long allowlist is a weak fix.
If the breakage is in wp-admin or the editor, scope the policy to the front end only while you fix the front end first, then tighten the admin policy next.
What mistakes should you avoid?
The most common CSP mistakes are enforcing on day one, copying a policy from another site, and setting the header in two places at once.
- Enforcing a policy before any report-only testing, so the first visitor finds the breakage.
- Copying a policy from another site, since your plugins and services load different sources.
- Setting the header in two places, such as a plugin and .htaccess, which can silently change the policy visitors receive.
- Adding 'unsafe-inline' and 'unsafe-eval' to script-src as a permanent fix instead of a stopgap.
- Forgetting embeds: video, maps, payment forms, and chat widgets each need their hosts in the matching directive.
- Never re-checking the policy after you add a plugin, since new plugins bring new sources.
Keep the rest of your security in place too: updates, backups, and a firewall all sit beside a CSP, not behind it. Set up the Wordfence firewall if you have not added that layer yet.
Frequently asked questions
Do I need a Content Security Policy if I already use a firewall?
Yes, because they do different jobs. A firewall filters bad requests before they reach WordPress, while a CSP runs in the visitor's browser and limits what a page may load or run. A firewall cannot stop an injected script that is already inside a page your server sent, so use both layers together.
How long should I run report-only mode before enforcing a policy?
Run it for several days of real traffic, and visit every key page type yourself: the home page, a post, a form, and checkout if you sell online. Enforce the policy only when the reports stop showing sources you need, and keep watching the console for a week after you switch.
Why does my site break after I enforce a Content Security Policy?
The policy blocks a script, style, font, or frame your theme or plugin loads from a source you did not list. Open the browser console to see the blocked address and the directive that blocked it, add the source you trust to that directive, clear your caches, and reload the page.
Can I use a Content Security Policy with analytics, video embeds, and payment forms?
Yes. Add the hosts those services load from to the matching directives: scripts to script-src, frames to frame-src, and data calls to connect-src. Test each service in report-only mode first, since a payment form that cannot load its scripts will fail at checkout.
Should I set a Content Security Policy with a meta tag instead of a header?
Use a header when you can. A meta tag cannot use report-only mode, frame-ancestors, sandbox, or a violation reporting endpoint, so it covers less than a header. Use a meta tag only when your host or CDN gives you no way to set response headers.
Does a Content Security Policy slow down my site?
No. The policy is one short header sent with each page, and the browser checks sources it already loads, so visitors will not notice a delay. The real cost is your time: building the policy, reading the reports, and fixing the breakage it finds.
Sources
- Content-Security-Policy (CSP) header MDN Web Docs
- Mitigate cross-site scripting (XSS) with a strict Content Security Policy (CSP) web.dev
- Content Security Policy OWASP Cheat Sheet Series
- Response Header Transform Rules Cloudflare Docs
- send_headers Developer.WordPress.org
- wp_headers Developer.WordPress.org
- fepo CSP & Security Headers WordPress.org
Is your product missing, or something out of date?
Tell us what to add or correct. We read every note and reply when we need a source. A message does not buy a ranking.
Thanks — we review requests within 3 business days.
