For businesses

How to add security headers in WordPress

To add security headers in WordPress, paste a short block of rules into your site's .htaccess file (Apache or LiteSpeed hosting, which includes most cPanel hosts), your Nginx config, or a Cloudflare Transform Rule. Start with five safe headers: Strict-Transport-Security, X-Content-Type-Options, X-Frame-Options, Referrer-Policy and Permissions-Policy. Then test the site with a free security headers checker.

By the Secuvexa team, I.T. Experts, Gujarat · Updated

Security headers are the cheapest security win a WordPress site can get. They take about 10 minutes to add, they cost nothing, and they stop attacks such as clickjacking, protocol downgrades and some cross-site scripting before they start. Many small-business WordPress sites send none of them.

First, see where you stand: run your site through the free Secuvexa security headers checker. It lists which headers are missing and gives a grade. Then follow the steps for your hosting below and check again.

The headers to add (and what each one does)

HeaderRecommended valueWhat it protects against
Strict-Transport-Securitymax-age=31536000; includeSubDomainsVisitors being moved from HTTPS to plain HTTP on public Wi-Fi.
X-Content-Type-OptionsnosniffBrowsers treating an uploaded file as a script.
X-Frame-OptionsSAMEORIGINClickjacking: your login or checkout page hidden inside another site.
Referrer-Policystrict-origin-when-cross-originFull page addresses (which can include order or reset tokens) leaking to other sites.
Permissions-Policycamera=(), microphone=(), geolocation=()Injected scripts quietly asking for the camera, microphone or location.
Content-Security-PolicyStart with Report-Only (see below)Many cross-site scripting and malicious script injections.

Only add Strict-Transport-Security once HTTPS works on your whole site, including every subdomain if you use includeSubDomains. Browsers remember it for the max-age period, so a broken HTTPS setup becomes a broken site.

Option 1: .htaccess (cPanel, Apache and LiteSpeed hosting)

Most shared hosting in India, the UAE, the UK and the US runs Apache or LiteSpeed, which read the .htaccess file in your WordPress folder.

  1. Take a backup of the current .htaccess (in cPanel File Manager, enable "Show hidden files", right-click, Download).
  2. Open .htaccess in the folder where WordPress is installed (usually public_html).
  3. Paste this block above the # BEGIN WordPress line, so WordPress does not overwrite it.
  4. Save, then reload your site in a private window to make sure it still opens.
<IfModule mod_headers.c>
  Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
  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=()"
</IfModule>

If the site shows a "500 Internal Server Error" after saving, put the old file back. It usually means a typo, or that your host does not allow these directives; their support team can enable mod_headers.

Option 2: Nginx (VPS or managed hosting)

Add these lines inside the server { } block for your HTTPS site, then test and reload with nginx -t && systemctl reload nginx. The word always makes Nginx send the headers on error pages too.

add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
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;

If a location block has its own add_header lines, Nginx ignores the server-level ones for that location. Repeat them there or use an include file.

Option 3: Cloudflare (any hosting)

If your domain uses Cloudflare, you can add headers without touching the server:

  1. In the Cloudflare dashboard open your domain, then Rules → Transform Rules → Modify Response Header.
  2. Create a rule that applies to all incoming requests.
  3. Choose Set static and add each header name and value from the table above.
  4. For HSTS you can instead use SSL/TLS → Edge Certificates → HTTP Strict Transport Security.

Option 4: a code snippet (when you cannot edit the server)

If your host does not let you edit .htaccess or server settings, WordPress can send the headers itself. Add this with a code-snippets plugin or in a child theme's functions.php:

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=()' );
} );

This only covers pages generated by WordPress. Images, CSS and pages served from a full-page cache will not get the headers, which is why the server options above are better.

Content-Security-Policy: add it carefully

CSP is the most powerful header and the one most likely to break a WordPress site, because themes, page builders, analytics, chat widgets and payment buttons load scripts from many places. Do it in two stages:

  1. Report only. Add Content-Security-Policy-Report-Only with a starting policy such as default-src 'self' https:; script-src 'self' 'unsafe-inline' https:; style-src 'self' 'unsafe-inline' https:; img-src 'self' data: https:; frame-ancestors 'self'. Nothing is blocked; the browser console shows what would have been.
  2. Enforce. After a week of normal use with no unexpected warnings, rename it to Content-Security-Policy. Tighten it over time by replacing https: with the specific domains you use.

Even a loose policy with frame-ancestors 'self' and object-src 'none' is a real improvement over none.

Test that it worked

  1. Clear your caching plugin and any CDN cache.
  2. Run the security headers checker again. The missing headers should now show as present.
  3. Click through the pages that matter: login, contact form, checkout, any embedded maps or videos.
  4. Run the full Secuvexa website scan. It also checks SSL, cookies, exposed files, outdated WordPress plugins and email spoofing protection, which headers do not cover.

Headers are one layer, not the whole job

Security headers protect your visitors' browsers. They do not fix an outdated plugin, a weak admin password or a hacked site. Pair them with the WordPress security checklist, and if your site already shows signs of trouble, read is my website hacked?

Frequently asked questions

What are security headers?

Security headers are short instructions your web server sends with every page. They tell the visitor's browser to always use HTTPS, not to load your site inside other sites, not to guess file types and to limit which browser features a page can use. They block whole classes of attacks such as clickjacking at almost no cost.

Do I need a plugin to add security headers in WordPress?

No. The most reliable way is your server configuration: .htaccess on Apache or LiteSpeed hosting, the server block on Nginx, or a Cloudflare rule. Plugins work too, but headers set by PHP are not sent for images, CSS or cached pages, and a plugin is one more thing to keep updated.

Can security headers break my WordPress site?

The five headers in this guide are safe for almost every site. Content-Security-Policy is the one that can break things such as page builders, analytics and payment widgets, so start it in Report-Only mode and tighten it gradually.

How do I check if my security headers are working?

Open your site in a private window and run the Secuvexa security headers checker, or open your browser developer tools, go to Network, click the first request and look at the response headers. Check a normal page, not just the home page, because caching plugins can serve pages differently.

Should I use X-Frame-Options or frame-ancestors?

Use both for now. Content-Security-Policy frame-ancestors is the modern way to control who can put your site in a frame, and X-Frame-Options SAMEORIGIN covers older browsers. Set them to the same rule so they do not conflict.

This guide is general information, not legal advice.