<?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>prepared statements Archives - Developry Plugins</title>
	<atom:link href="https://developryplugins.com/tag/prepared-statements/feed/" rel="self" type="application/rss+xml" />
	<link>https://developryplugins.com/tag/prepared-statements/</link>
	<description></description>
	<lastBuildDate>Mon, 24 Nov 2025 11:18:07 +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>prepared statements Archives - Developry Plugins</title>
	<link>https://developryplugins.com/tag/prepared-statements/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Preventing SQL Injection in WordPress: Security Best Practices</title>
		<link>https://developryplugins.com/preventing-sql-injection-in-wordpress-security-best-practices/</link>
		
		<dc:creator><![CDATA[Krasen Slavov]]></dc:creator>
		<pubDate>Tue, 05 May 2026 09:00:00 +0000</pubDate>
				<category><![CDATA[WordPress Security & Protection]]></category>
		<category><![CDATA[database security]]></category>
		<category><![CDATA[prepared statements]]></category>
		<category><![CDATA[sql injection]]></category>
		<category><![CDATA[wordpress security]]></category>
		<category><![CDATA[wpdb]]></category>
		<guid isPermaLink="false">https://developryplugins.com/?p=151</guid>

					<description><![CDATA[<p>SQL injection remains one of the most dangerous web vulnerabilities, capable of compromising entire databases, stealing user data, and granting attackers administrative access. In WordPress development, improper database queries are...</p>
<p>The post <a href="https://developryplugins.com/preventing-sql-injection-in-wordpress-security-best-practices/">Preventing SQL Injection in WordPress: Security Best Practices</a> appeared first on <a href="https://developryplugins.com">Developry Plugins</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><!-- @format --></p>
<p>SQL injection remains one of the most dangerous web vulnerabilities, capable of compromising entire databases, stealing user data, and granting attackers administrative access. In WordPress development, improper database queries are a leading cause of plugin and theme vulnerabilities.</p>
<p>This comprehensive guide teaches WordPress developers how to write secure database queries using prepared statements, proper escaping, and validation techniques to prevent SQL injection attacks.</p>
<h2 id="understanding-sql-injection">Understanding SQL Injection</h2>
<h3 id="what-is-sql-injection">What Is SQL Injection?</h3>
<p>SQL injection occurs when user input is inserted directly into SQL queries without proper sanitization, allowing attackers to manipulate the query logic.</p>
<p><strong>Vulnerable Code Example:</strong></p>
<div class="sourceCode" id="cb1">
<pre class="sourceCode php"><code class="sourceCode php"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true"></a><span class="co">// DANGEROUS - NEVER DO THIS</span></span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true"></a><span class="kw">$user_id</span> = <span class="kw">$_GET</span><span class="ot">[</span><span class="st">&#39;user_id&#39;</span><span class="ot">];</span></span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true"></a><span class="kw">$results</span> = <span class="kw">$wpdb</span>-&gt;get_results<span class="ot">(</span><span class="st">&quot;SELECT * FROM </span><span class="kw">{$wpdb-&gt;users}</span><span class="st"> WHERE ID = </span><span class="kw">$user_id</span><span class="st">&quot;</span><span class="ot">);</span></span></code></pre>
</div>
<p><strong>Attack:</strong></p>
<pre><code>?user_id=1 OR 1=1</code></pre>
<p>This would return ALL users instead of just one.</p>
<h3 id="real-world-impact">Real-World Impact</h3>
<p><strong>Data Theft:</strong></p>
<ul>
<li>Extract entire user database</li>
<li>Steal password hashes</li>
<li>Access sensitive customer information</li>
</ul>
<p><strong>Privilege Escalation:</strong></p>
<ul>
<li>Modify user roles to admin</li>
<li>Create backdoor admin accounts</li>
</ul>
<p><strong>Site Compromise:</strong></p>
<ul>
<li>Delete database tables</li>
<li>Inject malicious content</li>
<li>Modify wp-options for persistent access</li>
</ul>
<h2 id="the-wordpress-wpdb-class">The WordPress $wpdb Class</h2>
<p>WordPress provides the <code>$wpdb</code> global object for all database interactions. Using it correctly prevents SQL injection.</p>
<h3 id="core-principles">Core Principles</h3>
<p><strong>1. NEVER concatenate user input into queries</strong> <strong>2. ALWAYS use prepared statements</strong> <strong>3. VALIDATE input before database operations</strong> <strong>4. ESCAPE output when displaying data</strong></p>
<h2 id="using-wpdb-prepare-correctly">Using $wpdb-&gt;prepare() Correctly</h2>
<h3 id="prepared-statement-syntax">Prepared Statement Syntax</h3>
<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">global</span> <span class="kw">$wpdb</span><span class="ot">;</span></span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true"></a></span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true"></a><span class="co">// Secure query using prepared statement</span></span>
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true"></a><span class="kw">$user_id</span> = <span class="fu">intval</span><span class="ot">(</span><span class="kw">$_GET</span><span class="ot">[</span><span class="st">&#39;user_id&#39;</span><span class="ot">]);</span> <span class="co">// Always validate first</span></span>
<span id="cb3-5"><a href="#cb3-5" aria-hidden="true"></a><span class="kw">$results</span> = <span class="kw">$wpdb</span>-&gt;get_results<span class="ot">(</span></span>
<span id="cb3-6"><a href="#cb3-6" aria-hidden="true"></a>    <span class="kw">$wpdb</span>-&gt;prepare<span class="ot">(</span></span>
<span id="cb3-7"><a href="#cb3-7" aria-hidden="true"></a>        <span class="st">&quot;SELECT * FROM </span><span class="kw">{$wpdb-&gt;users}</span><span class="st"> WHERE ID = %d&quot;</span><span class="ot">,</span></span>
<span id="cb3-8"><a href="#cb3-8" aria-hidden="true"></a>        <span class="kw">$user_id</span></span>
<span id="cb3-9"><a href="#cb3-9" aria-hidden="true"></a>    <span class="ot">)</span></span>
<span id="cb3-10"><a href="#cb3-10" aria-hidden="true"></a><span class="ot">);</span></span></code></pre>
</div>
<h3 id="placeholders-explained">Placeholders Explained</h3>
<p><strong>%d &#8211; Integer placeholder</strong></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">$post_id</span> = <span class="kw">$_GET</span><span class="ot">[</span><span class="st">&#39;post_id&#39;</span><span class="ot">];</span></span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true"></a><span class="kw">$post</span> = <span class="kw">$wpdb</span>-&gt;get_row<span class="ot">(</span></span>
<span id="cb4-3"><a href="#cb4-3" aria-hidden="true"></a>    <span class="kw">$wpdb</span>-&gt;prepare<span class="ot">(</span></span>
<span id="cb4-4"><a href="#cb4-4" aria-hidden="true"></a>        <span class="st">&quot;SELECT * FROM </span><span class="kw">{$wpdb-&gt;posts}</span><span class="st"> WHERE ID = %d&quot;</span><span class="ot">,</span></span>
<span id="cb4-5"><a href="#cb4-5" aria-hidden="true"></a>        <span class="kw">$post_id</span></span>
<span id="cb4-6"><a href="#cb4-6" aria-hidden="true"></a>    <span class="ot">)</span></span>
<span id="cb4-7"><a href="#cb4-7" aria-hidden="true"></a><span class="ot">);</span></span></code></pre>
</div>
<p><strong>%s &#8211; String placeholder</strong></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">$username</span> = <span class="kw">$_POST</span><span class="ot">[</span><span class="st">&#39;username&#39;</span><span class="ot">];</span></span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true"></a><span class="kw">$user</span> = <span class="kw">$wpdb</span>-&gt;get_row<span class="ot">(</span></span>
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true"></a>    <span class="kw">$wpdb</span>-&gt;prepare<span class="ot">(</span></span>
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true"></a>        <span class="st">&quot;SELECT * FROM </span><span class="kw">{$wpdb-&gt;users}</span><span class="st"> WHERE user_login = %s&quot;</span><span class="ot">,</span></span>
<span id="cb5-5"><a href="#cb5-5" aria-hidden="true"></a>        <span class="kw">$username</span></span>
<span id="cb5-6"><a href="#cb5-6" aria-hidden="true"></a>    <span class="ot">)</span></span>
<span id="cb5-7"><a href="#cb5-7" aria-hidden="true"></a><span class="ot">);</span></span></code></pre>
</div>
<p><strong>%f &#8211; Float placeholder</strong></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">$price</span> = <span class="kw">$_POST</span><span class="ot">[</span><span class="st">&#39;price&#39;</span><span class="ot">];</span></span>
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true"></a><span class="kw">$products</span> = <span class="kw">$wpdb</span>-&gt;get_results<span class="ot">(</span></span>
<span id="cb6-3"><a href="#cb6-3" aria-hidden="true"></a>    <span class="kw">$wpdb</span>-&gt;prepare<span class="ot">(</span></span>
<span id="cb6-4"><a href="#cb6-4" aria-hidden="true"></a>        <span class="st">&quot;SELECT * FROM </span><span class="kw">{$wpdb-&gt;prefix}</span><span class="st">products WHERE price &gt;= %f&quot;</span><span class="ot">,</span></span>
<span id="cb6-5"><a href="#cb6-5" aria-hidden="true"></a>        <span class="kw">$price</span></span>
<span id="cb6-6"><a href="#cb6-6" aria-hidden="true"></a>    <span class="ot">)</span></span>
<span id="cb6-7"><a href="#cb6-7" aria-hidden="true"></a><span class="ot">);</span></span></code></pre>
</div>
<h3 id="common-prepare-mistakes">Common prepare() Mistakes</h3>
<p><strong>❌ WRONG: Preparing table/column names</strong></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="co">// This doesn&#39;t work - prepare() doesn&#39;t escape identifiers</span></span>
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true"></a><span class="kw">$table</span> = <span class="kw">$_GET</span><span class="ot">[</span><span class="st">&#39;table&#39;</span><span class="ot">];</span></span>
<span id="cb7-3"><a href="#cb7-3" aria-hidden="true"></a><span class="kw">$wpdb</span>-&gt;get_results<span class="ot">(</span></span>
<span id="cb7-4"><a href="#cb7-4" aria-hidden="true"></a>    <span class="kw">$wpdb</span>-&gt;prepare<span class="ot">(</span><span class="st">&quot;SELECT * FROM %s&quot;</span><span class="ot">,</span> <span class="kw">$table</span><span class="ot">)</span> <span class="co">// UNSAFE!</span></span>
<span id="cb7-5"><a href="#cb7-5" aria-hidden="true"></a><span class="ot">);</span></span></code></pre>
</div>
<p><strong>✅ CORRECT: Whitelist approach</strong></p>
<div class="sourceCode" id="cb8">
<pre class="sourceCode php"><code class="sourceCode php"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true"></a><span class="kw">$allowed_tables</span> = <span class="ot">[</span><span class="st">&#39;posts&#39;</span><span class="ot">,</span> <span class="st">&#39;users&#39;</span><span class="ot">,</span> <span class="st">&#39;comments&#39;</span><span class="ot">];</span></span>
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true"></a><span class="kw">$table</span> = <span class="kw">$_GET</span><span class="ot">[</span><span class="st">&#39;table&#39;</span><span class="ot">];</span></span>
<span id="cb8-3"><a href="#cb8-3" aria-hidden="true"></a></span>
<span id="cb8-4"><a href="#cb8-4" aria-hidden="true"></a><span class="kw">if</span> <span class="ot">(</span>!<span class="fu">in_array</span><span class="ot">(</span><span class="kw">$table</span><span class="ot">,</span> <span class="kw">$allowed_tables</span><span class="ot">))</span> {</span>
<span id="cb8-5"><a href="#cb8-5" aria-hidden="true"></a>    wp_die<span class="ot">(</span><span class="st">&#39;Invalid table&#39;</span><span class="ot">);</span></span>
<span id="cb8-6"><a href="#cb8-6" aria-hidden="true"></a>}</span>
<span id="cb8-7"><a href="#cb8-7" aria-hidden="true"></a></span>
<span id="cb8-8"><a href="#cb8-8" aria-hidden="true"></a><span class="co">// Now safe to use</span></span>
<span id="cb8-9"><a href="#cb8-9" aria-hidden="true"></a><span class="kw">$wpdb</span>-&gt;get_results<span class="ot">(</span><span class="st">&quot;SELECT * FROM </span><span class="kw">{$wpdb-&gt;prefix}{$table}</span><span class="st">&quot;</span><span class="ot">);</span></span></code></pre>
</div>
<p><strong>❌ WRONG: Using prepare() with already concatenated strings</strong></p>
<div class="sourceCode" id="cb9">
<pre class="sourceCode php"><code class="sourceCode php"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true"></a><span class="kw">$sql</span> = <span class="st">&quot;SELECT * FROM </span><span class="kw">{$wpdb-&gt;posts}</span><span class="st"> WHERE post_author = &quot;</span> . <span class="kw">$author_id</span><span class="ot">;</span></span>
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true"></a><span class="kw">$wpdb</span>-&gt;get_results<span class="ot">(</span><span class="kw">$wpdb</span>-&gt;prepare<span class="ot">(</span><span class="kw">$sql</span><span class="ot">));</span> <span class="co">// Too late!</span></span></code></pre>
</div>
<h2 id="safe-database-operations">Safe Database Operations</h2>
<h3 id="insert-operations">INSERT Operations</h3>
<p><strong>Using $wpdb-&gt;insert():</strong></p>
<div class="sourceCode" id="cb10">
<pre class="sourceCode php"><code class="sourceCode php"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true"></a><span class="co">// Secure insert method</span></span>
<span id="cb10-2"><a href="#cb10-2" aria-hidden="true"></a><span class="kw">$wpdb</span>-&gt;insert<span class="ot">(</span></span>
<span id="cb10-3"><a href="#cb10-3" aria-hidden="true"></a>    <span class="kw">$wpdb</span>-&gt;prefix . <span class="st">&#39;custom_table&#39;</span><span class="ot">,</span></span>
<span id="cb10-4"><a href="#cb10-4" aria-hidden="true"></a>    <span class="ot">[</span></span>
<span id="cb10-5"><a href="#cb10-5" aria-hidden="true"></a>        <span class="st">&#39;user_id&#39;</span>    =&gt; absint<span class="ot">(</span><span class="kw">$_POST</span><span class="ot">[</span><span class="st">&#39;user_id&#39;</span><span class="ot">]),</span></span>
<span id="cb10-6"><a href="#cb10-6" aria-hidden="true"></a>        <span class="st">&#39;title&#39;</span>      =&gt; sanitize_text_field<span class="ot">(</span><span class="kw">$_POST</span><span class="ot">[</span><span class="st">&#39;title&#39;</span><span class="ot">]),</span></span>
<span id="cb10-7"><a href="#cb10-7" aria-hidden="true"></a>        <span class="st">&#39;content&#39;</span>    =&gt; wp_kses_post<span class="ot">(</span><span class="kw">$_POST</span><span class="ot">[</span><span class="st">&#39;content&#39;</span><span class="ot">]),</span></span>
<span id="cb10-8"><a href="#cb10-8" aria-hidden="true"></a>        <span class="st">&#39;created_at&#39;</span> =&gt; current_time<span class="ot">(</span><span class="st">&#39;mysql&#39;</span><span class="ot">)</span></span>
<span id="cb10-9"><a href="#cb10-9" aria-hidden="true"></a>    <span class="ot">],</span></span>
<span id="cb10-10"><a href="#cb10-10" aria-hidden="true"></a>    <span class="ot">[</span></span>
<span id="cb10-11"><a href="#cb10-11" aria-hidden="true"></a>        <span class="st">&#39;%d&#39;</span><span class="ot">,</span> <span class="co">// user_id format</span></span>
<span id="cb10-12"><a href="#cb10-12" aria-hidden="true"></a>        <span class="st">&#39;%s&#39;</span><span class="ot">,</span> <span class="co">// title format</span></span>
<span id="cb10-13"><a href="#cb10-13" aria-hidden="true"></a>        <span class="st">&#39;%s&#39;</span><span class="ot">,</span> <span class="co">// content format</span></span>
<span id="cb10-14"><a href="#cb10-14" aria-hidden="true"></a>        <span class="st">&#39;%s&#39;</span>  <span class="co">// created_at format</span></span>
<span id="cb10-15"><a href="#cb10-15" aria-hidden="true"></a>    <span class="ot">]</span></span>
<span id="cb10-16"><a href="#cb10-16" aria-hidden="true"></a><span class="ot">);</span></span>
<span id="cb10-17"><a href="#cb10-17" aria-hidden="true"></a></span>
<span id="cb10-18"><a href="#cb10-18" aria-hidden="true"></a><span class="kw">if</span> <span class="ot">(</span><span class="kw">$wpdb</span>-&gt;insert_id<span class="ot">)</span> {</span>
<span id="cb10-19"><a href="#cb10-19" aria-hidden="true"></a>    <span class="kw">echo</span> <span class="st">&quot;Inserted with ID: &quot;</span> . <span class="kw">$wpdb</span>-&gt;insert_id<span class="ot">;</span></span>
<span id="cb10-20"><a href="#cb10-20" aria-hidden="true"></a>}</span></code></pre>
</div>
<h3 id="update-operations">UPDATE Operations</h3>
<p><strong>Using $wpdb-&gt;update():</strong></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">$updated</span> = <span class="kw">$wpdb</span>-&gt;update<span class="ot">(</span></span>
<span id="cb11-2"><a href="#cb11-2" aria-hidden="true"></a>    <span class="kw">$wpdb</span>-&gt;prefix . <span class="st">&#39;custom_table&#39;</span><span class="ot">,</span></span>
<span id="cb11-3"><a href="#cb11-3" aria-hidden="true"></a>    <span class="ot">[</span></span>
<span id="cb11-4"><a href="#cb11-4" aria-hidden="true"></a>        <span class="st">&#39;status&#39;</span> =&gt; sanitize_text_field<span class="ot">(</span><span class="kw">$_POST</span><span class="ot">[</span><span class="st">&#39;status&#39;</span><span class="ot">]),</span></span>
<span id="cb11-5"><a href="#cb11-5" aria-hidden="true"></a>        <span class="st">&#39;updated_at&#39;</span> =&gt; current_time<span class="ot">(</span><span class="st">&#39;mysql&#39;</span><span class="ot">)</span></span>
<span id="cb11-6"><a href="#cb11-6" aria-hidden="true"></a>    <span class="ot">],</span></span>
<span id="cb11-7"><a href="#cb11-7" aria-hidden="true"></a>    <span class="ot">[</span> <span class="st">&#39;id&#39;</span> =&gt; absint<span class="ot">(</span><span class="kw">$_POST</span><span class="ot">[</span><span class="st">&#39;id&#39;</span><span class="ot">])</span> <span class="ot">],</span> <span class="co">// WHERE clause</span></span>
<span id="cb11-8"><a href="#cb11-8" aria-hidden="true"></a>    <span class="ot">[</span> <span class="st">&#39;%s&#39;</span><span class="ot">,</span> <span class="st">&#39;%s&#39;</span> <span class="ot">],</span> <span class="co">// Format for UPDATE data</span></span>
<span id="cb11-9"><a href="#cb11-9" aria-hidden="true"></a>    <span class="ot">[</span> <span class="st">&#39;%d&#39;</span> <span class="ot">]</span>        <span class="co">// Format for WHERE clause</span></span>
<span id="cb11-10"><a href="#cb11-10" aria-hidden="true"></a><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><span class="kw">if</span> <span class="ot">(</span><span class="kw">false</span> === <span class="kw">$updated</span><span class="ot">)</span> {</span>
<span id="cb11-13"><a href="#cb11-13" aria-hidden="true"></a>    <span class="co">// Error occurred</span></span>
<span id="cb11-14"><a href="#cb11-14" aria-hidden="true"></a>    <span class="fu">error_log</span><span class="ot">(</span><span class="kw">$wpdb</span>-&gt;last_error<span class="ot">);</span></span>
<span id="cb11-15"><a href="#cb11-15" aria-hidden="true"></a>}</span></code></pre>
</div>
<h3 id="delete-operations">DELETE Operations</h3>
<p><strong>Using $wpdb-&gt;delete():</strong></p>
<div class="sourceCode" id="cb12">
<pre class="sourceCode php"><code class="sourceCode php"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true"></a><span class="kw">$deleted</span> = <span class="kw">$wpdb</span>-&gt;delete<span class="ot">(</span></span>
<span id="cb12-2"><a href="#cb12-2" aria-hidden="true"></a>    <span class="kw">$wpdb</span>-&gt;prefix . <span class="st">&#39;custom_table&#39;</span><span class="ot">,</span></span>
<span id="cb12-3"><a href="#cb12-3" aria-hidden="true"></a>    <span class="ot">[</span> <span class="st">&#39;id&#39;</span> =&gt; absint<span class="ot">(</span><span class="kw">$_POST</span><span class="ot">[</span><span class="st">&#39;id&#39;</span><span class="ot">])</span> <span class="ot">],</span></span>
<span id="cb12-4"><a href="#cb12-4" aria-hidden="true"></a>    <span class="ot">[</span> <span class="st">&#39;%d&#39;</span> <span class="ot">]</span></span>
<span id="cb12-5"><a href="#cb12-5" aria-hidden="true"></a><span class="ot">);</span></span>
<span id="cb12-6"><a href="#cb12-6" aria-hidden="true"></a></span>
<span id="cb12-7"><a href="#cb12-7" aria-hidden="true"></a><span class="kw">if</span> <span class="ot">(</span><span class="kw">$deleted</span><span class="ot">)</span> {</span>
<span id="cb12-8"><a href="#cb12-8" aria-hidden="true"></a>    <span class="kw">echo</span> <span class="st">&quot;Deleted </span><span class="kw">$deleted</span><span class="st"> row(s)&quot;</span><span class="ot">;</span></span>
<span id="cb12-9"><a href="#cb12-9" aria-hidden="true"></a>}</span></code></pre>
</div>
<h3 id="select-queries">SELECT Queries</h3>
<p><strong>Using prepare() with get_results():</strong></p>
<div class="sourceCode" id="cb13">
<pre class="sourceCode php"><code class="sourceCode php"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true"></a><span class="co">// Multiple results</span></span>
<span id="cb13-2"><a href="#cb13-2" aria-hidden="true"></a><span class="kw">$posts</span> = <span class="kw">$wpdb</span>-&gt;get_results<span class="ot">(</span></span>
<span id="cb13-3"><a href="#cb13-3" aria-hidden="true"></a>    <span class="kw">$wpdb</span>-&gt;prepare<span class="ot">(</span></span>
<span id="cb13-4"><a href="#cb13-4" aria-hidden="true"></a>        <span class="st">&quot;SELECT * FROM </span><span class="kw">{$wpdb-&gt;posts}</span></span>
<span id="cb13-5"><a href="#cb13-5" aria-hidden="true"></a><span class="st">        WHERE post_type = %s</span></span>
<span id="cb13-6"><a href="#cb13-6" aria-hidden="true"></a><span class="st">        AND post_status = %s</span></span>
<span id="cb13-7"><a href="#cb13-7" aria-hidden="true"></a><span class="st">        ORDER BY post_date DESC</span></span>
<span id="cb13-8"><a href="#cb13-8" aria-hidden="true"></a><span class="st">        LIMIT %d&quot;</span><span class="ot">,</span></span>
<span id="cb13-9"><a href="#cb13-9" aria-hidden="true"></a>        <span class="st">&#39;post&#39;</span><span class="ot">,</span></span>
<span id="cb13-10"><a href="#cb13-10" aria-hidden="true"></a>        <span class="st">&#39;publish&#39;</span><span class="ot">,</span></span>
<span id="cb13-11"><a href="#cb13-11" aria-hidden="true"></a>        <span class="dv">10</span></span>
<span id="cb13-12"><a href="#cb13-12" aria-hidden="true"></a>    <span class="ot">)</span></span>
<span id="cb13-13"><a href="#cb13-13" aria-hidden="true"></a><span class="ot">);</span></span>
<span id="cb13-14"><a href="#cb13-14" aria-hidden="true"></a></span>
<span id="cb13-15"><a href="#cb13-15" aria-hidden="true"></a><span class="co">// Single row</span></span>
<span id="cb13-16"><a href="#cb13-16" aria-hidden="true"></a><span class="kw">$post</span> = <span class="kw">$wpdb</span>-&gt;get_row<span class="ot">(</span></span>
<span id="cb13-17"><a href="#cb13-17" aria-hidden="true"></a>    <span class="kw">$wpdb</span>-&gt;prepare<span class="ot">(</span></span>
<span id="cb13-18"><a href="#cb13-18" aria-hidden="true"></a>        <span class="st">&quot;SELECT * FROM </span><span class="kw">{$wpdb-&gt;posts}</span><span class="st"> WHERE ID = %d&quot;</span><span class="ot">,</span></span>
<span id="cb13-19"><a href="#cb13-19" aria-hidden="true"></a>        <span class="kw">$post_id</span></span>
<span id="cb13-20"><a href="#cb13-20" aria-hidden="true"></a>    <span class="ot">)</span></span>
<span id="cb13-21"><a href="#cb13-21" aria-hidden="true"></a><span class="ot">);</span></span>
<span id="cb13-22"><a href="#cb13-22" aria-hidden="true"></a></span>
<span id="cb13-23"><a href="#cb13-23" aria-hidden="true"></a><span class="co">// Single variable</span></span>
<span id="cb13-24"><a href="#cb13-24" aria-hidden="true"></a><span class="kw">$count</span> = <span class="kw">$wpdb</span>-&gt;get_var<span class="ot">(</span></span>
<span id="cb13-25"><a href="#cb13-25" aria-hidden="true"></a>    <span class="kw">$wpdb</span>-&gt;prepare<span class="ot">(</span></span>
<span id="cb13-26"><a href="#cb13-26" aria-hidden="true"></a>        <span class="st">&quot;SELECT COUNT(*) FROM </span><span class="kw">{$wpdb-&gt;posts}</span><span class="st"> WHERE post_author = %d&quot;</span><span class="ot">,</span></span>
<span id="cb13-27"><a href="#cb13-27" aria-hidden="true"></a>        <span class="kw">$author_id</span></span>
<span id="cb13-28"><a href="#cb13-28" aria-hidden="true"></a>    <span class="ot">)</span></span>
<span id="cb13-29"><a href="#cb13-29" aria-hidden="true"></a><span class="ot">);</span></span>
<span id="cb13-30"><a href="#cb13-30" aria-hidden="true"></a></span>
<span id="cb13-31"><a href="#cb13-31" aria-hidden="true"></a><span class="co">// Single column</span></span>
<span id="cb13-32"><a href="#cb13-32" aria-hidden="true"></a><span class="kw">$post_ids</span> = <span class="kw">$wpdb</span>-&gt;get_col<span class="ot">(</span></span>
<span id="cb13-33"><a href="#cb13-33" aria-hidden="true"></a>    <span class="kw">$wpdb</span>-&gt;prepare<span class="ot">(</span></span>
<span id="cb13-34"><a href="#cb13-34" aria-hidden="true"></a>        <span class="st">&quot;SELECT ID FROM </span><span class="kw">{$wpdb-&gt;posts}</span><span class="st"> WHERE post_type = %s&quot;</span><span class="ot">,</span></span>
<span id="cb13-35"><a href="#cb13-35" aria-hidden="true"></a>        <span class="st">&#39;page&#39;</span></span>
<span id="cb13-36"><a href="#cb13-36" aria-hidden="true"></a>    <span class="ot">)</span></span>
<span id="cb13-37"><a href="#cb13-37" aria-hidden="true"></a><span class="ot">);</span></span></code></pre>
</div>
<h2 id="input-validation-and-sanitization">Input Validation and Sanitization</h2>
<h3 id="validate-before-database">Validate Before Database</h3>
<div class="sourceCode" id="cb14">
<pre class="sourceCode php"><code class="sourceCode php"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true"></a><span class="co">// Validate integer</span></span>
<span id="cb14-2"><a href="#cb14-2" aria-hidden="true"></a><span class="kw">$user_id</span> = absint<span class="ot">(</span><span class="kw">$_POST</span><span class="ot">[</span><span class="st">&#39;user_id&#39;</span><span class="ot">]);</span> <span class="co">// Forces positive integer</span></span>
<span id="cb14-3"><a href="#cb14-3" aria-hidden="true"></a></span>
<span id="cb14-4"><a href="#cb14-4" aria-hidden="true"></a><span class="co">// Validate email</span></span>
<span id="cb14-5"><a href="#cb14-5" aria-hidden="true"></a><span class="kw">$email</span> = sanitize_email<span class="ot">(</span><span class="kw">$_POST</span><span class="ot">[</span><span class="st">&#39;email&#39;</span><span class="ot">]);</span></span>
<span id="cb14-6"><a href="#cb14-6" aria-hidden="true"></a><span class="kw">if</span> <span class="ot">(</span>!is_email<span class="ot">(</span><span class="kw">$email</span><span class="ot">))</span> {</span>
<span id="cb14-7"><a href="#cb14-7" aria-hidden="true"></a>    wp_die<span class="ot">(</span><span class="st">&#39;Invalid email address&#39;</span><span class="ot">);</span></span>
<span id="cb14-8"><a href="#cb14-8" aria-hidden="true"></a>}</span>
<span id="cb14-9"><a href="#cb14-9" aria-hidden="true"></a></span>
<span id="cb14-10"><a href="#cb14-10" aria-hidden="true"></a><span class="co">// Validate URL</span></span>
<span id="cb14-11"><a href="#cb14-11" aria-hidden="true"></a><span class="kw">$url</span> = esc_url_raw<span class="ot">(</span><span class="kw">$_POST</span><span class="ot">[</span><span class="st">&#39;url&#39;</span><span class="ot">]);</span></span>
<span id="cb14-12"><a href="#cb14-12" aria-hidden="true"></a></span>
<span id="cb14-13"><a href="#cb14-13" aria-hidden="true"></a><span class="co">// Sanitize text field</span></span>
<span id="cb14-14"><a href="#cb14-14" aria-hidden="true"></a><span class="kw">$title</span> = sanitize_text_field<span class="ot">(</span><span class="kw">$_POST</span><span class="ot">[</span><span class="st">&#39;title&#39;</span><span class="ot">]);</span></span>
<span id="cb14-15"><a href="#cb14-15" aria-hidden="true"></a></span>
<span id="cb14-16"><a href="#cb14-16" aria-hidden="true"></a><span class="co">// Sanitize textarea (allows newlines)</span></span>
<span id="cb14-17"><a href="#cb14-17" aria-hidden="true"></a><span class="kw">$description</span> = sanitize_textarea_field<span class="ot">(</span><span class="kw">$_POST</span><span class="ot">[</span><span class="st">&#39;description&#39;</span><span class="ot">]);</span></span>
<span id="cb14-18"><a href="#cb14-18" aria-hidden="true"></a></span>
<span id="cb14-19"><a href="#cb14-19" aria-hidden="true"></a><span class="co">// HTML content (allows safe HTML)</span></span>
<span id="cb14-20"><a href="#cb14-20" aria-hidden="true"></a><span class="kw">$content</span> = wp_kses_post<span class="ot">(</span><span class="kw">$_POST</span><span class="ot">[</span><span class="st">&#39;content&#39;</span><span class="ot">]);</span></span>
<span id="cb14-21"><a href="#cb14-21" aria-hidden="true"></a></span>
<span id="cb14-22"><a href="#cb14-22" aria-hidden="true"></a><span class="co">// Alphanumeric only</span></span>
<span id="cb14-23"><a href="#cb14-23" aria-hidden="true"></a><span class="kw">$slug</span> = sanitize_key<span class="ot">(</span><span class="kw">$_POST</span><span class="ot">[</span><span class="st">&#39;slug&#39;</span><span class="ot">]);</span></span></code></pre>
</div>
<h3 id="custom-validation-functions">Custom Validation Functions</h3>
<div class="sourceCode" id="cb15">
<pre class="sourceCode php"><code class="sourceCode php"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true"></a><span class="kw">function</span> validate_username<span class="ot">(</span><span class="kw">$username</span><span class="ot">)</span> {</span>
<span id="cb15-2"><a href="#cb15-2" aria-hidden="true"></a>    <span class="co">// Only letters, numbers, underscores, hyphens</span></span>
<span id="cb15-3"><a href="#cb15-3" aria-hidden="true"></a>    <span class="kw">if</span> <span class="ot">(</span>!<span class="fu">preg_match</span><span class="ot">(</span><span class="st">&#39;/^[a-zA-Z0-9_-]+$/&#39;</span><span class="ot">,</span> <span class="kw">$username</span><span class="ot">))</span> {</span>
<span id="cb15-4"><a href="#cb15-4" aria-hidden="true"></a>        <span class="kw">return</span> <span class="kw">false</span><span class="ot">;</span></span>
<span id="cb15-5"><a href="#cb15-5" aria-hidden="true"></a>    }</span>
<span id="cb15-6"><a href="#cb15-6" aria-hidden="true"></a></span>
<span id="cb15-7"><a href="#cb15-7" aria-hidden="true"></a>    <span class="co">// Length check</span></span>
<span id="cb15-8"><a href="#cb15-8" aria-hidden="true"></a>    <span class="kw">if</span> <span class="ot">(</span><span class="fu">strlen</span><span class="ot">(</span><span class="kw">$username</span><span class="ot">)</span> &lt; <span class="dv">3</span> || <span class="fu">strlen</span><span class="ot">(</span><span class="kw">$username</span><span class="ot">)</span> &gt; <span class="dv">20</span><span class="ot">)</span> {</span>
<span id="cb15-9"><a href="#cb15-9" aria-hidden="true"></a>        <span class="kw">return</span> <span class="kw">false</span><span class="ot">;</span></span>
<span id="cb15-10"><a href="#cb15-10" aria-hidden="true"></a>    }</span>
<span id="cb15-11"><a href="#cb15-11" aria-hidden="true"></a></span>
<span id="cb15-12"><a href="#cb15-12" aria-hidden="true"></a>    <span class="kw">return</span> sanitize_user<span class="ot">(</span><span class="kw">$username</span><span class="ot">);</span></span>
<span id="cb15-13"><a href="#cb15-13" aria-hidden="true"></a>}</span>
<span id="cb15-14"><a href="#cb15-14" aria-hidden="true"></a></span>
<span id="cb15-15"><a href="#cb15-15" aria-hidden="true"></a><span class="co">// Usage</span></span>
<span id="cb15-16"><a href="#cb15-16" aria-hidden="true"></a><span class="kw">$username</span> = validate_username<span class="ot">(</span><span class="kw">$_POST</span><span class="ot">[</span><span class="st">&#39;username&#39;</span><span class="ot">]);</span></span>
<span id="cb15-17"><a href="#cb15-17" aria-hidden="true"></a><span class="kw">if</span> <span class="ot">(</span>!<span class="kw">$username</span><span class="ot">)</span> {</span>
<span id="cb15-18"><a href="#cb15-18" aria-hidden="true"></a>    wp_die<span class="ot">(</span><span class="st">&#39;Invalid username format&#39;</span><span class="ot">);</span></span>
<span id="cb15-19"><a href="#cb15-19" aria-hidden="true"></a>}</span></code></pre>
</div>
<h2 id="like-queries-and-wildcards">LIKE Queries and Wildcards</h2>
<h3 id="using-wpdb-esc_like">Using $wpdb-&gt;esc_like()</h3>
<div class="sourceCode" id="cb16">
<pre class="sourceCode php"><code class="sourceCode php"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true"></a><span class="co">// Search with LIKE query</span></span>
<span id="cb16-2"><a href="#cb16-2" aria-hidden="true"></a><span class="kw">$search_term</span> = <span class="kw">$_GET</span><span class="ot">[</span><span class="st">&#39;search&#39;</span><span class="ot">];</span></span>
<span id="cb16-3"><a href="#cb16-3" aria-hidden="true"></a></span>
<span id="cb16-4"><a href="#cb16-4" aria-hidden="true"></a><span class="kw">$results</span> = <span class="kw">$wpdb</span>-&gt;get_results<span class="ot">(</span></span>
<span id="cb16-5"><a href="#cb16-5" aria-hidden="true"></a>    <span class="kw">$wpdb</span>-&gt;prepare<span class="ot">(</span></span>
<span id="cb16-6"><a href="#cb16-6" aria-hidden="true"></a>        <span class="st">&quot;SELECT * FROM </span><span class="kw">{$wpdb-&gt;posts}</span></span>
<span id="cb16-7"><a href="#cb16-7" aria-hidden="true"></a><span class="st">        WHERE post_title LIKE %s&quot;</span><span class="ot">,</span></span>
<span id="cb16-8"><a href="#cb16-8" aria-hidden="true"></a>        <span class="st">&#39;%&#39;</span> . <span class="kw">$wpdb</span>-&gt;esc_like<span class="ot">(</span><span class="kw">$search_term</span><span class="ot">)</span> . <span class="st">&#39;%&#39;</span></span>
<span id="cb16-9"><a href="#cb16-9" aria-hidden="true"></a>    <span class="ot">)</span></span>
<span id="cb16-10"><a href="#cb16-10" aria-hidden="true"></a><span class="ot">);</span></span></code></pre>
</div>
<p><strong>Why esc_like() is necessary:</strong></p>
<p>Without it, users could inject wildcards:</p>
<div class="sourceCode" id="cb17">
<pre class="sourceCode php"><code class="sourceCode php"><span id="cb17-1"><a href="#cb17-1" aria-hidden="true"></a><span class="co">// Malicious input: %</span></span>
<span id="cb17-2"><a href="#cb17-2" aria-hidden="true"></a><span class="co">// Would match everything</span></span></code></pre>
</div>
<h2 id="handling-dynamic-order-by">Handling Dynamic ORDER BY</h2>
<h3 id="whitelist-approach">Whitelist Approach</h3>
<div class="sourceCode" id="cb18">
<pre class="sourceCode php"><code class="sourceCode php"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true"></a><span class="co">// UNSAFE - Never do this</span></span>
<span id="cb18-2"><a href="#cb18-2" aria-hidden="true"></a><span class="kw">$orderby</span> = <span class="kw">$_GET</span><span class="ot">[</span><span class="st">&#39;orderby&#39;</span><span class="ot">];</span></span>
<span id="cb18-3"><a href="#cb18-3" aria-hidden="true"></a><span class="kw">$sql</span> = <span class="st">&quot;SELECT * FROM </span><span class="kw">{$wpdb-&gt;posts}</span><span class="st"> ORDER BY </span><span class="kw">$orderby</span><span class="st">&quot;</span><span class="ot">;</span></span>
<span id="cb18-4"><a href="#cb18-4" aria-hidden="true"></a></span>
<span id="cb18-5"><a href="#cb18-5" aria-hidden="true"></a><span class="co">// SAFE - Whitelist allowed columns</span></span>
<span id="cb18-6"><a href="#cb18-6" aria-hidden="true"></a><span class="kw">$orderby</span> = <span class="kw">$_GET</span><span class="ot">[</span><span class="st">&#39;orderby&#39;</span><span class="ot">];</span></span>
<span id="cb18-7"><a href="#cb18-7" aria-hidden="true"></a><span class="kw">$allowed_orderby</span> = <span class="ot">[</span></span>
<span id="cb18-8"><a href="#cb18-8" aria-hidden="true"></a>    <span class="st">&#39;title&#39;</span> =&gt; <span class="st">&#39;post_title&#39;</span><span class="ot">,</span></span>
<span id="cb18-9"><a href="#cb18-9" aria-hidden="true"></a>    <span class="st">&#39;date&#39;</span>  =&gt; <span class="st">&#39;post_date&#39;</span><span class="ot">,</span></span>
<span id="cb18-10"><a href="#cb18-10" aria-hidden="true"></a>    <span class="st">&#39;author&#39;</span> =&gt; <span class="st">&#39;post_author&#39;</span></span>
<span id="cb18-11"><a href="#cb18-11" aria-hidden="true"></a><span class="ot">];</span></span>
<span id="cb18-12"><a href="#cb18-12" aria-hidden="true"></a></span>
<span id="cb18-13"><a href="#cb18-13" aria-hidden="true"></a><span class="kw">$orderby_column</span> = <span class="kw">isset</span><span class="ot">(</span><span class="kw">$allowed_orderby</span><span class="ot">[</span><span class="kw">$orderby</span><span class="ot">])</span></span>
<span id="cb18-14"><a href="#cb18-14" aria-hidden="true"></a>    <span class="ot">?</span> <span class="kw">$allowed_orderby</span><span class="ot">[</span><span class="kw">$orderby</span><span class="ot">]</span></span>
<span id="cb18-15"><a href="#cb18-15" aria-hidden="true"></a>    <span class="ot">:</span> <span class="st">&#39;post_date&#39;</span><span class="ot">;</span></span>
<span id="cb18-16"><a href="#cb18-16" aria-hidden="true"></a></span>
<span id="cb18-17"><a href="#cb18-17" aria-hidden="true"></a><span class="kw">$order</span> = <span class="ot">(</span><span class="kw">$_GET</span><span class="ot">[</span><span class="st">&#39;order&#39;</span><span class="ot">]</span> === <span class="st">&#39;ASC&#39;</span><span class="ot">)</span> <span class="ot">?</span> <span class="st">&#39;ASC&#39;</span> <span class="ot">:</span> <span class="st">&#39;DESC&#39;</span><span class="ot">;</span></span>
<span id="cb18-18"><a href="#cb18-18" aria-hidden="true"></a></span>
<span id="cb18-19"><a href="#cb18-19" aria-hidden="true"></a><span class="kw">$results</span> = <span class="kw">$wpdb</span>-&gt;get_results<span class="ot">(</span></span>
<span id="cb18-20"><a href="#cb18-20" aria-hidden="true"></a>    <span class="st">&quot;SELECT * FROM </span><span class="kw">{$wpdb-&gt;posts}</span></span>
<span id="cb18-21"><a href="#cb18-21" aria-hidden="true"></a><span class="st">    ORDER BY </span><span class="kw">{$orderby_column}</span><span class="st"> </span><span class="kw">{$order}</span><span class="st">&quot;</span></span>
<span id="cb18-22"><a href="#cb18-22" aria-hidden="true"></a><span class="ot">);</span></span></code></pre>
</div>
<h2 id="in-clause-with-multiple-values">IN Clause with Multiple Values</h2>
<h3 id="safe-array-handling">Safe Array Handling</h3>
<div class="sourceCode" id="cb19">
<pre class="sourceCode php"><code class="sourceCode php"><span id="cb19-1"><a href="#cb19-1" aria-hidden="true"></a><span class="co">// Get posts with specific IDs</span></span>
<span id="cb19-2"><a href="#cb19-2" aria-hidden="true"></a><span class="kw">$post_ids</span> = <span class="kw">$_POST</span><span class="ot">[</span><span class="st">&#39;post_ids&#39;</span><span class="ot">];</span> <span class="co">// Array of IDs</span></span>
<span id="cb19-3"><a href="#cb19-3" aria-hidden="true"></a></span>
<span id="cb19-4"><a href="#cb19-4" aria-hidden="true"></a><span class="co">// Sanitize each ID</span></span>
<span id="cb19-5"><a href="#cb19-5" aria-hidden="true"></a><span class="kw">$post_ids</span> = <span class="fu">array_map</span><span class="ot">(</span><span class="st">&#39;absint&#39;</span><span class="ot">,</span> <span class="kw">$post_ids</span><span class="ot">);</span></span>
<span id="cb19-6"><a href="#cb19-6" aria-hidden="true"></a></span>
<span id="cb19-7"><a href="#cb19-7" aria-hidden="true"></a><span class="co">// Create placeholders</span></span>
<span id="cb19-8"><a href="#cb19-8" aria-hidden="true"></a><span class="kw">$placeholders</span> = <span class="fu">implode</span><span class="ot">(</span><span class="st">&#39;,&#39;</span><span class="ot">,</span> <span class="fu">array_fill</span><span class="ot">(</span><span class="dv">0</span><span class="ot">,</span> <span class="fu">count</span><span class="ot">(</span><span class="kw">$post_ids</span><span class="ot">),</span> <span class="st">&#39;%d&#39;</span><span class="ot">));</span></span>
<span id="cb19-9"><a href="#cb19-9" aria-hidden="true"></a></span>
<span id="cb19-10"><a href="#cb19-10" aria-hidden="true"></a><span class="kw">$results</span> = <span class="kw">$wpdb</span>-&gt;get_results<span class="ot">(</span></span>
<span id="cb19-11"><a href="#cb19-11" aria-hidden="true"></a>    <span class="kw">$wpdb</span>-&gt;prepare<span class="ot">(</span></span>
<span id="cb19-12"><a href="#cb19-12" aria-hidden="true"></a>        <span class="st">&quot;SELECT * FROM </span><span class="kw">{$wpdb-&gt;posts}</span><span class="st"> WHERE ID IN (</span><span class="kw">$placeholders</span><span class="st">)&quot;</span><span class="ot">,</span></span>
<span id="cb19-13"><a href="#cb19-13" aria-hidden="true"></a>        <span class="kw">$post_ids</span></span>
<span id="cb19-14"><a href="#cb19-14" aria-hidden="true"></a>    <span class="ot">)</span></span>
<span id="cb19-15"><a href="#cb19-15" aria-hidden="true"></a><span class="ot">);</span></span></code></pre>
</div>
<p><strong>For string arrays:</strong></p>
<div class="sourceCode" id="cb20">
<pre class="sourceCode php"><code class="sourceCode php"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true"></a><span class="kw">$categories</span> = <span class="kw">$_POST</span><span class="ot">[</span><span class="st">&#39;categories&#39;</span><span class="ot">];</span> <span class="co">// Array of strings</span></span>
<span id="cb20-2"><a href="#cb20-2" aria-hidden="true"></a></span>
<span id="cb20-3"><a href="#cb20-3" aria-hidden="true"></a><span class="co">// Sanitize</span></span>
<span id="cb20-4"><a href="#cb20-4" aria-hidden="true"></a><span class="kw">$categories</span> = <span class="fu">array_map</span><span class="ot">(</span><span class="st">&#39;sanitize_text_field&#39;</span><span class="ot">,</span> <span class="kw">$categories</span><span class="ot">);</span></span>
<span id="cb20-5"><a href="#cb20-5" aria-hidden="true"></a></span>
<span id="cb20-6"><a href="#cb20-6" aria-hidden="true"></a><span class="co">// Placeholders</span></span>
<span id="cb20-7"><a href="#cb20-7" aria-hidden="true"></a><span class="kw">$placeholders</span> = <span class="fu">implode</span><span class="ot">(</span><span class="st">&#39;,&#39;</span><span class="ot">,</span> <span class="fu">array_fill</span><span class="ot">(</span><span class="dv">0</span><span class="ot">,</span> <span class="fu">count</span><span class="ot">(</span><span class="kw">$categories</span><span class="ot">),</span> <span class="st">&#39;%s&#39;</span><span class="ot">));</span></span>
<span id="cb20-8"><a href="#cb20-8" aria-hidden="true"></a></span>
<span id="cb20-9"><a href="#cb20-9" aria-hidden="true"></a><span class="kw">$results</span> = <span class="kw">$wpdb</span>-&gt;get_results<span class="ot">(</span></span>
<span id="cb20-10"><a href="#cb20-10" aria-hidden="true"></a>    <span class="kw">$wpdb</span>-&gt;prepare<span class="ot">(</span></span>
<span id="cb20-11"><a href="#cb20-11" aria-hidden="true"></a>        <span class="st">&quot;SELECT * FROM </span><span class="kw">{$wpdb-&gt;terms}</span><span class="st"> WHERE slug IN (</span><span class="kw">$placeholders</span><span class="st">)&quot;</span><span class="ot">,</span></span>
<span id="cb20-12"><a href="#cb20-12" aria-hidden="true"></a>        <span class="kw">$categories</span></span>
<span id="cb20-13"><a href="#cb20-13" aria-hidden="true"></a>    <span class="ot">)</span></span>
<span id="cb20-14"><a href="#cb20-14" aria-hidden="true"></a><span class="ot">);</span></span></code></pre>
</div>
<h2 id="using-wordpress-core-functions">Using WordPress Core Functions</h2>
<h3 id="prefer-wp-functions-over-raw-sql">Prefer WP Functions Over Raw SQL</h3>
<p><strong>Instead of raw queries, use:</strong></p>
<div class="sourceCode" id="cb21">
<pre class="sourceCode php"><code class="sourceCode php"><span id="cb21-1"><a href="#cb21-1" aria-hidden="true"></a><span class="co">// Get posts</span></span>
<span id="cb21-2"><a href="#cb21-2" aria-hidden="true"></a><span class="kw">$posts</span> = get_posts<span class="ot">([</span></span>
<span id="cb21-3"><a href="#cb21-3" aria-hidden="true"></a>    <span class="st">&#39;post_type&#39;</span>   =&gt; <span class="st">&#39;product&#39;</span><span class="ot">,</span></span>
<span id="cb21-4"><a href="#cb21-4" aria-hidden="true"></a>    <span class="st">&#39;post_status&#39;</span> =&gt; <span class="st">&#39;publish&#39;</span><span class="ot">,</span></span>
<span id="cb21-5"><a href="#cb21-5" aria-hidden="true"></a>    <span class="st">&#39;author&#39;</span>      =&gt; <span class="kw">$author_id</span><span class="ot">,</span></span>
<span id="cb21-6"><a href="#cb21-6" aria-hidden="true"></a>    <span class="st">&#39;numberposts&#39;</span> =&gt; <span class="dv">10</span></span>
<span id="cb21-7"><a href="#cb21-7" aria-hidden="true"></a><span class="ot">]);</span></span>
<span id="cb21-8"><a href="#cb21-8" aria-hidden="true"></a></span>
<span id="cb21-9"><a href="#cb21-9" aria-hidden="true"></a><span class="co">// Get users</span></span>
<span id="cb21-10"><a href="#cb21-10" aria-hidden="true"></a><span class="kw">$users</span> = get_users<span class="ot">([</span></span>
<span id="cb21-11"><a href="#cb21-11" aria-hidden="true"></a>    <span class="st">&#39;role&#39;</span>    =&gt; <span class="st">&#39;subscriber&#39;</span><span class="ot">,</span></span>
<span id="cb21-12"><a href="#cb21-12" aria-hidden="true"></a>    <span class="st">&#39;orderby&#39;</span> =&gt; <span class="st">&#39;registered&#39;</span><span class="ot">,</span></span>
<span id="cb21-13"><a href="#cb21-13" aria-hidden="true"></a>    <span class="st">&#39;number&#39;</span>  =&gt; <span class="dv">50</span></span>
<span id="cb21-14"><a href="#cb21-14" aria-hidden="true"></a><span class="ot">]);</span></span>
<span id="cb21-15"><a href="#cb21-15" aria-hidden="true"></a></span>
<span id="cb21-16"><a href="#cb21-16" aria-hidden="true"></a><span class="co">// Get post meta</span></span>
<span id="cb21-17"><a href="#cb21-17" aria-hidden="true"></a><span class="kw">$value</span> = get_post_meta<span class="ot">(</span><span class="kw">$post_id</span><span class="ot">,</span> <span class="st">&#39;custom_field&#39;</span><span class="ot">,</span> <span class="kw">true</span><span class="ot">);</span></span>
<span id="cb21-18"><a href="#cb21-18" aria-hidden="true"></a></span>
<span id="cb21-19"><a href="#cb21-19" aria-hidden="true"></a><span class="co">// WP_Query for complex queries</span></span>
<span id="cb21-20"><a href="#cb21-20" aria-hidden="true"></a><span class="kw">$query</span> = <span class="kw">new</span> WP_Query<span class="ot">([</span></span>
<span id="cb21-21"><a href="#cb21-21" aria-hidden="true"></a>    <span class="st">&#39;post_type&#39;</span> =&gt; <span class="st">&#39;post&#39;</span><span class="ot">,</span></span>
<span id="cb21-22"><a href="#cb21-22" aria-hidden="true"></a>    <span class="st">&#39;meta_query&#39;</span> =&gt; <span class="ot">[</span></span>
<span id="cb21-23"><a href="#cb21-23" aria-hidden="true"></a>        <span class="ot">[</span></span>
<span id="cb21-24"><a href="#cb21-24" aria-hidden="true"></a>            <span class="st">&#39;key&#39;</span>     =&gt; <span class="st">&#39;price&#39;</span><span class="ot">,</span></span>
<span id="cb21-25"><a href="#cb21-25" aria-hidden="true"></a>            <span class="st">&#39;value&#39;</span>   =&gt; <span class="dv">100</span><span class="ot">,</span></span>
<span id="cb21-26"><a href="#cb21-26" aria-hidden="true"></a>            <span class="st">&#39;compare&#39;</span> =&gt; <span class="st">&#39;&gt;=&#39;</span><span class="ot">,</span></span>
<span id="cb21-27"><a href="#cb21-27" aria-hidden="true"></a>            <span class="st">&#39;type&#39;</span>    =&gt; <span class="st">&#39;NUMERIC&#39;</span></span>
<span id="cb21-28"><a href="#cb21-28" aria-hidden="true"></a>        <span class="ot">]</span></span>
<span id="cb21-29"><a href="#cb21-29" aria-hidden="true"></a>    <span class="ot">]</span></span>
<span id="cb21-30"><a href="#cb21-30" aria-hidden="true"></a><span class="ot">]);</span></span></code></pre>
</div>
<h2 id="custom-table-best-practices">Custom Table Best Practices</h2>
<h3 id="creating-secure-custom-tables">Creating Secure Custom Tables</h3>
<div class="sourceCode" id="cb22">
<pre class="sourceCode php"><code class="sourceCode php"><span id="cb22-1"><a href="#cb22-1" aria-hidden="true"></a><span class="kw">function</span> create_custom_table<span class="ot">()</span> {</span>
<span id="cb22-2"><a href="#cb22-2" aria-hidden="true"></a>    <span class="kw">global</span> <span class="kw">$wpdb</span><span class="ot">;</span></span>
<span id="cb22-3"><a href="#cb22-3" aria-hidden="true"></a>    <span class="kw">$table_name</span> = <span class="kw">$wpdb</span>-&gt;prefix . <span class="st">&#39;my_custom_table&#39;</span><span class="ot">;</span></span>
<span id="cb22-4"><a href="#cb22-4" aria-hidden="true"></a>    <span class="kw">$charset_collate</span> = <span class="kw">$wpdb</span>-&gt;get_charset_collate<span class="ot">();</span></span>
<span id="cb22-5"><a href="#cb22-5" aria-hidden="true"></a></span>
<span id="cb22-6"><a href="#cb22-6" aria-hidden="true"></a>    <span class="kw">$sql</span> = <span class="st">&quot;CREATE TABLE IF NOT EXISTS </span><span class="kw">$table_name</span><span class="st"> (</span></span>
<span id="cb22-7"><a href="#cb22-7" aria-hidden="true"></a><span class="st">        id mediumint(9) NOT NULL AUTO_INCREMENT,</span></span>
<span id="cb22-8"><a href="#cb22-8" aria-hidden="true"></a><span class="st">        user_id bigint(20) NOT NULL,</span></span>
<span id="cb22-9"><a href="#cb22-9" aria-hidden="true"></a><span class="st">        title varchar(255) NOT NULL,</span></span>
<span id="cb22-10"><a href="#cb22-10" aria-hidden="true"></a><span class="st">        content longtext NOT NULL,</span></span>
<span id="cb22-11"><a href="#cb22-11" aria-hidden="true"></a><span class="st">        status varchar(20) NOT NULL,</span></span>
<span id="cb22-12"><a href="#cb22-12" aria-hidden="true"></a><span class="st">        created_at datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,</span></span>
<span id="cb22-13"><a href="#cb22-13" aria-hidden="true"></a><span class="st">        PRIMARY KEY  (id),</span></span>
<span id="cb22-14"><a href="#cb22-14" aria-hidden="true"></a><span class="st">        KEY user_id (user_id)</span></span>
<span id="cb22-15"><a href="#cb22-15" aria-hidden="true"></a><span class="st">    ) </span><span class="kw">$charset_collate</span><span class="st">;&quot;</span><span class="ot">;</span></span>
<span id="cb22-16"><a href="#cb22-16" aria-hidden="true"></a></span>
<span id="cb22-17"><a href="#cb22-17" aria-hidden="true"></a>    <span class="kw">require_once</span><span class="ot">(</span><span class="kw">ABSPATH</span> . <span class="st">&#39;wp-admin/includes/upgrade.php&#39;</span><span class="ot">);</span></span>
<span id="cb22-18"><a href="#cb22-18" aria-hidden="true"></a>    dbDelta<span class="ot">(</span><span class="kw">$sql</span><span class="ot">);</span></span>
<span id="cb22-19"><a href="#cb22-19" aria-hidden="true"></a>}</span></code></pre>
</div>
<h3 id="querying-custom-tables-safely">Querying Custom Tables Safely</h3>
<div class="sourceCode" id="cb23">
<pre class="sourceCode php"><code class="sourceCode php"><span id="cb23-1"><a href="#cb23-1" aria-hidden="true"></a><span class="kw">$table_name</span> = <span class="kw">$wpdb</span>-&gt;prefix . <span class="st">&#39;my_custom_table&#39;</span><span class="ot">;</span></span>
<span id="cb23-2"><a href="#cb23-2" aria-hidden="true"></a></span>
<span id="cb23-3"><a href="#cb23-3" aria-hidden="true"></a><span class="co">// Insert</span></span>
<span id="cb23-4"><a href="#cb23-4" aria-hidden="true"></a><span class="kw">$wpdb</span>-&gt;insert<span class="ot">(</span></span>
<span id="cb23-5"><a href="#cb23-5" aria-hidden="true"></a>    <span class="kw">$table_name</span><span class="ot">,</span></span>
<span id="cb23-6"><a href="#cb23-6" aria-hidden="true"></a>    <span class="ot">[</span></span>
<span id="cb23-7"><a href="#cb23-7" aria-hidden="true"></a>        <span class="st">&#39;user_id&#39;</span> =&gt; get_current_user_id<span class="ot">(),</span></span>
<span id="cb23-8"><a href="#cb23-8" aria-hidden="true"></a>        <span class="st">&#39;title&#39;</span>   =&gt; sanitize_text_field<span class="ot">(</span><span class="kw">$_POST</span><span class="ot">[</span><span class="st">&#39;title&#39;</span><span class="ot">]),</span></span>
<span id="cb23-9"><a href="#cb23-9" aria-hidden="true"></a>        <span class="st">&#39;content&#39;</span> =&gt; wp_kses_post<span class="ot">(</span><span class="kw">$_POST</span><span class="ot">[</span><span class="st">&#39;content&#39;</span><span class="ot">]),</span></span>
<span id="cb23-10"><a href="#cb23-10" aria-hidden="true"></a>        <span class="st">&#39;status&#39;</span>  =&gt; <span class="st">&#39;pending&#39;</span></span>
<span id="cb23-11"><a href="#cb23-11" aria-hidden="true"></a>    <span class="ot">],</span></span>
<span id="cb23-12"><a href="#cb23-12" aria-hidden="true"></a>    <span class="ot">[</span><span class="st">&#39;%d&#39;</span><span class="ot">,</span> <span class="st">&#39;%s&#39;</span><span class="ot">,</span> <span class="st">&#39;%s&#39;</span><span class="ot">,</span> <span class="st">&#39;%s&#39;</span><span class="ot">]</span></span>
<span id="cb23-13"><a href="#cb23-13" aria-hidden="true"></a><span class="ot">);</span></span>
<span id="cb23-14"><a href="#cb23-14" aria-hidden="true"></a></span>
<span id="cb23-15"><a href="#cb23-15" aria-hidden="true"></a><span class="co">// Select</span></span>
<span id="cb23-16"><a href="#cb23-16" aria-hidden="true"></a><span class="kw">$results</span> = <span class="kw">$wpdb</span>-&gt;get_results<span class="ot">(</span></span>
<span id="cb23-17"><a href="#cb23-17" aria-hidden="true"></a>    <span class="kw">$wpdb</span>-&gt;prepare<span class="ot">(</span></span>
<span id="cb23-18"><a href="#cb23-18" aria-hidden="true"></a>        <span class="st">&quot;SELECT * FROM </span><span class="kw">$table_name</span><span class="st"> WHERE user_id = %d AND status = %s&quot;</span><span class="ot">,</span></span>
<span id="cb23-19"><a href="#cb23-19" aria-hidden="true"></a>        <span class="kw">$user_id</span><span class="ot">,</span></span>
<span id="cb23-20"><a href="#cb23-20" aria-hidden="true"></a>        <span class="st">&#39;approved&#39;</span></span>
<span id="cb23-21"><a href="#cb23-21" aria-hidden="true"></a>    <span class="ot">)</span></span>
<span id="cb23-22"><a href="#cb23-22" aria-hidden="true"></a><span class="ot">);</span></span></code></pre>
</div>
<h2 id="testing-for-sql-injection">Testing for SQL Injection</h2>
<h3 id="manual-testing">Manual Testing</h3>
<p>Try these payloads in inputs:</p>
<pre><code>1&#39; OR &#39;1&#39;=&#39;1
1; DROP TABLE wp_posts;--
&#39; UNION SELECT NULL, NULL, NULL--
1&#39; AND 1=0 UNION ALL SELECT table_name,2,3 FROM information_schema.tables--</code></pre>
<p>If any return unexpected results or errors, you have SQL injection.</p>
<h3 id="automated-security-scanning">Automated Security Scanning</h3>
<div class="sourceCode" id="cb25">
<pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true"></a><span class="co"># WPScan</span></span>
<span id="cb25-2"><a href="#cb25-2" aria-hidden="true"></a><span class="ex">wpscan</span> --url https://yoursite.com --enumerate vp</span>
<span id="cb25-3"><a href="#cb25-3" aria-hidden="true"></a></span>
<span id="cb25-4"><a href="#cb25-4" aria-hidden="true"></a><span class="co"># RIPS (PHP security scanner)</span></span>
<span id="cb25-5"><a href="#cb25-5" aria-hidden="true"></a><span class="co"># Upload code to https://github.com/ripsscanner/rips</span></span>
<span id="cb25-6"><a href="#cb25-6" aria-hidden="true"></a></span>
<span id="cb25-7"><a href="#cb25-7" aria-hidden="true"></a><span class="co"># Psalm with security plugin</span></span>
<span id="cb25-8"><a href="#cb25-8" aria-hidden="true"></a><span class="ex">composer</span> require --dev vimeo/psalm psalm/plugin-wordpress</span>
<span id="cb25-9"><a href="#cb25-9" aria-hidden="true"></a><span class="ex">psalm</span> --taint-analysis</span></code></pre>
</div>
<h2 id="common-vulnerable-patterns">Common Vulnerable Patterns</h2>
<h3 id="pattern-1-direct-variable-insertion">Pattern 1: Direct Variable Insertion</h3>
<div class="sourceCode" id="cb26">
<pre class="sourceCode php"><code class="sourceCode php"><span id="cb26-1"><a href="#cb26-1" aria-hidden="true"></a><span class="co">// VULNERABLE</span></span>
<span id="cb26-2"><a href="#cb26-2" aria-hidden="true"></a><span class="kw">$id</span> = <span class="kw">$_GET</span><span class="ot">[</span><span class="st">&#39;id&#39;</span><span class="ot">];</span></span>
<span id="cb26-3"><a href="#cb26-3" aria-hidden="true"></a><span class="kw">$wpdb</span>-&gt;query<span class="ot">(</span><span class="st">&quot;DELETE FROM </span><span class="kw">{$wpdb-&gt;posts}</span><span class="st"> WHERE ID = </span><span class="kw">$id</span><span class="st">&quot;</span><span class="ot">);</span></span>
<span id="cb26-4"><a href="#cb26-4" aria-hidden="true"></a></span>
<span id="cb26-5"><a href="#cb26-5" aria-hidden="true"></a><span class="co">// SECURE</span></span>
<span id="cb26-6"><a href="#cb26-6" aria-hidden="true"></a><span class="kw">$id</span> = absint<span class="ot">(</span><span class="kw">$_GET</span><span class="ot">[</span><span class="st">&#39;id&#39;</span><span class="ot">]);</span></span>
<span id="cb26-7"><a href="#cb26-7" aria-hidden="true"></a><span class="kw">$wpdb</span>-&gt;delete<span class="ot">(</span><span class="kw">$wpdb</span>-&gt;posts<span class="ot">,</span> <span class="ot">[</span><span class="st">&#39;ID&#39;</span> =&gt; <span class="kw">$id</span><span class="ot">],</span> <span class="ot">[</span><span class="st">&#39;%d&#39;</span><span class="ot">]);</span></span></code></pre>
</div>
<h3 id="pattern-2-string-concatenation">Pattern 2: String Concatenation</h3>
<div class="sourceCode" id="cb27">
<pre class="sourceCode php"><code class="sourceCode php"><span id="cb27-1"><a href="#cb27-1" aria-hidden="true"></a><span class="co">// VULNERABLE</span></span>
<span id="cb27-2"><a href="#cb27-2" aria-hidden="true"></a><span class="kw">$search</span> = <span class="kw">$_POST</span><span class="ot">[</span><span class="st">&#39;search&#39;</span><span class="ot">];</span></span>
<span id="cb27-3"><a href="#cb27-3" aria-hidden="true"></a><span class="kw">$sql</span> = <span class="st">&quot;SELECT * FROM </span><span class="kw">{$wpdb-&gt;posts}</span><span class="st"> WHERE post_title LIKE &#39;%&quot;</span> . <span class="kw">$search</span> . <span class="st">&quot;%&#39;&quot;</span><span class="ot">;</span></span>
<span id="cb27-4"><a href="#cb27-4" aria-hidden="true"></a></span>
<span id="cb27-5"><a href="#cb27-5" aria-hidden="true"></a><span class="co">// SECURE</span></span>
<span id="cb27-6"><a href="#cb27-6" aria-hidden="true"></a><span class="kw">$search</span> = <span class="kw">$_POST</span><span class="ot">[</span><span class="st">&#39;search&#39;</span><span class="ot">];</span></span>
<span id="cb27-7"><a href="#cb27-7" aria-hidden="true"></a><span class="kw">$results</span> = <span class="kw">$wpdb</span>-&gt;get_results<span class="ot">(</span></span>
<span id="cb27-8"><a href="#cb27-8" aria-hidden="true"></a>    <span class="kw">$wpdb</span>-&gt;prepare<span class="ot">(</span></span>
<span id="cb27-9"><a href="#cb27-9" aria-hidden="true"></a>        <span class="st">&quot;SELECT * FROM </span><span class="kw">{$wpdb-&gt;posts}</span><span class="st"> WHERE post_title LIKE %s&quot;</span><span class="ot">,</span></span>
<span id="cb27-10"><a href="#cb27-10" aria-hidden="true"></a>        <span class="st">&#39;%&#39;</span> . <span class="kw">$wpdb</span>-&gt;esc_like<span class="ot">(</span><span class="kw">$search</span><span class="ot">)</span> . <span class="st">&#39;%&#39;</span></span>
<span id="cb27-11"><a href="#cb27-11" aria-hidden="true"></a>    <span class="ot">)</span></span>
<span id="cb27-12"><a href="#cb27-12" aria-hidden="true"></a><span class="ot">);</span></span></code></pre>
</div>
<h3 id="pattern-3-unvalidated-array-access">Pattern 3: Unvalidated Array Access</h3>
<div class="sourceCode" id="cb28">
<pre class="sourceCode php"><code class="sourceCode php"><span id="cb28-1"><a href="#cb28-1" aria-hidden="true"></a><span class="co">// VULNERABLE</span></span>
<span id="cb28-2"><a href="#cb28-2" aria-hidden="true"></a><span class="kw">$ids</span> = <span class="kw">$_POST</span><span class="ot">[</span><span class="st">&#39;ids&#39;</span><span class="ot">];</span> <span class="co">// &quot;1,2,3 OR 1=1&quot;</span></span>
<span id="cb28-3"><a href="#cb28-3" aria-hidden="true"></a><span class="kw">$sql</span> = <span class="st">&quot;SELECT * FROM </span><span class="kw">{$wpdb-&gt;posts}</span><span class="st"> WHERE ID IN (</span><span class="kw">$ids</span><span class="st">)&quot;</span><span class="ot">;</span></span>
<span id="cb28-4"><a href="#cb28-4" aria-hidden="true"></a></span>
<span id="cb28-5"><a href="#cb28-5" aria-hidden="true"></a><span class="co">// SECURE</span></span>
<span id="cb28-6"><a href="#cb28-6" aria-hidden="true"></a><span class="kw">$ids</span> = <span class="fu">array_map</span><span class="ot">(</span><span class="st">&#39;absint&#39;</span><span class="ot">,</span> <span class="kw">$_POST</span><span class="ot">[</span><span class="st">&#39;ids&#39;</span><span class="ot">]);</span></span>
<span id="cb28-7"><a href="#cb28-7" aria-hidden="true"></a><span class="kw">$placeholders</span> = <span class="fu">implode</span><span class="ot">(</span><span class="st">&#39;,&#39;</span><span class="ot">,</span> <span class="fu">array_fill</span><span class="ot">(</span><span class="dv">0</span><span class="ot">,</span> <span class="fu">count</span><span class="ot">(</span><span class="kw">$ids</span><span class="ot">),</span> <span class="st">&#39;%d&#39;</span><span class="ot">));</span></span>
<span id="cb28-8"><a href="#cb28-8" aria-hidden="true"></a><span class="kw">$results</span> = <span class="kw">$wpdb</span>-&gt;get_results<span class="ot">(</span></span>
<span id="cb28-9"><a href="#cb28-9" aria-hidden="true"></a>    <span class="kw">$wpdb</span>-&gt;prepare<span class="ot">(</span><span class="st">&quot;SELECT * FROM </span><span class="kw">{$wpdb-&gt;posts}</span><span class="st"> WHERE ID IN (</span><span class="kw">$placeholders</span><span class="st">)&quot;</span><span class="ot">,</span> <span class="kw">$ids</span><span class="ot">)</span></span>
<span id="cb28-10"><a href="#cb28-10" aria-hidden="true"></a><span class="ot">);</span></span></code></pre>
</div>
<h2 id="security-checklist">Security Checklist</h2>
<p>✅ Use $wpdb-&gt;prepare() for all queries with variables ✅ Validate all user input (absint, sanitize_text_field, etc.) ✅ Use $wpdb-&gt;insert/update/delete methods ✅ Whitelist table/column names (never from user input) ✅ Use $wpdb-&gt;esc_like() for LIKE queries ✅ Prefer WordPress core functions over raw SQL ✅ Test with SQL injection payloads ✅ Regular security audits and code reviews ✅ Use static analysis tools ✅ Keep WordPress and plugins updated</p>
<p>SQL injection is preventable through disciplined coding practices. Always treat user input as hostile, validate rigorously, use prepared statements consistently, and leverage WordPress’s built-in security functions.</p>
<h2 id="external-links">External Links</h2>
<ol type="1">
<li><a href="https://owasp.org/www-community/attacks/SQL_Injection">SQL Injection (OWASP)</a></li>
<li><a href="https://developer.wordpress.org/reference/classes/wpdb/">wpdb Class Documentation</a></li>
<li><a href="https://developer.wordpress.org/plugins/security/data-validation/">Data Validation</a></li>
<li><a href="https://developer.wordpress.org/apis/security/sanitizing-securing-output/">Prepared Statements</a></li>
<li><a href="https://developer.wordpress.org/apis/security/">WordPress Security Handbook</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/preventing-sql-injection-in-wordpress-security-best-practices/">Preventing SQL Injection in WordPress: Security Best Practices</a> appeared first on <a href="https://developryplugins.com">Developry Plugins</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
