Understanding HTTP Security Headers for Your WordPress Website

Your WordPress site can be secure in many ways, strong passwords, good hosting, updated plugins. But there’s a layer of protection that often gets overlooked: HTTP security headers.

These are small instructions your server sends to the browser. They tell the browser how to behave when loading your site. Implemented correctly, they can prevent attacks like Cross-Site Scripting (XSS), clickjacking, and information disclosure.

The best part? They’re relatively easy to add to WordPress. This guide covers the most important headers, why they matter, and how to implement them.

Table of Contents

  1. What Are HTTP Security Headers?
  2. The Essential Headers for WordPress
  3. Implementation in WordPress (No Code & Custom)
  4. Testing Your Headers

What Are HTTP Security Headers?

When a browser requests your WordPress site, your server responds with the page content plus a set of HTTP headers. Security headers tell the browser things like:

  • “Don’t let other sites embed my site in an iframe.”
  • “Only load resources from my own domain.”
  • “Never send cookies over insecure connections.”

Without these headers, browsers default to less secure behavior. Adding them costs almost nothing and significantly hardens your site.

The Essential Headers for WordPress

Here are the headers every WordPress site should consider, from most critical to nice-to-have.

1. X-Frame-Options: DENY

Prevents: Clickjacking (hiding malicious links under fake UI elements)

This header stops other sites from embedding your site in an iframe. Unless you explicitly need your site embeddable (e.g., for a widget), lock it down.

Recommended value:

X-Frame-Options: DENY

2. X-Content-Type-Options: nosniff

Prevents: MIME type sniffing (browsers guessing file types, which can turn images into executable code)

This tells the browser: “Don’t guess the file type. Use exactly what I told you.”

Recommended value:

X-Content-Type-Options: nosniff

3. Referrer-Policy: strict-origin-when-cross-origin

Controls: How much referrer data (where the user came from) is sent to other sites

This balances privacy and functionality. Your own site sees full referrers; external sites only see the origin domain.

Recommended value:

Referrer-Policy: strict-origin-when-cross-origin

(Note: this is the default in modern browsers)

4. Strict-Transport-Security (HSTS)

Enforces: HTTPS-only access

Once a user visits your site over HTTPS, HSTS tells the browser to never use HTTP again for that domain, even if the user types http://.

Recommended value:

Strict-Transport-Security: max-age=63072000; includeSubDomains; preload

(2-year expiry, includes subdomains, ready for browser preload lists)

5. Set-Cookie (Secure + HttpOnly + SameSite)

Protects: Session cookies from theft and tampering

These are attributes on the cookie header, not a separate header. They ensure cookies are only sent over HTTPS, can’t be read by JavaScript, and aren’t sent cross-site.

Recommended cookie format:

Set-Cookie: name=value; Secure; HttpOnly; SameSite=Strict

6. Content-Security-Policy (CSP)

Prevents: XSS and data injection attacks, the big one

CSP is powerful but complex. It tells the browser exactly which sources are allowed to load scripts, styles, images, fonts, and more. A well-configured CSP can stop an XSS attack even if an attacker finds a vulnerability.

Basic recommended value (adjust for your site):

Content-Security-Policy: default-src 'self'; script-src 'self' 'unsafe-inline' https:; style-src 'self' 'unsafe-inline' https:; img-src 'self' data: https:; object-src 'none'

Warning: CSP is easy to break. Test thoroughly before deploying to production.

7. Server / X-Powered-By (Remove or Obfuscate)

Prevents: Information disclosure to attackers

These headers announce exactly what server software and version you’re running, useful for attackers scanning for known vulnerabilities. Remove or minimize them.

Recommended: Remove entirely, or set to generic values like Server: webserver

Headers to Disable (Legacy or Problematic)

HeaderAction
X-XSS-ProtectionSet to 0 (modern browsers ignore it; in old ones, it introduces more risks than benefits)
Public-Key-PinsDeprecated. Do not use.

Implementation in WordPress

You have several options, from plugins to manual code.

Option 1: Using a Plugin (Easiest)

Recommended plugins:

  • Security Headers (free, simple interface)
  • Really Simple SSL (also handles HSTS)
  • NinjaFirewall (advanced firewall + headers)

These let you toggle headers on/off without touching code.

Option 2: Via .htaccess (Apache)

Add to your root .htaccess file:

<IfModule mod_headers.c>
    Header set X-Frame-Options "DENY"
    Header set X-Content-Type-Options "nosniff"
    Header set Referrer-Policy "strict-origin-when-cross-origin"
    Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains; preload"
    Header unset X-Powered-By
    Header unset Server
</IfModule>

Option 3: Via Nginx

Add to your server block configuration:

add_header X-Frame-Options "DENY" always;
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=63072000; includeSubDomains; preload" always;
add_header X-Powered-By "" always;

Option 4: Via WordPress functions.php (PHP)

For headers that can’t be set at the server level, or for conditional headers:

add_action('send_headers', 'add_security_headers');
function add_security_headers() {
    header('X-Frame-Options: DENY');
    header('X-Content-Type-Options: nosniff');
    header('Referrer-Policy: strict-origin-when-cross-origin');
    // Do NOT set HSTS here if you have mixed HTTP/HTTPS content
}

Important: HSTS should ideally be set at the server level, not in PHP, to protect all requests including static files.

Testing Your Headers

After implementing, verify they’re working correctly.

Free Online Tools

Browser Dev Tools

  1. Open your site.
  2. Press F12 → Network tab.
  3. Reload the page.
  4. Click the first request (usually the HTML document).
  5. Look for Response Headers.

You should see your added headers listed.

What to Check For

  • All expected headers are present
  • No conflicting headers (e.g., two different CSP policies)
  • Cookies show Secure and HttpOnly flags
  • HSTS is set on HTTPS responses only

Final Thoughts

HTTP security headers are not a replacement for good WordPress hygiene, keeping plugins updated, using strong authentication, and choosing reliable hosting. But they are a powerful, low-effort addition that closes several common attack vectors.

Start small. Implement X-Frame-OptionsX-Content-Type-Options, and Referrer-Policy first. Then add HSTS. Finally, tackle CSP gradually.

Test after every change. And remember: some headers (especially CSP) can break functionality if misconfigured. Always test on a staging site first.

Was this post helpful?
Buy us a coffee!
Tags: