Add Security Headers in WordPress Without a Plugin

Security headers are small instructions your web server sends to every visitor browser along with your pages. They tell the browser how to behave when it loads your site: which content it may run, whether your pages may appear inside a frame on another site, and whether it must always use a secure connection. They cost nothing, they need no plugin, and they close several common attack paths that a login form or a firewall alone cannot cover.
Most WordPress owners never set them, because the settings live on the server rather than in the WordPress dashboard. That is a missed opportunity. Adding the main security headers takes about fifteen minutes, and you can do it with a few lines in your .htaccess file, in your nginx configuration, or with a small PHP snippet. This guide covers all three methods, what each header does, and how to test the result.
What Are Security Headers and Why Do They Matter?
Every time someone visits your site, the server sends HTTP response headers together with the page content. Most of these headers are invisible housekeeping, but security headers carry explicit safety rules for the browser. For example, one header can forbid your pages from loading inside an iframe on a stranger site, which stops clickjacking attacks where a visitor is tricked into clicking something they cannot see.
Headers do not replace updates, strong passwords, or backups. They are one layer in a layered defence. A site with good headers and weak passwords is still an easy target, and a site with strong passwords but no headers still leaves several browser-level attacks open. Think of headers as locking the windows while your password locks the front door. Together with other simple hardening steps, such as our guide on how to limit login attempts in WordPress without a plugin, they make your site a much harder target for automated attacks.
- They protect your visitors, not just your files, by stopping attacks that run inside the browser.
- They cost nothing and add no measurable load to your server.
- They improve trust signals: security scanners, clients, and partners often check them first.
- They work alongside any theme, page builder, or caching plugin you already use.
Which Security Headers Should You Set?
There are dozens of headers, but six cover almost everything a typical WordPress site needs. Start with these and add them one at a time, checking your site after each change, because setting all six at once without testing is how sites break.
- Strict-Transport-Security (HSTS): tells the browser to use HTTPS only for your domain for a set period, which blocks downgrade attacks that try to force a plain HTTP connection.
- X-Frame-Options: controls whether your pages may load inside a frame. SAMEORIGIN allows framing only by your own site, which stops clickjacking.
- X-Content-Type-Options: set to nosniff, it stops the browser from guessing a file type, which blocks scripts disguised as images or stylesheets.
- Referrer-Policy: controls how much of the page address is sent to other sites when a visitor clicks an outgoing link. strict-origin-when-cross-origin is a sensible default.
- Permissions-Policy: tells the browser which device features your site may use, such as camera, microphone, and geolocation. Most blogs need none of them, so you can disable them all.
- Content-Security-Policy (CSP): the most powerful header. It lists exactly which sources may load scripts, styles, images, and fonts. It is also the easiest to get wrong, so start with a report-only version if your site uses many third party scripts.
Do not copy a CSP from another site without checking it against your own plugins and embeds. A policy that blocks your analytics script, your YouTube embeds, or your font files will break visible parts of your site. The Mozilla developer documentation on HTTP headers explains every header in detail if you want to go deeper than this guide.
Before You Start: Check Your Current Headers
Open securityheaders.com, enter your site address, and run the scan. You will get a grade from A+ to F and a list of which headers are present and which are missing. Save a screenshot or note the grade, because it gives you a clear before and after comparison. If your site already scores well, your host may be setting some headers for you, and you should check before adding duplicates.
Also confirm that your site loads fully over HTTPS before you touch HSTS. HSTS tells browsers to refuse plain HTTP for your domain, so if any page, image, or checkout step still needs HTTP, visitors will hit errors. Fix mixed content first, then enable HSTS. Some hosts set headers at server level, so check your control panel first. Our web hosting guide explains what to look for in a host that gives you this control.
Method 1: Add Security Headers With .htaccess on Apache
Most shared hosting runs Apache, which reads a file called .htaccess in your site root folder. Adding headers there is the simplest method, because one file covers your whole site, including images, stylesheets, and other static files. Open File Manager in your hosting panel or connect by FTP, find .htaccess in the public_html folder, and download a backup copy before you edit anything.
Add these lines at the end of the file. The IfModule wrapper makes sure the lines are ignored safely if the headers module is not available on your server, instead of throwing an error.
<IfModule mod_headers.c>
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
Header always set X-Frame-Options "SAMEORIGIN"
Header always set X-Content-Type-Options "nosniff"
Header always set Referrer-Policy "strict-origin-when-cross-origin"
Header always set Permissions-Policy "camera=(), microphone=(), geolocation=()"
</IfModule>
Save the file and reload your site. If the site loads normally, the headers are active. If you see a 500 error, your server may not support one of the directives, so restore your backup copy and add the lines one at a time to find the problem. Leave Content-Security-Policy out of this first pass and add it only after testing it in report-only mode.
Method 2: Add Security Headers in Nginx
If your site runs on nginx, there is no .htaccess file. Headers go in the server block of your site configuration instead, usually in a file under etc/nginx/sites-available. You need server access for this method, so it suits VPS and cloud hosting rather than basic shared hosting. If you are on shared hosting, use Method 1 or Method 3 instead.
server {
listen 443 ssl;
server_name example.com;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;
}
After editing, test the configuration with nginx -t and reload the service. Never reload without testing first, because a syntax error in the server block can take the whole site offline. Once reloaded, visit your site and run the securityheaders.com scan again to confirm the new grade. The always parameter matters: without it, nginx skips the header on error pages and redirects, which leaves gaps in your coverage.
Method 3: Add Security Headers With a PHP Snippet
If you cannot edit server files, you can send headers from WordPress itself using the send_headers hook. Add this snippet to your child theme functions.php file or a code snippets plugin. It runs on every WordPress page load and adds the same five headers as the methods above.
add_action( 'send_headers', function() {
header( 'Strict-Transport-Security: max-age=31536000; includeSubDomains' );
header( 'X-Frame-Options: SAMEORIGIN' );
header( 'X-Content-Type-Options: nosniff' );
header( 'Referrer-Policy: strict-origin-when-cross-origin' );
header( 'Permissions-Policy: camera=(), microphone=(), geolocation=()' );
} );
This method has one important limit: it only covers pages that WordPress generates. Images, stylesheets, JavaScript files, and other static assets are served directly by the web server, so they never run your PHP code and never get these headers. If your host gives you .htaccess access, Method 1 is the better choice for full coverage. Use the PHP method only when server files are out of reach.
One more caution: headers must be sent before any page content. If your snippet runs after output has already started, PHP will show a headers already sent warning. Code in functions.php or a snippets plugin runs early enough to avoid this.
How Do You Test Your Security Headers?
Testing takes two minutes and you should do it after every change, not just at the end. Run the securityheaders.com scan again and compare the grade with your earlier result. A typical WordPress site with the five headers above scores an A.
- Check the grade on securityheaders.com and read the notes for any header it still flags.
- Open your site in a private browser window and click through the homepage, a blog post, and the contact page.
- Test any embedded content: videos, maps, payment forms, and social feeds are the first things a strict header breaks.
- Clear your cache plugin and CDN cache before testing, or you will be checking the old version of your pages.
- Ask a friend to open the site on a different device to catch problems your own browser cache hides.
What Mistakes Should You Avoid?
- Setting headers twice: if your host, your CDN, and your .htaccess file all set the same header, browsers may see conflicting values. Pick one place and keep it there.
- Enabling HSTS before HTTPS works everywhere: HSTS locks browsers into HTTPS for months. Fix every mixed content warning first, or visitors will be locked out of pages that still need HTTP.
- Adding a strict CSP on the first day: a Content-Security-Policy that blocks your own scripts will break your site visibly. Start with Content-Security-Policy-Report-Only, read the violation reports, and only then enforce it.
- Using DENY for X-Frame-Options when you need embeds: DENY blocks all framing, including your own. Use SAMEORIGIN if your site frames its own pages anywhere.
- Forgetting to test after each change: Add one, test, then add the next.
- Skipping backups: always keep a copy of .htaccess or functions.php before editing, so a bad edit costs you seconds instead of hours.
Headers are one layer of a wider security routine. They sit beside the other basics: keeping WordPress and plugins updated, using strong passwords, limiting login attempts, and removing features you do not use. Our guide on how to disable XML-RPC in WordPress covers another quiet attack surface that most site owners never check, and it pairs well with the headers in this guide.
Frequently Asked Questions
Do security headers slow down my WordPress site?
No. Headers add a few bytes to each server response and the browser reads them in microseconds. They have no measurable effect on page load time, and HSTS can even speed up repeat visits slightly because the browser skips the HTTP to HTTPS redirect. If your site is slow, the cause is elsewhere, such as unoptimised images or a slow host.
Can I add security headers without a plugin?
Yes. On Apache hosting you add them to .htaccess, on nginx you add them to the server block, and on any host you can send them with a small PHP snippet in your child theme. All three methods are covered in this guide, and none of them needs a plugin. A plugin only adds convenience, not capability.
What is a good score on securityheaders.com?
An A is a good target for most WordPress sites, and the five headers in this guide usually get you there. A+ needs a Content-Security-Policy as well, which is worth adding only after you have tested it in report-only mode. Do not chase A+ at the cost of breaking embeds, videos, or checkout pages on your site.
Will HSTS lock me out of my own site?
HSTS can lock browsers into HTTPS, so if your HTTPS setup is broken, visitors will see errors instead of your site until the max-age period expires or you fix the certificate. That is why you should only enable HSTS after every page, image, and form loads correctly over HTTPS. Start with a short max-age such as 300 seconds while testing, then raise it to 31536000 once you are sure everything works.
Last updated: November 2026.



