Sql Injection Prevention logo

Sql Injection Prevention

Organization
wpultimatesecurity
sql-injection-prevention

Use when writing any custom database query in WordPress with $wpdb — get_results, get_var, get_row, query, or building WHERE/IN/LIKE/ORDER BY clauses. Uses $wpdb->prepare() with correct placeholders (%d, %s, %f, %i), $wpdb->esc_like() for LIKE, and allowlists for identifiers that cannot be parameterized. Prevents SQL injection. Apply proactively to every query containing a dynamic value.

Overview

Publisherwpultimatesecurity
RepositoryWordPress-Security-Skills
Skill namesql-injection-prevention
Stars
54
Forks
3
Bundled files
2
LicenseMIT
Links
  • Markdown instructions

    A SKILL.md file the model loads on demand, so it only costs tokens when a request actually matches.

  • Works with any LLM

    AI skills are plain Markdown, not provider-specific code, so this works with GPT, Claude, Gemini, Grok, or a local model.

  • 2 bundled files

    Scripts, templates, and references the model can read while it works. Files are read-only and never executed.

  • Open source

    Published by wpultimatesecurity on GitHub. Read the source before you install it.

Installation

Install the Sql Injection Prevention AI skill in TypingMind to use it with any LLM, or drop it into another agent that reads SKILL.md.

1

Install in TypingMind

TypingMind installs a skill straight from its GitHub folder — it reads SKILL.md, bundles the resource files, and stores the result locally.

  1. Open the app and go to Plugins → Skills.
  2. Choose "Install from GitHub".
  3. Paste the skill folder URL below and confirm.
    https://github.com/wpultimatesecurity/WordPress-Security-Skills/tree/dev/skills/sql-injection-prevention
  4. Enable the skill in any chat where you want it available.
Plugins → Skills → Add skill → From GitHub URL, then paste the folder URL and press Continue.
2

Install in another agent

Any agent that reads the Agent Skills format can use this skill — copy the folder into that agent's skills directory.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/wpultimatesecurity/WordPress-Security-Skills.git /tmp/WordPress-Security-Skills
mkdir -p .claude/skills
cp -r /tmp/WordPress-Security-Skills/skills/sql-injection-prevention .claude/skills/sql-injection-prevention
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable Sql Injection Prevention in any TypingMind chat and the model takes it from there. Its name and description sit in the system prompt, and the moment a request matches, the model loads the full instructions itself — you never invoke it by hand, and it costs no tokens until it is actually used.

The model loads Sql Injection Prevention on its own as soon as a request matches it.

Works with any AI model

AI skills are plain Markdown instructions rather than provider-specific code, so Sql Injection Prevention is not tied to the model it was written for. Install it once in TypingMind and use it with GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama, or a local model you run yourself — all on your own API keys.

  • Loaded only when it is needed

    The system prompt carries just the name and description. The instructions are fetched on the first matching request, so an idle skill costs nothing.

  • Switch models mid-chat

    Because the skill is instructions rather than code, changing model does not break it — the next model reads the same SKILL.md.

Skill instructions

This is the SKILL.md content the model loads. Read it before installing — a skill is instructions your model will follow.

SQL injection prevention with $wpdb

When to use this skill

Use this skill for any custom SQL that includes a dynamic value:

  • $wpdb->get_results(), get_var(), get_row(), get_col(), query().
  • WHERE, IN (...), LIKE, LIMIT, ORDER BY, table/column names.
  • Custom tables, custom meta queries, reporting/analytics queries.

Prefer the high-level APIs (WP_Query, get_posts, get_users, $wpdb->insert/update/ delete) when they fit — they parameterize for you. Drop to raw SQL only when necessary, and then always via $wpdb->prepare().

Core principles (and why they matter)

  1. Never concatenate user input into SQL. String interpolation is the root cause of injection. prepare() separates the query template from the data.
  2. Use the right placeholder. %d (integer), %f (float), %s (string), %i (identifier — table/column, WordPress 6.2+). The wrong placeholder can break quoting and reopen injection.
  3. prepare() quotes strings for you — don't add your own quotes. Writing '%s' (with surrounding quotes) double-quotes the value and breaks the query.
  4. esc_like() before prepare() for LIKE. Otherwise % and _ in user input act as wildcards (and can be abused). Escape, then pass through %s.
  5. Identifiers can't be parameterized as data. Column/table names and ORDER BY direction must be validated against an allowlist (or use %i for identifiers in 6.2+).
  6. Prefer the table prefix property: $wpdb->prefix, $wpdb->posts, etc., never a hard-coded wp_.

Step-by-step implementation

  1. Can a core API do it (WP_Query, $wpdb->insert)? If yes, use it.
  2. If raw SQL is needed, write the query with placeholders, not variables.
  3. Call $wpdb->prepare( $sql, $args... ) and run the prepared string.
  4. For LIKE, wrap the term: '%' . $wpdb->esc_like( $term ) . '%', passed via %s.
  5. For IN() lists, build a placeholder string and spread the values.
  6. For identifiers/sort columns, allowlist them (or use %i on 6.2+).

Supporting references

ReferenceLoad when
SQL injection prevention checklistBefore final verification of the sql injection prevention controls.
Secure wpdb query patternsImplementing parameterized database query flows with typed values and safe identifiers.

Common AI mistakes / anti-patterns

Mistake 1 — Interpolating input directly

php
// ❌ Insecure: classic SQL injection.
$id   = $_GET['id'];
$row  = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}orders WHERE id = $id" );
php
// ✅ Secure: prepared statement with a typed placeholder.
$id  = absint( $_GET['id'] ?? 0 );
$row = $wpdb->get_row(
    $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}orders WHERE id = %d", $id )
);

Mistake 2 — Quoting the placeholder yourself

php
// ❌ Broken/insecure: prepare already quotes %s — this double-quotes.
$wpdb->prepare( "SELECT * FROM t WHERE name = '%s'", $name );
php
// ✅ Correct: no surrounding quotes; prepare handles it.
$wpdb->prepare( "SELECT * FROM t WHERE name = %s", $name );

Mistake 3 — LIKE without esc_like()

php
// ❌ Insecure: user-supplied % / _ become wildcards.
$wpdb->prepare( "SELECT * FROM t WHERE title LIKE %s", '%' . $term . '%' );
php
// ✅ Secure: escape LIKE wildcards first, then prepare.
$like = '%' . $wpdb->esc_like( $term ) . '%';
$wpdb->prepare( "SELECT * FROM t WHERE title LIKE %s", $like );

Mistake 4 — Building an IN() list by joining input

php
// ❌ Insecure: raw join of user values into IN().
$ids = implode( ',', $_POST['ids'] );
$wpdb->query( "DELETE FROM t WHERE id IN ($ids)" );
php
// ✅ Secure: one placeholder per value, spread into prepare.
$ids          = array_map( 'absint', (array) ( $_POST['ids'] ?? array() ) );
$ids          = array_filter( $ids );
if ( $ids ) {
    $placeholders = implode( ',', array_fill( 0, count( $ids ), '%d' ) );
    $wpdb->query(
        $wpdb->prepare( "DELETE FROM {$wpdb->prefix}t WHERE id IN ($placeholders)", $ids )
    );
}

Mistake 5 — Dynamic ORDER BY / column name from input

php
// ❌ Insecure: identifiers can't be safely parameterized as %s.
$orderby = $_GET['orderby'];
$wpdb->get_results( "SELECT * FROM t ORDER BY $orderby" );
php
// ✅ Secure: allowlist the column and direction, or use %i on WP 6.2+.
$orderby = sanitize_key( $_GET['orderby'] ?? 'created' );
$order   = strtoupper( $_GET['order'] ?? 'DESC' );
$columns = array( 'created', 'name', 'total' );
$orderby = in_array( $orderby, $columns, true ) ? $orderby : 'created';
$order   = ( 'ASC' === $order ) ? 'ASC' : 'DESC';

if ( version_compare( $GLOBALS['wp_version'], '6.2', '>=' ) ) {
    $wpdb->get_results(
        $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}t ORDER BY %i $order", $orderby )
    );
} else {
    $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}t ORDER BY {$orderby} {$order}" );
}

Mistake 6 — Hand-rolled insert instead of $wpdb->insert()

php
// ❌ Insecure: manual INSERT with interpolation.
$wpdb->query( "INSERT INTO t (name) VALUES ('{$_POST['name']}')" );
php
// ✅ Secure: $wpdb->insert() with format specifiers handles escaping.
$wpdb->insert(
    $wpdb->prefix . 't',
    array( 'name' => sanitize_text_field( wp_unslash( $_POST['name'] ?? '' ) ) ),
    array( '%s' )
);

Correct code examples

Full prepared-query patterns — single value, IN(), LIKE, insert/update/delete, and allowlisted ORDER BY — are in references/secure-wpdb-queries.php.

Checklist

  • No user input is concatenated/interpolated into SQL.
  • Every dynamic value goes through $wpdb->prepare() with the right placeholder.
  • %d/%f/%s chosen by type; placeholders are not wrapped in extra quotes.
  • LIKE terms are wrapped with $wpdb->esc_like() then passed via %s.
  • IN() lists use one placeholder per value, spread into prepare().
  • Identifiers / ORDER BY columns are allowlisted (or use %i on WP 6.2+).
  • Table names use $wpdb->prefix / $wpdb->posts, not hard-coded wp_.
  • insert/update/delete use $wpdb helpers with format specifiers where possible.

Official references

Bundled files

The model reads these on demand while the skill is loaded. They are exposed as readable files and are never executed.

Frequently asked questions

What does the Sql Injection Prevention AI skill do?

Use when writing any custom database query in WordPress with $wpdb — get_results, get_var, get_row, query, or building WHERE/IN/LIKE/ORDER BY clauses. Uses $wpdb->prepare() with correct placeholders (%d, %s, %f, %i), $wpdb->esc_like() for LIKE, and allowlists for identifiers that cannot be parameterized. Prevents SQL injection. Apply proactively to every query containing a dynamic value.

Why use Sql Injection Prevention on TypingMind?

Because you install it once and use it with any model. Sql Injection Prevention is plain Markdown rather than provider-specific code, so the same skill runs on GPT-5, Claude, Gemini, Grok, or a local model — and you can switch model mid-chat without it breaking. TypingMind runs on your own API keys, so you pay providers directly instead of a per-seat subscription, and your skills and chats stay in your own storage.

How do I install Sql Injection Prevention in TypingMind?

Open Plugins → Skills → Install from GitHub in TypingMind and paste https://github.com/wpultimatesecurity/WordPress-Security-Skills/tree/dev/skills/sql-injection-prevention. TypingMind reads its SKILL.md and bundles its files and installs it as a skill you can enable per chat.

Which AI models can use Sql Injection Prevention?

Any model you connect in TypingMind. AI skills are plain Markdown instructions rather than provider-specific code, so GPT, Claude, Gemini, Grok, and local models can all load this skill when a request matches it.

How many AI models can I use with Sql Injection Prevention?

As many as you like. As long as a model supports skills, you can use Sql Injection Prevention with it — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama and more — all on TypingMind with your own API keys.

Is the Sql Injection Prevention AI skill free?

Yes. It is published on GitHub by wpultimatesecurity under the MIT license. You only pay your own AI provider for the tokens you use.

What are AI skills?

An AI skill is a reusable instruction bundle that teaches an AI model how to do one specific task. It follows the open Agent Skills format: a SKILL.md file with a name and description, plus any scripts, templates or reference files the model may need. The model reads the instructions only when your request matches the skill, so an installed skill costs nothing until it is used.

How are AI skills different from plugins or MCP servers?

A plugin or MCP server gives a model new tools to call — code that runs somewhere and returns a result. An AI skill gives the model knowledge and process instead: how to approach a task, which steps to follow, what good output looks like. Skills are plain Markdown, so they need no server, no API key and no runtime, and they work with any model.

View all

Set up your own AI workspace now

Get notified about new features and future giveaways by subscribing to our newsletter 👇