Plugins Plugin hub Backup SEO Caching Security Forms WooCommerce Images Best backup plugins
Hosting Hosting directory Shared Managed Cloud India
Themes & builders Compare Rank Math vs Yoast
Tools All tools Hosting finder Plugin stack builder Cost calculator Salt generator Theme detector Who is hosting this? Speed test Headline analyzer robots.txt generator Redirect generator Uptime calculator Blog name generator
Best lists Best backup plugins Deals
Guides All guides Speed up WordPress Choose hosting Back up WordPress
How we test

Security · Guide

How to Add Security Headers to WordPress

Short answer

To add security headers to WordPress, set them in .htaccess, with a PHP snippet, with a plugin, or in Cloudflare Transform Rules. Start with X-Content-Type-Options: nosniff, X-Frame-Options: SAMEORIGIN, Referrer-Policy: strict-origin-when-cross-origin, Permissions-Policy and a short HSTS value. Then clear the cache and check the live response headers.

Key takeaways

  • X-Content-Type-Options: nosniff tells the browser to trust the declared file type and not guess it.
  • X-Frame-Options: SAMEORIGIN lets only your own site frame its pages, which helps stop clickjacking.
  • Start HSTS at max-age=300, then raise it step by step to 31536000 seconds, which is 1 year.
  • Set each header in only one place, or the browser may receive it twice.
  • WordPress core sends X-Frame-Options: SAMEORIGIN in protected contexts through send_frame_options_header().
  • Referrer-Policy: strict-origin-when-cross-origin sends the full URL for same-origin requests and only the origin for cross-origin requests.

What are security headers in WordPress?

Security headers are short instructions your server sends with each page that tell the browser how to handle that page.

A security header is an HTTP response header. It does not change what visitors see, but it changes what the browser is allowed to do with the page, such as framing it, guessing a file type, or loading it over plain HTTP.

web.dev lists X-Content-Type-Options, X-Frame-Options and HTTP Strict Transport Security (HSTS) among the headers it recommends for all websites, as of November 2026.

Headers add a layer of defence. They do not fix a vulnerable plugin, a weak password, or an out-of-date theme, so keep updates and login protection in place alongside them.

Which security headers should you add first?

Start with five headers: HSTS, X-Content-Type-Options, X-Frame-Options, Referrer-Policy and Permissions-Policy.

HeaderSafe starting valueWhat it does
Strict-Transport-Securitymax-age=300, then raise itTells the browser to use HTTPS for the stated time
X-Content-Type-OptionsnosniffStops the browser from guessing a file type
X-Frame-OptionsSAMEORIGINStops other sites from framing your pages
Referrer-Policystrict-origin-when-cross-originLimits the referrer data sent to other sites
Permissions-Policycamera=(), microphone=(), geolocation=()Blocks browser features you do not use

web.dev says X-Content-Type-Options: nosniff is recommended for all resources served from your server, with the correct Content-Type header, as of November 2026.

MDN says strict-origin-when-cross-origin is the default policy when no policy is set, as of November 2026.

A Content Security Policy is a separate, larger job with its own guide, so this guide does not set a full Content Security Policy here.

How do you add security headers with .htaccess?

Add them above the WordPress block in the .htaccess file in your site root if your host runs Apache or LiteSpeed.

Back up the file first, and keep a copy of the original. If you need a refresher, read this guide to backing up WordPress.

  1. Open your host file manager or FTP client and find .htaccess in the site root.
  2. Download a copy to your computer before you change anything.
  3. Open .htaccess and paste the block below above the line that says # BEGIN WordPress.
  4. Save the file and load your home page to check that the site still works.
  5. Clear your cache plugin and any host cache, then check the headers.
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=300"

Use Header always set, not Header set, so Apache also sends the headers on error pages and redirects. Put the block outside the WordPress markers, because WordPress rewrites the text between those markers.

Allow about 30 minutes for the first set, including the backup, the edit, and the header check. If the site shows a 500 error, restore the copy you saved and check the block for a typo.

How do you add security headers with a PHP snippet?

Use the send_headers hook in a small plugin if you cannot edit server files.

WordPress fires send_headers when it sends headers for a page request. You can add your headers at that point with a short function in a code snippets plugin or a must-use plugin.

add_action( 'send_headers', function () {
    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=()' );
} );

Add HSTS to the same function only after HTTPS works on every page. Start it at max-age=300, as in the .htaccess example above.

This method covers pages that WordPress builds. Images, CSS, and JavaScript files are often served straight by the web server, so they may not get these headers.

Where should you put the PHP code?

Put it in a must-use plugin at wp-content/mu-plugins/security-headers.php, or in a code snippets plugin.

Do not put it in a parent theme file, because a theme update can remove it. A must-use plugin loads on its own and keeps working when you change themes.

How do you add security headers in Cloudflare?

Use Cloudflare Transform Rules for the other headers and the Edge Certificates page for HSTS if Cloudflare sits in front of your site.

Cloudflare says you can change up to 30 HTTP response headers in a single rule, as of November 2026.

  1. In the Cloudflare dashboard, open your domain and go to Rules, then Overview.
  2. Select Create rule, then Response Header Transform Rule.
  3. Give the rule a clear name, such as Add security headers.
  4. Under Modify response header, choose Set static for each header.
  5. Enter the header name and value, then select Set new header for the next one.
  6. Save the rule, purge the Cloudflare cache, and check the live headers.

Choose Set static, not Add static, for these headers. Set static replaces any value your server already sent, while Add static can leave two copies of the same header.

For HSTS, use the dedicated Cloudflare control instead of a Transform Rule. Go to SSL/TLS, then Edge Certificates, then HTTP Strict Transport Security (HSTS), and select Enable HSTS.

Cloudflare says the Max Age Header in the dashboard offers a range from 1 to 12 months, as of November 2026.

If you use Cloudflare for more than headers, read this guide to Cloudflare WAF rules for WordPress.

How do you add security headers with a plugin?

Use one headers plugin if you have no server or Cloudflare access and you want a settings screen instead of code.

  1. Install a dedicated headers plugin from the WordPress plugin directory and activate it.
  2. Open its settings page and find the header fields.
  3. Enter nosniff, SAMEORIGIN, strict-origin-when-cross-origin, and your Permissions-Policy value.
  4. Set HSTS to max-age=300 at first, then raise it only after HTTPS checks pass.
  5. Save the settings, purge every cache, and check the live headers.

A plugin is the easiest route, but it has the same limit as the PHP method. It can only add headers to responses that pass through WordPress.

Use one method per header. If a plugin and your server both send the same header, the browser may see it twice, so turn off the duplicate source.

A headers plugin does not replace a firewall. If you want firewall protection as well, read this guide to setting up the Wordfence firewall.

What HSTS value should you start with?

Start HSTS at max-age=300 for 5 minutes, then raise it to 1 week, 1 month and finally 1 year.

web.dev gives Strict-Transport-Security: max-age=31536000 as its example, and 31536000 seconds is 1 year, as of November 2026.

Cloudflare says you must enable HTTPS before HSTS, so browsers can accept your HSTS settings, as of November 2026.

Use these steps to raise the value safely.

  1. Start at max-age=300 and check that every page loads over HTTPS.
  2. Raise it to 604800, which is 1 week, and check again after a few days.
  3. Raise it to 2592000, which is 1 month, and check subdomains too.
  4. Raise it to 31536000, which is 1 year, only when HTTPS is stable.

Cloudflare says its includeSubDomains option applies the policy to subdomains, and subdomains are inaccessible if they do not support HTTPS, as of November 2026.

Only add includeSubDomains when every subdomain works over HTTPS. Only add preload when you plan to keep HTTPS for the long term.

To turn HSTS off in Cloudflare, set the Max Age Header to 0 (Disable). Cloudflare lists 0 as the disable value in its HSTS docs, as of November 2026.

Before you enable HSTS, make sure your certificate is valid. If you need a free certificate, read this guide to Let's Encrypt vs paid SSL.

How do you check that your security headers work?

Check the live response with curl or your browser developer tools after you clear the cache.

Run this command in a terminal, with your own domain in place of example.com:

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

Look for each header name and value in the output. If a header is missing, check that you saved the change, purged the cache, and set it in only one place.

You can also check in Chrome DevTools. Open the Network tab, reload the page, click the main document request, and read the Response Headers list.

Check more than the home page. Check a post, a page, and the login page, because different routes can send different headers.

WordPress core already protects some routes. WordPress says send_frame_options_header() sends X-Frame-Options: SAMEORIGIN and Content-Security-Policy: frame-ancestors 'self';, and the function was introduced in version 3.1.3.

What mistakes should you avoid?

The most common mistake is setting the same header in two places, which can send it twice.

  • Do not set HSTS before HTTPS works on every page and subdomain you need.
  • Do not use X-Frame-Options: DENY if your Customizer or page builder needs to preview pages in a frame on your own site.
  • Do not paste header code inside the # BEGIN WordPress block in .htaccess.
  • Do not use Referrer-Policy: unsafe-url, because it can leak the full URL to less secure sites.
  • Do not block a browser feature in Permissions-Policy that your site needs, such as fullscreen for video.
  • Do not skip the cache purge, or you may check an old copy and think the change failed.

MalCare says SAMEORIGIN allows framing only when the framing page and every ancestor have the same origin as the response, as of November 2026.

If a change breaks a page, remove the last header you added, purge the cache, and check again. Fix one header at a time so you can see which change caused the problem.

For the HTTPS step that comes before HSTS, read this guide to Cloudflare SSL settings for WordPress.

Frequently asked questions

Do security headers slow down a WordPress site?

No, security headers add only a few short lines to each response, so the effect on load time is tiny. The main risk is a wrong value that blocks a feature, not speed. Add one header at a time, check the site, and remove any value that breaks a page.

Can I add security headers without a plugin?

Yes, you can add them in .htaccess on Apache or LiteSpeed hosting, with a PHP snippet on the send_headers hook, or in Cloudflare Transform Rules. The server or Cloudflare route is best when you can use it, because it can cover static files as well as pages.

Should I use X-Frame-Options: DENY or SAMEORIGIN?

Use SAMEORIGIN for most WordPress sites, because it lets your own site frame its pages while it blocks other sites. Use DENY only when no page on your site ever needs to appear in a frame, including previews in the Customizer or a page builder.

How long does it take to add security headers to WordPress?

Allow about 30 minutes for the first set, including a backup, the edit, and the header check. HSTS takes longer in practice, because you should raise its max-age in steps over days or weeks, not jump straight to 1 year.

Will security headers fix a hacked WordPress site?

No, headers do not clean malware, close a vulnerable plugin, or reset stolen passwords. They only tell the browser how to handle future responses. If a site is hacked, clean it, update everything, change passwords, and then add headers as part of the hardening work.

Do I need a Content Security Policy as well?

A Content Security Policy is useful, but it is a larger job than the five headers in this guide. It can block scripts, styles, and embeds if the rules are wrong. Plan it separately, start in report-only mode if your setup supports it, and fix the reports before you enforce it.

Sources

  1. Security headers quick reference web.dev
  2. HTTP Strict Transport Security (HSTS) Cloudflare
  3. Create a response header transform rule in the dashboard Cloudflare
  4. send_frame_options_header() WordPress Developer Resources
  5. Referrer-Policy header MDN
  6. X-Frame-Options WordPress: Configure and Test It Safely MalCare

Is your product missing, or something out of date?

Tell us what to add or correct. We read every note and reply when we need a source. A message does not buy a ranking.

About the editors

WPLegible editors

Researched from the primary sources listed above and checked against our editorial rules. About the site · How we test