WordPress GuideSecurity → Prevent Content Sniffing

How to prevent content sniffing in WordPress

Content sniffing might sound technical (or just awkward), but ignoring it leaves your WordPress site exposed to avoidable threats. By taking one small step—adding a simple HTTP header—you can block this browser behavior and tighten your security instantly.

Let’s get into it.

What is content sniffing and why it’s a problem

Content sniffing is a browser behavior where the browser tries to guess a file’s content type based on its contents rather than trusting the server’s declared MIME type.

Most modern browsers try to “sniff” the content type of a file based on its content rather than trusting the server’s declared Content-Type. This behavior is meant to help when websites send incorrect MIME types, but it can also be exploited.

Here’s the risk: An attacker might upload a seemingly harmless file (like a .jpg) that actually contains JavaScript. If the browser sniffs it as a script instead of an image, it could execute malicious code in the user’s browser—triggering cross-site scripting (XSS) attacks or drive-by downloads.

To stop this, HTTP headers can tell the browser: “Don’t try to guess—just trust what I told you.” That’s where the X-Content-Type-Options: nosniff header comes in. It enforces strict MIME type handling, making it a lightweight but powerful security hardening tactic for any WordPress site.

1. Use a plugin to add the nosniff header

If you’re not comfortable editing server files or code, using a plugin is the safest and fastest method.

That’s it. The plugin adds the nosniff header for you on every page load. To double-check, open your browser’s DevTools, go to the Network tab, click your homepage, and look under Response Headers.

This method is ideal for beginners or site owners who don’t want to touch configuration files. It’s also a quick win for WordPress multisite networks, where you might want a consistent policy applied across many subsites.

2. Add the nosniff header via .htaccess (for Apache servers)

If your WordPress site runs on an Apache server, you can add the header manually through the .htaccess file. This gives you more control and doesn’t rely on a plugin.

Make sure your server has the mod_headers module enabled. Most hosting environments do, but if you get a server error after saving the file, remove the code and contact your host for help.

You can now test your site using browser tools or SecurityHeaders.com to confirm the nosniff header is being sent.

3. Add the nosniff header via Nginx

If your server runs Nginx instead of Apache, you’ll need to configure the header directly in your Nginx server block.

curl -I https://yourdomain.com
X-Content-Type-Options: nosniff

This method gives you server-wide control over headers, making it a great fit for developers and anyone managing multiple WordPress installs on the same server.

Use a PHP function (advanced method)

If you want to inject the header directly from within WordPress, you can use the send_headers action. This is not recommended as your primary solution because some caching layers and CDNs might strip or override it, but it’s still worth knowing.

This will add the header on every page load, as long as nothing else removes or overrides it. Test using browser tools or curl to confirm it’s active.

Note: If you’re using aggressive caching, object cache, or a CDN, this method may not consistently apply the header. Use server-level or plugin-level options if you need reliability.

How to verify the header is working

Whether you used a plugin, .htaccess, Nginx, or PHP, you should always confirm that the header is being sent correctly.

Here’s how:

Alternatively, use:

curl -I https://yourdomain.com

Or visit SecurityHeaders.com, enter your URL, and look for a green checkmark next to the nosniff header.

Combine with other secure headers for better protection

X-Content-Type-Options is just one of many HTTP headers that help secure a WordPress site. For a more robust defense, consider enabling these as well:

Many of these can be enabled through the same Security Headers plugin or by extending your server configuration.

Bonus: Restrict unsafe MIME types in uploads

To take MIME security even further, limit which file types can be uploaded to your site. WordPress uses the upload_mimes filter to control this.

Here’s how to allow only safe types like images and PDFs:

You can also block PHP execution inside /wp-content/uploads/ by adding this to an .htaccess file in that directory:

This prevents attackers from running scripts even if they somehow manage to upload them.

Additional resources

Comprehensive guide to securing WordPress with ModSecurity

This guide provides a comprehensive overview of how to use ModSecurity to enhance the security of your WordPress site.

WordPress vulnerability scanners  →

Learn how a WordPress vulnerability scanner protects your site by detecting and addressing security risks early.

Why security matters for WordPress enterprise hosting

Use the blog as your guide to attacks to watch out for, security best practices, and steps to improve the WordPress protection you already have.