<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>content security policy Archives - Developry Plugins</title>
	<atom:link href="https://developryplugins.com/tag/content-security-policy/feed/" rel="self" type="application/rss+xml" />
	<link>https://developryplugins.com/tag/content-security-policy/</link>
	<description></description>
	<lastBuildDate>Mon, 24 Nov 2025 11:18:04 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://developryplugins.com/wp-content/uploads/2025/11/cropped-favicon-32x32.png</url>
	<title>content security policy Archives - Developry Plugins</title>
	<link>https://developryplugins.com/tag/content-security-policy/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Implementing Content Security Policy (CSP) in WordPress</title>
		<link>https://developryplugins.com/implementing-content-security-policy-csp-in-wordpress/</link>
		
		<dc:creator><![CDATA[Krasen Slavov]]></dc:creator>
		<pubDate>Fri, 05 Jun 2026 09:00:00 +0000</pubDate>
				<category><![CDATA[WordPress Security & Protection]]></category>
		<category><![CDATA[content security policy]]></category>
		<category><![CDATA[csp]]></category>
		<category><![CDATA[http headers]]></category>
		<category><![CDATA[security headers]]></category>
		<category><![CDATA[xss prevention]]></category>
		<guid isPermaLink="false">https://developryplugins.com/?p=144</guid>

					<description><![CDATA[<p>Content Security Policy (CSP) is one of the most powerful security mechanisms available to protect WordPress sites from cross-site scripting (XSS) attacks, code injection, and other malicious activities. This advanced...</p>
<p>The post <a href="https://developryplugins.com/implementing-content-security-policy-csp-in-wordpress/">Implementing Content Security Policy (CSP) in WordPress</a> appeared first on <a href="https://developryplugins.com">Developry Plugins</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><!-- @format --></p>
<p>Content Security Policy (CSP) is one of the most powerful security mechanisms available to protect WordPress sites from cross-site scripting (XSS) attacks, code injection, and other malicious activities. This advanced security feature allows you to control which resources can be loaded and executed on your website, creating a robust defense layer that complements traditional security measures.</p>
<h2 id="understanding-content-security-policy">Understanding Content Security Policy</h2>
<p>Content Security Policy is an HTTP response header that instructs browsers which sources of content are trustworthy for your website. When properly configured, CSP prevents browsers from executing malicious scripts injected through vulnerabilities, even if attackers manage to insert code into your database or files.</p>
<p>The CSP header works by defining a whitelist of approved sources for various types of content including scripts, stylesheets, images, fonts, and other resources. When a browser encounters content from an unapproved source, it blocks the resource and optionally reports the violation.</p>
<p>Modern browsers support CSP Level 2 and Level 3 specifications, providing extensive protection capabilities. According to Mozilla’s compatibility data, over 95% of users have browsers that support core CSP features, making it a practical security enhancement for most WordPress sites.</p>
<h2 id="core-csp-directives-explained">Core CSP Directives Explained</h2>
<p>Understanding CSP directives is crucial for implementing effective policies. The <code>default-src</code> directive serves as a fallback for other directives, establishing the baseline policy. For example, <code>default-src 'self'</code> restricts all content to your own domain unless overridden by specific directives.</p>
<p>The <code>script-src</code> directive controls JavaScript execution and is typically the most critical for preventing XSS attacks. A policy like <code>script-src 'self' https://cdn.example.com</code> allows scripts only from your domain and the specified CDN.</p>
<p>The <code>style-src</code> directive manages CSS sources, while <code>img-src</code> controls image loading. The <code>font-src</code> directive governs font file sources, essential for custom typography. The <code>connect-src</code> directive restricts URLs for AJAX requests, WebSocket connections, and EventSource connections.</p>
<p>For embedded content, <code>frame-src</code> controls iframe sources, crucial for preventing clickjacking and unauthorized embeds. The <code>media-src</code> directive manages audio and video sources, while <code>object-src</code> controls plugins like Flash (typically set to <code>'none'</code> in modern sites).</p>
<h2 id="implementing-csp-in-wordpress-via-.htaccess">Implementing CSP in WordPress via .htaccess</h2>
<p>For Apache-based WordPress installations, adding CSP headers through .htaccess provides server-level implementation. Add this configuration to your root .htaccess file:</p>
<div class="sourceCode" id="cb1">
<pre class="sourceCode apache"><code class="sourceCode apache"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true"></a><span class="fu">&lt;IfModule</span><span class="at"> mod_headers.c</span><span class="fu">&gt;</span></span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true"></a>    Header<span class="st"> set Content-Security-Policy &quot;default-src &#39;self&#39;; script-src &#39;self&#39; &#39;unsafe-inline&#39; &#39;unsafe-eval&#39;; style-src &#39;self&#39; &#39;unsafe-inline&#39;; img-src &#39;self&#39; data: https:; font-src &#39;self&#39; data:; connect-src &#39;self&#39;; frame-src &#39;self&#39;; media-src &#39;self&#39;; object-src &#39;none&#39;; base-uri &#39;self&#39;; form-action &#39;self&#39;;&quot;</span></span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true"></a><span class="fu">&lt;/IfModule&gt;</span></span></code></pre>
</div>
<p>This basic policy allows same-origin resources and includes <code>'unsafe-inline'</code> for inline scripts and styles, which many WordPress themes require. While not the most secure configuration, it provides a starting point that won’t immediately break most sites.</p>
<p>For production environments, gradually tighten this policy by removing <code>'unsafe-inline'</code> and implementing nonce-based or hash-based approaches for inline content.</p>
<h2 id="nginx-csp-configuration">Nginx CSP Configuration</h2>
<p>For Nginx-powered WordPress sites, add CSP headers in your server configuration file or location block:</p>
<pre class="nginx"><code>add_header Content-Security-Policy &quot;default-src &#39;self&#39;; script-src &#39;self&#39; &#39;unsafe-inline&#39; &#39;unsafe-eval&#39;; style-src &#39;self&#39; &#39;unsafe-inline&#39;; img-src &#39;self&#39; data: https:; font-src &#39;self&#39; data:; connect-src &#39;self&#39;; frame-src &#39;self&#39;; media-src &#39;self&#39;; object-src &#39;none&#39;; base-uri &#39;self&#39;; form-action &#39;self&#39;;&quot; always;</code></pre>
<p>The <code>always</code> parameter ensures the header is sent even for error responses, maintaining consistent security coverage across all HTTP response codes.</p>
<h2 id="plugin-based-csp-implementation">Plugin-Based CSP Implementation</h2>
<p>The HTTP Headers plugin provides a user-friendly interface for adding security headers including CSP without editing server configuration files. After installing from the WordPress repository, navigate to Settings &gt; HTTP Headers to configure your policy.</p>
<p>The Really Simple SSL plugin also includes CSP functionality in its premium version, offering pre-configured policies and easy management through the WordPress dashboard.</p>
<p>For programmatic control, implement CSP headers using WordPress hooks:</p>
<div class="sourceCode" id="cb3">
<pre class="sourceCode php"><code class="sourceCode php"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true"></a><span class="kw">function</span> add_csp_header<span class="ot">()</span> {</span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true"></a>    <span class="kw">$csp</span> = <span class="st">&quot;default-src &#39;self&#39;; &quot;</span><span class="ot">;</span></span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true"></a>    <span class="kw">$csp</span> .= <span class="st">&quot;script-src &#39;self&#39; &#39;unsafe-inline&#39; &#39;unsafe-eval&#39; https://www.google-analytics.com; &quot;</span><span class="ot">;</span></span>
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true"></a>    <span class="kw">$csp</span> .= <span class="st">&quot;style-src &#39;self&#39; &#39;unsafe-inline&#39; https://fonts.googleapis.com; &quot;</span><span class="ot">;</span></span>
<span id="cb3-5"><a href="#cb3-5" aria-hidden="true"></a>    <span class="kw">$csp</span> .= <span class="st">&quot;font-src &#39;self&#39; https://fonts.gstatic.com; &quot;</span><span class="ot">;</span></span>
<span id="cb3-6"><a href="#cb3-6" aria-hidden="true"></a>    <span class="kw">$csp</span> .= <span class="st">&quot;img-src &#39;self&#39; data: https:; &quot;</span><span class="ot">;</span></span>
<span id="cb3-7"><a href="#cb3-7" aria-hidden="true"></a>    <span class="kw">$csp</span> .= <span class="st">&quot;connect-src &#39;self&#39;; &quot;</span><span class="ot">;</span></span>
<span id="cb3-8"><a href="#cb3-8" aria-hidden="true"></a>    <span class="kw">$csp</span> .= <span class="st">&quot;frame-src &#39;self&#39; https://www.youtube.com; &quot;</span><span class="ot">;</span></span>
<span id="cb3-9"><a href="#cb3-9" aria-hidden="true"></a>    <span class="kw">$csp</span> .= <span class="st">&quot;object-src &#39;none&#39;; &quot;</span><span class="ot">;</span></span>
<span id="cb3-10"><a href="#cb3-10" aria-hidden="true"></a>    <span class="kw">$csp</span> .= <span class="st">&quot;base-uri &#39;self&#39;;&quot;</span><span class="ot">;</span></span>
<span id="cb3-11"><a href="#cb3-11" aria-hidden="true"></a></span>
<span id="cb3-12"><a href="#cb3-12" aria-hidden="true"></a>    <span class="fu">header</span><span class="ot">(</span><span class="st">&quot;Content-Security-Policy: &quot;</span> . <span class="kw">$csp</span><span class="ot">);</span></span>
<span id="cb3-13"><a href="#cb3-13" aria-hidden="true"></a>}</span>
<span id="cb3-14"><a href="#cb3-14" aria-hidden="true"></a>add_action<span class="ot">(</span><span class="st">&#39;send_headers&#39;</span><span class="ot">,</span> <span class="st">&#39;add_csp_header&#39;</span><span class="ot">);</span></span></code></pre>
</div>
<p>This approach allows dynamic CSP generation based on conditions, user roles, or page types.</p>
<h2 id="implementing-nonce-based-csp">Implementing Nonce-Based CSP</h2>
<p>Nonces (numbers used once) provide a secure alternative to <code>'unsafe-inline'</code> by allowing specific inline scripts while blocking others. WordPress 5.7+ includes built-in support for script and style nonces.</p>
<p>Generate and implement nonces in your theme’s functions.php:</p>
<div class="sourceCode" id="cb4">
<pre class="sourceCode php"><code class="sourceCode php"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true"></a><span class="kw">function</span> add_csp_nonce<span class="ot">()</span> {</span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true"></a>    <span class="kw">$nonce</span> = <span class="fu">base64_encode</span><span class="ot">(</span><span class="fu">random_bytes</span><span class="ot">(</span><span class="dv">16</span><span class="ot">));</span></span>
<span id="cb4-3"><a href="#cb4-3" aria-hidden="true"></a></span>
<span id="cb4-4"><a href="#cb4-4" aria-hidden="true"></a>    <span class="co">// Store nonce for use in CSP header</span></span>
<span id="cb4-5"><a href="#cb4-5" aria-hidden="true"></a>    <span class="kw">global</span> <span class="kw">$csp_nonce</span><span class="ot">;</span></span>
<span id="cb4-6"><a href="#cb4-6" aria-hidden="true"></a>    <span class="kw">$csp_nonce</span> = <span class="kw">$nonce</span><span class="ot">;</span></span>
<span id="cb4-7"><a href="#cb4-7" aria-hidden="true"></a></span>
<span id="cb4-8"><a href="#cb4-8" aria-hidden="true"></a>    <span class="co">// Add nonce to inline scripts</span></span>
<span id="cb4-9"><a href="#cb4-9" aria-hidden="true"></a>    add_filter<span class="ot">(</span><span class="st">&#39;script_loader_tag&#39;</span><span class="ot">,</span> <span class="kw">function</span><span class="ot">(</span><span class="kw">$tag</span><span class="ot">,</span> <span class="kw">$handle</span><span class="ot">)</span> <span class="kw">use</span> <span class="ot">(</span><span class="kw">$nonce</span><span class="ot">)</span> {</span>
<span id="cb4-10"><a href="#cb4-10" aria-hidden="true"></a>        <span class="kw">if</span> <span class="ot">(</span><span class="fu">strpos</span><span class="ot">(</span><span class="kw">$tag</span><span class="ot">,</span> <span class="st">&#39;&lt;script&#39;</span><span class="ot">)</span> !== <span class="kw">false</span><span class="ot">)</span> {</span>
<span id="cb4-11"><a href="#cb4-11" aria-hidden="true"></a>            <span class="kw">$tag</span> = <span class="fu">str_replace</span><span class="ot">(</span><span class="st">&#39;&lt;script&#39;</span><span class="ot">,</span> <span class="st">&quot;&lt;script nonce=&#39;</span><span class="kw">{$nonce}</span><span class="st">&#39;&quot;</span><span class="ot">,</span> <span class="kw">$tag</span><span class="ot">);</span></span>
<span id="cb4-12"><a href="#cb4-12" aria-hidden="true"></a>        }</span>
<span id="cb4-13"><a href="#cb4-13" aria-hidden="true"></a>        <span class="kw">return</span> <span class="kw">$tag</span><span class="ot">;</span></span>
<span id="cb4-14"><a href="#cb4-14" aria-hidden="true"></a>    }<span class="ot">,</span> <span class="dv">10</span><span class="ot">,</span> <span class="dv">2</span><span class="ot">);</span></span>
<span id="cb4-15"><a href="#cb4-15" aria-hidden="true"></a></span>
<span id="cb4-16"><a href="#cb4-16" aria-hidden="true"></a>    <span class="co">// Add nonce to inline styles</span></span>
<span id="cb4-17"><a href="#cb4-17" aria-hidden="true"></a>    add_filter<span class="ot">(</span><span class="st">&#39;style_loader_tag&#39;</span><span class="ot">,</span> <span class="kw">function</span><span class="ot">(</span><span class="kw">$tag</span><span class="ot">,</span> <span class="kw">$handle</span><span class="ot">)</span> <span class="kw">use</span> <span class="ot">(</span><span class="kw">$nonce</span><span class="ot">)</span> {</span>
<span id="cb4-18"><a href="#cb4-18" aria-hidden="true"></a>        <span class="kw">if</span> <span class="ot">(</span><span class="fu">strpos</span><span class="ot">(</span><span class="kw">$tag</span><span class="ot">,</span> <span class="st">&#39;&lt;style&#39;</span><span class="ot">)</span> !== <span class="kw">false</span><span class="ot">)</span> {</span>
<span id="cb4-19"><a href="#cb4-19" aria-hidden="true"></a>            <span class="kw">$tag</span> = <span class="fu">str_replace</span><span class="ot">(</span><span class="st">&#39;&lt;style&#39;</span><span class="ot">,</span> <span class="st">&quot;&lt;style nonce=&#39;</span><span class="kw">{$nonce}</span><span class="st">&#39;&quot;</span><span class="ot">,</span> <span class="kw">$tag</span><span class="ot">);</span></span>
<span id="cb4-20"><a href="#cb4-20" aria-hidden="true"></a>        }</span>
<span id="cb4-21"><a href="#cb4-21" aria-hidden="true"></a>        <span class="kw">return</span> <span class="kw">$tag</span><span class="ot">;</span></span>
<span id="cb4-22"><a href="#cb4-22" aria-hidden="true"></a>    }<span class="ot">,</span> <span class="dv">10</span><span class="ot">,</span> <span class="dv">2</span><span class="ot">);</span></span>
<span id="cb4-23"><a href="#cb4-23" aria-hidden="true"></a>}</span>
<span id="cb4-24"><a href="#cb4-24" aria-hidden="true"></a>add_action<span class="ot">(</span><span class="st">&#39;init&#39;</span><span class="ot">,</span> <span class="st">&#39;add_csp_nonce&#39;</span><span class="ot">);</span></span>
<span id="cb4-25"><a href="#cb4-25" aria-hidden="true"></a></span>
<span id="cb4-26"><a href="#cb4-26" aria-hidden="true"></a><span class="kw">function</span> send_csp_with_nonce<span class="ot">()</span> {</span>
<span id="cb4-27"><a href="#cb4-27" aria-hidden="true"></a>    <span class="kw">global</span> <span class="kw">$csp_nonce</span><span class="ot">;</span></span>
<span id="cb4-28"><a href="#cb4-28" aria-hidden="true"></a>    <span class="kw">if</span> <span class="ot">(</span>!<span class="kw">isset</span><span class="ot">(</span><span class="kw">$csp_nonce</span><span class="ot">))</span> <span class="kw">return</span><span class="ot">;</span></span>
<span id="cb4-29"><a href="#cb4-29" aria-hidden="true"></a></span>
<span id="cb4-30"><a href="#cb4-30" aria-hidden="true"></a>    <span class="kw">$csp</span> = <span class="st">&quot;default-src &#39;self&#39;; &quot;</span><span class="ot">;</span></span>
<span id="cb4-31"><a href="#cb4-31" aria-hidden="true"></a>    <span class="kw">$csp</span> .= <span class="st">&quot;script-src &#39;self&#39; &#39;nonce-</span><span class="kw">{$csp_nonce}</span><span class="st">&#39;; &quot;</span><span class="ot">;</span></span>
<span id="cb4-32"><a href="#cb4-32" aria-hidden="true"></a>    <span class="kw">$csp</span> .= <span class="st">&quot;style-src &#39;self&#39; &#39;nonce-</span><span class="kw">{$csp_nonce}</span><span class="st">&#39;;&quot;</span><span class="ot">;</span></span>
<span id="cb4-33"><a href="#cb4-33" aria-hidden="true"></a></span>
<span id="cb4-34"><a href="#cb4-34" aria-hidden="true"></a>    <span class="fu">header</span><span class="ot">(</span><span class="st">&quot;Content-Security-Policy: &quot;</span> . <span class="kw">$csp</span><span class="ot">);</span></span>
<span id="cb4-35"><a href="#cb4-35" aria-hidden="true"></a>}</span>
<span id="cb4-36"><a href="#cb4-36" aria-hidden="true"></a>add_action<span class="ot">(</span><span class="st">&#39;send_headers&#39;</span><span class="ot">,</span> <span class="st">&#39;send_csp_with_nonce&#39;</span><span class="ot">);</span></span></code></pre>
</div>
<p>This implementation generates a cryptographically secure random nonce for each page load and applies it to inline scripts and styles.</p>
<h2 id="csp-reporting-and-monitoring">CSP Reporting and Monitoring</h2>
<p>CSP’s reporting capabilities help identify violations and refine policies. Implement reporting using the <code>report-uri</code> directive:</p>
<div class="sourceCode" id="cb5">
<pre class="sourceCode php"><code class="sourceCode php"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true"></a><span class="kw">function</span> add_csp_with_reporting<span class="ot">()</span> {</span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true"></a>    <span class="kw">$csp</span> = <span class="st">&quot;default-src &#39;self&#39;; &quot;</span><span class="ot">;</span></span>
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true"></a>    <span class="kw">$csp</span> .= <span class="st">&quot;script-src &#39;self&#39; &#39;unsafe-inline&#39;; &quot;</span><span class="ot">;</span></span>
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true"></a>    <span class="kw">$csp</span> .= <span class="st">&quot;report-uri /csp-violation-report-endpoint/;&quot;</span><span class="ot">;</span></span>
<span id="cb5-5"><a href="#cb5-5" aria-hidden="true"></a></span>
<span id="cb5-6"><a href="#cb5-6" aria-hidden="true"></a>    <span class="fu">header</span><span class="ot">(</span><span class="st">&quot;Content-Security-Policy: &quot;</span> . <span class="kw">$csp</span><span class="ot">);</span></span>
<span id="cb5-7"><a href="#cb5-7" aria-hidden="true"></a>}</span>
<span id="cb5-8"><a href="#cb5-8" aria-hidden="true"></a>add_action<span class="ot">(</span><span class="st">&#39;send_headers&#39;</span><span class="ot">,</span> <span class="st">&#39;add_csp_with_reporting&#39;</span><span class="ot">);</span></span></code></pre>
</div>
<p>Create an endpoint to receive violation reports:</p>
<div class="sourceCode" id="cb6">
<pre class="sourceCode php"><code class="sourceCode php"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true"></a><span class="kw">function</span> csp_violation_report_endpoint<span class="ot">()</span> {</span>
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true"></a>    <span class="kw">if</span> <span class="ot">(</span><span class="kw">$_SERVER</span><span class="ot">[</span><span class="st">&#39;REQUEST_URI&#39;</span><span class="ot">]</span> === <span class="st">&#39;/csp-violation-report-endpoint/&#39;</span><span class="ot">)</span> {</span>
<span id="cb6-3"><a href="#cb6-3" aria-hidden="true"></a>        <span class="kw">$report</span> = <span class="fu">file_get_contents</span><span class="ot">(</span><span class="st">&#39;php://input&#39;</span><span class="ot">);</span></span>
<span id="cb6-4"><a href="#cb6-4" aria-hidden="true"></a>        <span class="fu">error_log</span><span class="ot">(</span><span class="st">&#39;CSP Violation: &#39;</span> . <span class="kw">$report</span><span class="ot">);</span></span>
<span id="cb6-5"><a href="#cb6-5" aria-hidden="true"></a>        <span class="fu">http_response_code</span><span class="ot">(</span><span class="dv">204</span><span class="ot">);</span></span>
<span id="cb6-6"><a href="#cb6-6" aria-hidden="true"></a>        <span class="kw">exit</span><span class="ot">;</span></span>
<span id="cb6-7"><a href="#cb6-7" aria-hidden="true"></a>    }</span>
<span id="cb6-8"><a href="#cb6-8" aria-hidden="true"></a>}</span>
<span id="cb6-9"><a href="#cb6-9" aria-hidden="true"></a>add_action<span class="ot">(</span><span class="st">&#39;init&#39;</span><span class="ot">,</span> <span class="st">&#39;csp_violation_report_endpoint&#39;</span><span class="ot">);</span></span></code></pre>
</div>
<h2 id="report-only-mode-for-testing">Report-Only Mode for Testing</h2>
<p>Before enforcing CSP, use <code>Content-Security-Policy-Report-Only</code> to test without breaking functionality:</p>
<div class="sourceCode" id="cb7">
<pre class="sourceCode php"><code class="sourceCode php"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true"></a><span class="kw">function</span> add_csp_report_only<span class="ot">()</span> {</span>
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true"></a>    <span class="kw">$csp</span> = <span class="st">&quot;default-src &#39;self&#39;; script-src &#39;self&#39;; report-uri /csp-violations/&quot;</span><span class="ot">;</span></span>
<span id="cb7-3"><a href="#cb7-3" aria-hidden="true"></a>    <span class="fu">header</span><span class="ot">(</span><span class="st">&quot;Content-Security-Policy-Report-Only: &quot;</span> . <span class="kw">$csp</span><span class="ot">);</span></span>
<span id="cb7-4"><a href="#cb7-4" aria-hidden="true"></a>}</span>
<span id="cb7-5"><a href="#cb7-5" aria-hidden="true"></a>add_action<span class="ot">(</span><span class="st">&#39;send_headers&#39;</span><span class="ot">,</span> <span class="st">&#39;add_csp_report_only&#39;</span><span class="ot">);</span></span></code></pre>
</div>
<p>This header monitors violations without blocking content, allowing you to identify issues before enforcement.</p>
<h2 id="handling-third-party-resources">Handling Third-Party Resources</h2>
<p>WordPress sites frequently use external resources requiring CSP accommodation. For Google Fonts, update your policy:</p>
<pre><code>font-src &#39;self&#39; https://fonts.gstatic.com;
style-src &#39;self&#39; https://fonts.googleapis.com;</code></pre>
<p>For Google Analytics:</p>
<pre><code>script-src &#39;self&#39; https://www.google-analytics.com https://ssl.google-analytics.com;
connect-src &#39;self&#39; https://www.google-analytics.com;</code></pre>
<p>For YouTube embeds:</p>
<pre><code>frame-src &#39;self&#39; https://www.youtube.com https://www.youtube-nocookie.com;</code></pre>
<h2 id="page-builder-compatibility">Page Builder Compatibility</h2>
<p>Popular page builders like Elementor and Divi rely heavily on inline scripts and styles. For compatibility while maintaining security:</p>
<div class="sourceCode" id="cb11">
<pre class="sourceCode php"><code class="sourceCode php"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true"></a><span class="kw">function</span> conditional_csp<span class="ot">()</span> {</span>
<span id="cb11-2"><a href="#cb11-2" aria-hidden="true"></a>    <span class="co">// Relaxed CSP for admin and editors</span></span>
<span id="cb11-3"><a href="#cb11-3" aria-hidden="true"></a>    <span class="kw">if</span> <span class="ot">(</span>current_user_can<span class="ot">(</span><span class="st">&#39;edit_posts&#39;</span><span class="ot">))</span> {</span>
<span id="cb11-4"><a href="#cb11-4" aria-hidden="true"></a>        <span class="kw">$csp</span> = <span class="st">&quot;default-src &#39;self&#39;; script-src &#39;self&#39; &#39;unsafe-inline&#39; &#39;unsafe-eval&#39;; style-src &#39;self&#39; &#39;unsafe-inline&#39;;&quot;</span><span class="ot">;</span></span>
<span id="cb11-5"><a href="#cb11-5" aria-hidden="true"></a>    } <span class="kw">else</span> {</span>
<span id="cb11-6"><a href="#cb11-6" aria-hidden="true"></a>        <span class="co">// Strict CSP for visitors</span></span>
<span id="cb11-7"><a href="#cb11-7" aria-hidden="true"></a>        <span class="kw">$csp</span> = <span class="st">&quot;default-src &#39;self&#39;; script-src &#39;self&#39;; style-src &#39;self&#39;;&quot;</span><span class="ot">;</span></span>
<span id="cb11-8"><a href="#cb11-8" aria-hidden="true"></a>    }</span>
<span id="cb11-9"><a href="#cb11-9" aria-hidden="true"></a></span>
<span id="cb11-10"><a href="#cb11-10" aria-hidden="true"></a>    <span class="fu">header</span><span class="ot">(</span><span class="st">&quot;Content-Security-Policy: &quot;</span> . <span class="kw">$csp</span><span class="ot">);</span></span>
<span id="cb11-11"><a href="#cb11-11" aria-hidden="true"></a>}</span>
<span id="cb11-12"><a href="#cb11-12" aria-hidden="true"></a>add_action<span class="ot">(</span><span class="st">&#39;send_headers&#39;</span><span class="ot">,</span> <span class="st">&#39;conditional_csp&#39;</span><span class="ot">);</span></span></code></pre>
</div>
<p>This approach maintains strict security for visitors while allowing necessary flexibility for content editors.</p>
<h2 id="best-practices-and-recommendations">Best Practices and Recommendations</h2>
<p>Start with a permissive policy using Report-Only mode, monitor violations for at least two weeks across different pages and user scenarios, then gradually tighten restrictions. Avoid <code>'unsafe-inline'</code> and <code>'unsafe-eval'</code> in production policies when possible.</p>
<p>Minimize whitelisted domains to reduce attack surface. Regularly audit your CSP configuration as themes, plugins, and third-party integrations change. Use the Google CSP Evaluator to analyze your policy for common security issues.</p>
<p>Implement different policies for admin areas versus public pages, as admin functionality often requires more permissive policies. Document your CSP configuration and the reasoning behind specific directives for future reference.</p>
<p>Testing tools like Mozilla Observatory and Security Headers provide comprehensive CSP analysis and recommendations. Browser developer consoles display CSP violations in real-time, essential for debugging.</p>
<p>By implementing Content Security Policy thoughtfully and incrementally, WordPress sites gain significant protection against XSS and injection attacks while maintaining compatibility with necessary functionality and third-party integrations.</p>
<h2 id="external-links">External Links</h2>
<ol type="1">
<li><a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP">Content Security Policy (MDN)</a></li>
<li><a href="https://content-security-policy.com/">CSP Quick Reference</a></li>
<li><a href="https://csp-evaluator.withgoogle.com/">Google CSP Evaluator</a></li>
<li><a href="https://cheatsheetseries.owasp.org/cheatsheets/Content_Security_Policy_Cheat_Sheet.html">OWASP CSP Cheat Sheet</a></li>
<li><a href="https://wordpress.org/plugins/http-headers/">Security Headers Plugin</a></li>
</ol>
<h2 id="call-to-action">Call to Action</h2>
<p>Secure your site with bulletproof backups! <a href="https://backupcopilotplugin.com/">Backup Copilot Pro</a> offers automated security audits, malware scanning before backups, and instant recovery—try it free!</p>
<p>The post <a href="https://developryplugins.com/implementing-content-security-policy-csp-in-wordpress/">Implementing Content Security Policy (CSP) in WordPress</a> appeared first on <a href="https://developryplugins.com">Developry Plugins</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
