Security Guide Intermediate 4 min

Hardening WordPress, Part 2: Security Headers That Actually Protect Visitors

Five HTTP headers that tell the browser how to protect your visitors: no MIME sniffing, no clickjacking, less data leaked to other sites, and HTTPS every time. Plus a safe way to start a Content Security Policy.

Hardening WordPress · Part 2 of 4
TutorialIntermediate15 minTested on WordPress 7.1Updated November 13, 2026

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

  1. Add them with one snippet

    Our snippet library has a ready-made version that uses WordPress’s wp_headers filter. It adds the four headers below to every page WordPress builds, and removes the X-Powered-By header that tells the world which PHP version you run.

    functions.phpPHP
    add_filter( 'wp_headers', function( $headers ) {
        // Prevent MIME-type sniffing
        $headers['X-Content-Type-Options'] = 'nosniff';
    
        // Block your site from being embedded in iframes on other domains
        $headers['X-Frame-Options'] = 'SAMEORIGIN';
    
        // Control how much referrer info is sent with outbound links
        $headers['Referrer-Policy'] = 'strict-origin-when-cross-origin';
    
        // Restrict access to browser features
        $headers['Permissions-Policy'] = 'camera=(), microphone=(), geolocation=()';
    
        // Remove the X-Powered-By header if PHP sets it
        header_remove( 'X-Powered-By' );
    
        return $headers;
    } );
    Tested on WordPress 7.1 · PHP 8.3Checked November 13, 2026
  2. Know what each one does

    • X-Content-Type-Options: nosniff stops browsers guessing a file’s type, so a file uploaded as an image can’t be run as a script.
    • X-Frame-Options: SAMEORIGIN stops other sites loading yours inside a hidden frame and tricking visitors into clicking (clickjacking).
    • Referrer-Policy: strict-origin-when-cross-origin sends only your domain, not the full page address with search terms or IDs, when a visitor follows a link to another site.
    • Permissions-Policy switches 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: SAMEORIGIN blocks 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 Policy frame-ancestors rule listing the allowed sites instead.

  3. Check the result

    Reload the page with the Network tab open. The four headers should now appear under Response Headers, and X-Powered-By should be gone.

You should now see

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_headers only 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.

functions.phpPHP
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;
} );
Tested on WordPress 7.1 · PHP 8.3Add to: your child theme's functions.phpChecked November 13, 2026
Read this before raising max-age

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.

Snippet

Add a Content Security Policy in Report-Only Mode

Send a starter Content Security Policy that reports what it would block without breaking anything, so you can tighten it safely.

PHPAdvanced
View

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

1Why start HSTS with a max-age of one day?
2What does a Report-Only Content Security Policy do?

Was this helpful?

Written by Nahnu Plugins

We build and maintain WordPress plugins. Every guide is tested on a fresh WordPress site before it goes out, and checked again when WordPress updates.

More guides

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only