Headers for WordPress security are HTTP directives sent by your server, CDN, or host to the browser. They don’t fix a vulnerability in WordPress. However, they reduce certain avenues for abuse in the browser, including loading unintended resources, embedding your pages in an external frame, and risky interpretation of files.
The right approach is to add them gradually. An overly strict policy can block a form, a web font, a payment gateway, or the admin editor. Before making any changes, therefore, keep a verifiable copy of your site. You can follow the method described in testing a WordPress backup restore.
Which security headers should be configured first?
For most public WordPress sites, four headers deserve priority attention: Content-Security-Policy, Strict-Transport-Security, X-Content-Type-Options and Referrer-Policy. Depending on your usage, you may also want a rule to prevent embedding in an iframe.
The OWASP sheet on HTTP headers describes these mechanisms as complementary protections against risks such as XSS, clickjacking, and certain information disclosures. They do not replace updates, least privilege, or data validation.
| Header | Primary role | Caution point |
|---|---|---|
Content-Security-Policy | Limits sources for scripts, styles, images, and frames. | May break plugins or external scripts. |
Strict-Transport-Security | Asks the browser to use HTTPS after receiving the rule. | A certificate error or an HTTP subdomain may become inaccessible. |
X-Content-Type-Options | Prevents the browser from guessing a different MIME type. | The types Content-Type must be correct. |
Referrer-Policy | Reduces the URL details sent to external sites. | Some analytics tools may receive less information. |
Content-Security-Policy: a powerful protection, but tricky to deploy
A Content Security Policy, often called a CSP, tells the browser which sources are allowed to load and execute content. It can limit JavaScript scripts, stylesheets, images, fonts, iframes and connections to external services.
This approach helps mitigate certain script-injection scenarios. However, a CSP does not fix an existing vulnerability. It instead adds a browser-side constraint. The OWASP page on CSP recommends delivering the policy in the HTTP header, as this method supports the full range of CSP features.
Why WordPress asks for a testing phase
WordPress, its theme and many plugins can produce inline scripts or styles. External services can also load fonts, a map, a video, a booking tool or a payment service. Therefore, a policy that only allows 'self' often blocks legitimate elements.
Start with Content-Security-Policy-Report-Only. The browser then reports violations without blocking resources. OWASP explains this mode is precisely for observing gaps before enforcing a blocking policy. Check the browser console and test your important pages: home, form, search, cart, checkout and login.
Avoid adding 'unsafe-inline' or 'unsafe-eval' simply to make errors disappear. These exceptions lessen the policy’s value. A robust CSP instead requires inventorying needed resources, then fixing or containing inline scripts. For code that goes through its script APIs, WordPress also allows adding attributes to inline tags <script> as shown in the documentation for wp_get_inline_script_tag(). However, this integration does require configuration appropriate to your theme and plugins.
Preventing unwanted embedding in an iframe
You can use frame-ancestors 'self' in your CSP to allow only pages from the same origin — same protocol, host and port — to embed your pages in an iframe. If no embedding is necessary, frame-ancestors 'none' is more restrictive.
This rule can prevent legitimate uses, for example a partner portal or an embedded preview. Check your needs before enabling it. OWASP considers frame-ancestors as the modern approach to replace X-Frame-Options in browsers that support CSP.
HSTS: enforce HTTPS only when your domain is ready
Strict-Transport-Security, or HSTS, tells the browser to communicate with your domain over HTTPS for a set period. After receiving this header via HTTPS, the browser converts future HTTP attempts into HTTPS requests.
HSTS is relevant if your site already runs entirely on HTTPS. However, do not add includeSubDomains until all your current and planned subdomains also work with a valid certificate. The OWASP HSTS guidance notes that an overly broad configuration can block pages or subdomains that can only respond over HTTP.
Deploy a short duration first, for example max-age=86400, and validate the behavior. Increase the duration only once redirects, certificates, and subdomains are confirmed. Never send HSTS on an HTTP response: the browser must receive this directive only over HTTPS.
Two simple headers to add without complicating the site
X-Content-Type-Options: nosniff
X-Content-Type-Options: nosniff asks the browser to respect the content type declared by the server instead of guessing. This setting reduces the risk that a file declared non-executable will be treated as a script. It remains important to serve the correct MIME types for your files.
In practice, this header is minimally intrusive for a standard WordPress site. It should therefore be among the first settings checked.
Referrer-Policy: strict-origin-when-cross-origin
Referrer-Policy: strict-origin-when-cross-origin sends the full URL for same-origin requests. To another origin, the browser generally sends only the origin — the protocol, host, and port. It sends nothing when going from HTTPS to HTTP.
This policy offers a good compromise for a public site. It notably avoids sending certain page parameters to external sites. Still, check your analytics tools and marketing integrations after adding it.
Where do you configure headers in a WordPress site?
Ideally configure headers at the web server, CDN, or reverse proxy level. That way HTML pages, error responses, and files served directly follow a consistent rule. WordPress can send headers via its hook send_headers, but this option doesn’t necessarily cover all cached responses or responses generated before PHP.
On Nginx, the add_header directive is used to add header fields. Its documentation notes that the always option also applies the header to responses whose status code is not in the default list. By default, as soon as a child block contains its own add_header, directive, it no longer inherits the add_header directives from the parent level. See the official Nginx headers module documentation before adding a rule in a location.
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Strict-Transport-Security "max-age=86400" always;This example is a starting point, not a universal configuration. Add HSTS only on your HTTPS host. Also, build the CSP separately after testing it in report-only mode.
Don’t turn CORS into a fake security header
Access-Control-Allow-Origin addresses a specific need: allowing JavaScript hosted on another origin to read a response. It’s not a header to add globally to «secure» WordPress. A value * can unnecessarily expose responses intended for a more restricted context.
WordPress already handles CORS headers for its REST API. Its documentation explains that public endpoints can be read from other sites, and that stricter restrictions can break some authentication methods. Before changing CORS, consult the WordPress REST API frequently asked questions and test Gutenberg, forms that use this API, and any external application connected to the site.
A cautious deployment method
- Inventory the external domains used by your theme, plugins, and landing pages.
- Add first
X-Content-Type-OptionsandReferrer-Policy. - Enable HSTS with a short duration, only after you’ve validated HTTPS everywhere.
- Deploy a CSP in
Report-Only mode, then fix the useful violations. - Test public pages and the admin area with a non‑admin account.
- Check the actual headers in the browser dev tools and after a cache purge.
Finally, headers complement other protective measures. Keep your components up to date, limit privileges, and remove unused tools. To expand your approach without mixing topics, see methods for securing WordPress without a plugin as well as signs of an abandoned WordPress plugin.









