In part 1 we locked down wp-config.php, which protects the server. Security headers protect the other side: the browsers of the people reading your site. They are a few short lines per response, cost nothing in speed, and close off whole classes of attack such as clickjacking and content sniffing.
Before you start
- HTTPS working everywhereNeeded before you add HSTS
- A way to add PHP snippetsChild theme, must-use plugin or a snippets plugin
- Your browser’s developer toolsTo check the headers after each change
See what your site sends today
Open your home page, open the developer tools, go to the Network tab and reload. Click the first request (the page itself) and look at Response Headers. Most WordPress sites show none of the headers below. Keep this tab open: you will check it after every step.
The four safe headers
Add them with one snippet
Our snippet library has a ready-made version that uses WordPress’s
wp_headersfilter. It adds the four headers below to every page WordPress builds, and removes theX-Powered-Byheader that tells the world which PHP version you run.Know what each one does
X-Content-Type-Options: nosniffstops browsers guessing a file’s type, so a file uploaded as an image can’t be run as a script.X-Frame-Options: SAMEORIGINstops other sites loading yours inside a hidden frame and tricking visitors into clicking (clickjacking).Referrer-Policy: strict-origin-when-cross-originsends only your domain, not the full page address with search terms or IDs, when a visitor follows a link to another site.Permissions-Policyswitches off the camera, microphone and location for your pages, so an injected script can’t quietly ask for them.
Embedding your own pages elsewhere?X-Frame-Options: SAMEORIGINblocks your pages from being framed by any other domain, including your own app or a partner site. If you need that, leave this header out and use a Content Security Policyframe-ancestorsrule listing the allowed sites instead.Check the result
Reload the page with the Network tab open. The four headers should now appear under Response Headers, and
X-Powered-Byshould be gone.
Your home page, a blog post and a product page all show the four headers. Your site looks and works exactly as before.
The headers don't appear3 common fixes
- A page cache serves an old copy. Clear your cache plugin and any CDN, then reload.
- Only some pages have them.
wp_headersonly runs for pages WordPress builds. Images, CSS and JavaScript files are served by the web server directly. That is fine for these four headers, which matter on pages. - Your host or CDN adds its own. If you see a header twice, keep one. Set it either in WordPress or at the server, not both.
HSTS: always HTTPS, with no way back for a while
Strict-Transport-Security tells the browser: for the next year, only ever connect to this site over HTTPS, even if someone types http://. It stops attacks that downgrade a visitor to an insecure connection on public Wi-Fi.
add_filter( 'wp_headers', function ( array $headers ): array {
if ( is_ssl() ) {
// Start with one day; raise to a year once everything works.
$headers['Strict-Transport-Security'] = 'max-age=86400';
}
return $headers;
} );Once a browser has seen HSTS, it refuses plain HTTP to your site until max-age runs out, and you can’t recall it. Start with one day (86400). When every page, subdomain and image works on HTTPS, raise it to a year (31536000). Only add includeSubDomains if every subdomain has HTTPS, including old ones like mail. or shop..
Content Security Policy: start in report-only mode
A Content Security Policy (CSP) lists where your site may load scripts, styles, images and fonts from. If an attacker manages to inject a script, the browser refuses to run it. It is the strongest header of all, and the easiest to get wrong: a strict policy on a WordPress site with page builders, analytics and embeds will block things you need.
So start with Content-Security-Policy-Report-Only. The browser checks every rule and reports what it would block in the developer tools console, but blocks nothing.
Leave it running for a week, browsing the site as a visitor and as an editor. Add each legitimate source the console reports to the policy. Only when the console stays quiet do you switch the header name to Content-Security-Policy.
What to skip
X-XSS-Protection. Modern browsers removed the feature it controlled, and old versions of it could create problems of their own. Leave it out.Expect-CT. Retired. Browsers no longer use it.- Copying a strict CSP from another site. Every site loads different things. A copied policy either blocks your own scripts or allows so much that it protects nothing.
Summary
Add the four safe headers today with the snippet, check them in the Network tab, then add HSTS with a short max-age and raise it later. Start a Content Security Policy in report-only mode and tighten it over a week or two.
Next in this series: Part 3, protecting the login from bots and stolen passwords.
Check your understanding
Was this helpful?
Sorry about that. What was missing?
Thanks for your feedback!

