Welcome to HowToShipIt — practical how-to guides for developers: code, AI tools, and servers, explained step by step.

WordPress Security Headers: Add Them Without Breaking Your Site (2026)

WordPress Security Headers: What They Are and Why Your Site Scores a D

WordPress security headers are instructions your server sends to every visitor’s browser before a single line of your theme loads. They switch on protections the browser leaves off by default: no framing your site inside someone else’s page, no guessing file types, no downgrading to plain HTTP, no loading scripts from anywhere on the internet. They cost nothing in server resources, and they close off entire classes of attack — clickjacking, content sniffing, some cross-site scripting — at the browser level.

Here is the awkward part: a fresh WordPress install sends almost none of them on your public pages. Run a scan on a default site and you will likely get a D or F grade. WordPress core does send a couple of headers on login and admin pages, but your homepage, posts, and pages go out bare. That is not a bug — headers are the server’s job, and WordPress assumes your server handles them. Most servers do not, unless you tell them to.

This guide shows you how to add the six WordPress security headers that matter, in an order that will not break your site, with copy-paste configs for Apache and Nginx, a PHP option, and a plugin option. Every value below was verified against official documentation and current guides on 14 November 2026.

The Six Headers That Matter

You do not need to memorise every header in the spec. Six do nearly all of the work for a typical WordPress site:

Header What it does Value to start with
X-Content-Type-Options Stops the browser guessing a file’s type from its content instead of trusting the declared Content-Type nosniff
X-Frame-Options Stops your site being loaded inside an iframe on someone else’s domain (clickjacking) SAMEORIGIN
Referrer-Policy Controls how much of your URL is sent to other sites when a visitor clicks away strict-origin-when-cross-origin
Permissions-Policy Switches off browser features your site does not use, like camera and microphone camera=(), microphone=(), geolocation=()
Strict-Transport-Security Tells the browser to only ever reach your site over HTTPS max-age=31536000; includeSubDomains
Content-Security-Policy Controls exactly which sources may load scripts, styles, images, and fonts Report-only first — see below

Two honest caveats before you start. First, headers do not fix vulnerabilities. A content security policy does not replace input sanitisation; it limits the damage when your escaping fails. Second, the two aggressive headers — HSTS and CSP — can break your site if you flip them on all at once. That is why the order below matters more than the values themselves.

Step 0: Scan Your Site First

Before changing anything, get a baseline. Go to securityheaders.com, the free scanner created by security researcher Scott Helme, enter your domain, and click Scan. You get a letter grade from A+ to F in seconds, plus a list of exactly which headers are missing. A default WordPress site usually lands on D or F. That grade is your before photo.

Mozilla Observatory is the other scanner worth knowing — now hosted on MDN, it scores headers plus other security configuration. Use one or both; the point is to have a number you can compare against after each change.

You can also check headers directly from your terminal, which is the fastest way to confirm a change actually took effect:

curl -sI https://yoursite.com | grep -iE 'strict-transport|x-frame|x-content|referrer|permissions|content-security'

If a header does not show up in that output, the browser is not receiving it either, no matter what your config file says.

The Rollout Order That Will Not Break Your Site

Add the headers in this sequence, verifying each step before moving to the next:

  1. X-Content-Type-Options and Referrer-Policy first. Both are effectively risk-free and almost never interact with themes or plugins.
  2. Permissions-Policy next. Disable only the browser features you know you do not use. If you are unsure whether a plugin needs geolocation or camera access, leave that directive out rather than guessing.
  3. X-Frame-Options (or CSP frame-ancestors). Set it to SAMEORIGIN unless you deliberately embed your site in an iframe on another domain you control, such as a preview environment.
  4. HSTS with a short max-age first. Start with a day (max-age=86400), confirm HTTPS works everywhere on your site including any subdomains you reference, then raise the value over weeks (a year, max-age=31536000, is the common long-term setting).
  5. CSP last, and always in report-only mode first. Use Content-Security-Policy-Report-Only to see what would have been blocked without actually blocking anything, watch your browser console for violations, adjust the policy to allow the domains you genuinely need, and only then switch to the enforcing header.

This order exists because the first four headers are set-and-forget, while the last two punish haste. HSTS with a long max-age on a site whose HTTPS is not fully working locks visitors out. CSP enforced on day one breaks your admin, your fonts, and your analytics in ways that look like random plugin failures.

Method 1: Apache .htaccess (Most Shared Hosts)

If your host runs Apache — most cPanel and shared hosts do — add this to the .htaccess file in your WordPress root. Put it outside the # BEGIN WordPress block, because WordPress rewrites that section whenever it updates its rules, and your headers would vanish:

<IfModule mod_headers.c>
    Header always set X-Content-Type-Options "nosniff"
    Header always set X-Frame-Options "SAMEORIGIN"
    Header always set Referrer-Policy "strict-origin-when-cross-origin"
    Header always set Permissions-Policy "camera=(), microphone=(), geolocation=()"
    Header always set Strict-Transport-Security "max-age=86400; includeSubDomains"
</IfModule>

Three details matter here. First, the word always is not decoration. Without it, Apache skips the header on error pages and redirects, so your 404s and your HTTP-to-HTTPS redirect go out unprotected. Use Header always set every time. Second, the <IfModule> wrapper means Apache silently skips the whole block if mod_headers is missing, rather than throwing a 500 error — safer, but it also means a missing module fails silently, so verify with curl -I afterwards. Third, AllowOverride FileInfo must be on for your directory, otherwise Apache ignores Header directives in .htaccess entirely.

Note the HSTS value: max-age=86400, one day. That is deliberate. Ramp it up over weeks once you are sure HTTPS works everywhere — only then move to the one-year value, and only add preload after reading the HSTS section below.

Method 2: Nginx Server Block

Nginx ignores .htaccess entirely, so the equivalent goes in your site’s server block. If you migrated from Apache to Nginx, this is the step people forget — and it is why a site can 404 or lose its headers after a move:

server {
    listen 443 ssl;
    server_name yoursite.com www.yoursite.com;

    add_header X-Content-Type-Options "nosniff" always;
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header Referrer-Policy "strict-origin-when-cross-origin" always;
    add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;
    add_header Strict-Transport-Security "max-age=86400; includeSubDomains" always;

    # ... rest of your server block
}

After editing, test the config and reload — never restart blind:

sudo nginx -t && sudo systemctl reload nginx

One Nginx gotcha: add_header directives are inherited only if the current block defines no add_header of its own. If a location block sets its own headers, it silently drops the server-level ones. If your security headers vanish on some pages but not others, a location block overriding them is the first place to look.

Method 3: PHP via the send_headers Hook

If you do not have server access, or you want the headers version-controlled with your theme, you can set them from PHP. WordPress provides the send_headers action hook for exactly this — it fires once the requested HTTP headers have been sent, and it is documented as the hook to use to add additional headers to the outgoing response. Since WordPress 6.1 it fires after the query is parsed, so conditional tags like is_singular() work inside it.

Add this to your child theme’s functions.php — a child theme, not your main theme, or the next theme update wipes it out. Better still, put it in a tiny custom plugin so it survives theme changes too:

function htsi_add_security_headers() {
    header( 'X-Content-Type-Options: nosniff' );
    header( 'X-Frame-Options: SAMEORIGIN' );
    header( 'Referrer-Policy: strict-origin-when-cross-origin' );
    header( 'Permissions-Policy: camera=(), microphone=(), geolocation=()' );
    header( 'Strict-Transport-Security: max-age=86400; includeSubDomains' );
}
add_action( 'send_headers', 'htsi_add_security_headers' );

Know the limits of this method. It only covers responses WordPress itself generates. Static files — images, CSS, JavaScript served directly by the web server — never pass through PHP, so they get no headers. And if a caching plugin or your CDN serves a cached page, that page is served before your hook ever runs. For full coverage, the server-level methods above are better; use PHP when server config is genuinely out of reach.

Method 4: Use a Plugin

If editing server files is not something you want to do, a plugin is the honest answer. Really Simple SSL can set the common headers from its settings page, and the dedicated Headers Security Advanced & HSTS WP plugin focuses exclusively on security headers, including CSP and HSTS configuration through its interface.

Plugins are the right choice when you have no server access or you manage client sites and want a repeatable setup. The trade-off is the same as the PHP method: plugin-set headers only apply where WordPress runs, and a plugin that sets a header your server also sets will send it twice. Pick one layer and stick to it.

HSTS: The One Header That Can Lock You Out

Strict-Transport-Security tells the browser to never load your site over plain HTTP again, for as long as max-age says. That is powerful and, if you get it wrong, punishing: while the policy is active, browsers will refuse plain-HTTP connections until it expires, and you cannot remotely undo that for visitors who already received the header.

So follow the ramp. Start at max-age=300 (five minutes) for a week and watch what breaks — a subdomain whose certificate is not renewing, an old integration generating http:// links. Then a week (604800), then a month (2592000), then a year (31536000). Only add includeSubDomains once every subdomain, including staging and internal tools, serves valid HTTPS. If you have not already moved your site fully to HTTPS, do that first — our guide to moving WordPress from HTTP to HTTPS without breaking anything covers the order of operations.

Then there is preload, the part people enable without understanding. Adding preload to the header does nothing by itself — it is a petition, not the act. Getting on the list is a separate submission at hstspreload.org, and the requirements are strict: a valid certificate, an HTTP-to-HTTPS redirect on the same host, every subdomain served over HTTPS, and the header with max-age of at least 31536000 plus includeSubDomains and preload. Once accepted, your domain is compiled into Chrome, Firefox, Safari, and Edge. Removal is possible but takes months to propagate through browser releases. Only preload when your HTTPS setup is boringly reliable.

CSP: The Powerful One — Start in Report-Only Mode

Content-Security-Policy is the most powerful header and the hardest to get right. It tells the browser exactly which domains may serve scripts, styles, images, and fonts, and it can block inline scripts outright. On a WordPress site, that last part is the problem: the admin, the Customizer, and many plugins rely on inline scripts and styles. A strict policy enforced on day one will break your wp-admin in ways that look like a plugin conflict.

Start with Content-Security-Policy-Report-Only. The browser evaluates the policy, reports every violation to your console, and blocks nothing. Here is a realistic starter policy for a WordPress site using Google Fonts and Google Analytics — adjust the domains to match what your site actually loads:

Content-Security-Policy-Report-Only: default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval' https://www.google-analytics.com https://www.googletagmanager.com; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; img-src 'self' data: https:; font-src 'self' https://fonts.gstatic.com; connect-src 'self' https://www.google-analytics.com; frame-ancestors 'self'; base-uri 'self'; form-action 'self'; object-src 'none'

Yes, 'unsafe-inline' and 'unsafe-eval' weaken the policy — that is the honest trade-off on WordPress today. A policy that breaks your admin is worse than a looser policy that actually ships. Tighten it over time: move plugin scripts to external files where you can, and drop 'unsafe-eval' first, since fewer plugins genuinely need it than claim to.

Watch the browser console for a week. Every violation is a resource your site tried to load that the policy would block. Add the domains you genuinely need — your CDN, your analytics provider, your fonts — and nothing else. Only when the console is quiet do you change the header name from Content-Security-Policy-Report-Only to Content-Security-Policy and start enforcing. MDN’s CSP reference is the authoritative place to check any directive you are unsure about.

One more thing: frame-ancestors 'self' in a CSP replaces X-Frame-Options in modern browsers, but the old header is still what most scanners grade, so keep both. They do not conflict.

Verify Your Headers Are Actually Live

Adding headers to a config file is not the same as sending them. Verify at three levels:

  1. Terminal: curl -sI https://yoursite.com and look for each header in the response. Check a post page and an image URL too — headers set in PHP will not appear on the image.
  2. Browser: open DevTools, go to the Network tab, reload, click the main document request, and read the Response Headers. This is also where CSP violations show up in the console during your report-only week.
  3. Scanner: re-run securityheaders.com. A site that started at D or F should now be at A or A+ once all six headers are live. If the grade does not move, something is sending the headers somewhere else — a CDN, a caching plugin, or a second layer duplicating them.

If you use Cloudflare or another CDN, remember the CDN sits in front of your server. Headers your origin sets pass through, but the CDN can also add, strip, or override them. When a header is missing on the live site but present on the origin, the CDN layer is where to look. The same applies to page caching: a cached page was generated before your change, so purge every cache after editing headers.

Five Mistakes That Break Sites

  1. Setting the same header at two layers. Server config plus a plugin, or .htaccess plus Nginx, sends the header twice. For CSP, two policies both apply, and the stricter one wins — which is how a “safe” plugin policy suddenly blocks your fonts.
  2. Enabling HSTS before HTTPS works everywhere. One subdomain with an expired certificate, one old http:// link in a widget, and your visitors get errors you cannot undo until max-age expires.
  3. Enforcing CSP on day one. Always report-only first. The console tells you what your site actually loads; your assumptions about it are almost certainly wrong.
  4. Putting rules inside the # BEGIN WordPress block. WordPress rewrites that block. Your headers disappear at the next permalink save, and you will spend an afternoon wondering what changed.
  5. Forgetting the CDN and the cache. Headers are only real when the browser receives them. Purge caches, check the live response with curl, and re-scan after every change.

The Bottom Line

WordPress security headers are the cheapest hardening you will ever do: six headers, one afternoon, no plugins required if you have server access. Start with the four safe ones, ramp HSTS slowly, and let CSP earn its enforcement in report-only mode. A site that ships an A on securityheaders.com is not invulnerable — headers never replace updates, strong passwords, and backups — but it has closed off entire attack classes for free, and that is a trade worth making.

Header values and submission requirements in this post were verified on 14 November 2026 against MDN, the WordPress developer reference, hstspreload.org requirements, and current security-header guides.

Further Reading & References

Leave a Comment