Bird
Raised Fist0
Wordpressframework~5 mins

SQL injection prevention in Wordpress - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is SQL injection?
SQL injection is a security attack where harmful SQL code is inserted into a query to manipulate or access the database in unauthorized ways.
Click to reveal answer
beginner
How does WordPress help prevent SQL injection?
WordPress uses prepared statements and the $wpdb class to safely handle database queries, which helps prevent SQL injection by separating code from data.
Click to reveal answer
intermediate
What is a prepared statement in WordPress?
A prepared statement is a way to write SQL queries where placeholders are used for data. WordPress replaces these placeholders safely to avoid SQL injection.
Click to reveal answer
beginner
Why should you never directly insert user input into SQL queries?
Directly inserting user input can allow attackers to add harmful SQL code, leading to data leaks or damage. Always sanitize or use prepared statements.
Click to reveal answer
beginner
Which WordPress function helps safely prepare SQL queries?
The $wpdb->prepare() function helps safely prepare SQL queries by escaping user input and using placeholders.
Click to reveal answer
Which WordPress class is commonly used to interact with the database safely?
A$db_query
B$wpdb_safe
C$sql_inject
D$wpdb
What does $wpdb->prepare() do?
APrepares SQL queries safely with placeholders
BExecutes SQL queries directly
CDeletes unsafe data from the database
DEncrypts the database
Why is it unsafe to insert user input directly into SQL queries?
AIt slows down the query
BIt uses more memory
CIt can cause SQL injection attacks
DIt makes the code longer
Which of these is a good practice to prevent SQL injection in WordPress?
AUse raw SQL queries with string concatenation
BUse prepared statements with $wpdb->prepare()
CDisable the database
DIgnore user input validation
What is a placeholder in a prepared statement?
AA symbol in SQL query replaced safely with user data
BA variable that stores user input
CA type of database table
DA WordPress plugin
Explain how WordPress prevents SQL injection using $wpdb and prepared statements.
Think about how WordPress separates code from data in queries.
You got /4 concepts.
    Describe why directly inserting user input into SQL queries is dangerous and how to avoid it in WordPress.
    Focus on the risks and the safe coding practice.
    You got /4 concepts.

      Practice

      (1/5)
      1. What is the main purpose of using $wpdb->prepare() in WordPress?
      easy
      A. To speed up database queries by caching results
      B. To safely insert user input into SQL queries and prevent SQL injection
      C. To format dates and times in SQL queries
      D. To automatically backup the database before running queries

      Solution

      1. Step 1: Understand the role of $wpdb->prepare()

        $wpdb->prepare() is designed to safely insert user data into SQL queries by escaping dangerous characters.
      2. Step 2: Identify the security benefit

        This prevents attackers from injecting harmful SQL commands that could damage or expose the database.
      3. Final Answer:

        To safely insert user input into SQL queries and prevent SQL injection -> Option B
      4. Quick Check:

        SQL injection prevention = B [OK]
      Hint: Remember: prepare() protects queries from harmful input [OK]
      Common Mistakes:
      • Thinking prepare() speeds up queries
      • Confusing prepare() with formatting dates
      • Assuming prepare() backs up the database
      2. Which of the following is the correct syntax to prepare a SQL query with one string placeholder in WordPress?
      easy
      A. $wpdb->prepare('SELECT * FROM wp_users WHERE user_login = %s', $username);
      B. $wpdb->prepare('SELECT * FROM wp_users WHERE user_login = ?', $username);
      C. $wpdb->prepare('SELECT * FROM wp_users WHERE user_login = :username', $username);
      D. $wpdb->prepare('SELECT * FROM wp_users WHERE user_login = $username');

      Solution

      1. Step 1: Identify correct placeholder syntax

        WordPress uses %s for string placeholders in $wpdb->prepare(), not ? or named placeholders.
      2. Step 2: Match the placeholder with the variable

        The variable $username is passed as the second argument to safely replace %s.
      3. Final Answer:

        $wpdb->prepare('SELECT * FROM wp_users WHERE user_login = %s', $username); -> Option A
      4. Quick Check:

        Use %s for strings in prepare() = A [OK]
      Hint: Use %s for strings, %d for numbers in prepare() [OK]
      Common Mistakes:
      • Using ? instead of %s as placeholder
      • Using named placeholders like :username
      • Not passing variables as arguments to prepare()
      3. What will be the output of the following code snippet?
      $user_id = 5;
      $query = $wpdb->prepare('SELECT * FROM wp_users WHERE ID = %d', $user_id);
      echo $query;
      medium
      A. SELECT * FROM wp_users WHERE ID = 5
      B. SELECT * FROM wp_users WHERE ID = %d
      C. SELECT * FROM wp_users WHERE ID = '5'
      D. Error: Missing argument for prepare()

      Solution

      1. Step 1: Understand %d placeholder usage

        %d is used for integers and will be replaced by the integer value without quotes.
      2. Step 2: Check the variable substitution

        $user_id = 5 replaces %d, so the query becomes SELECT * FROM wp_users WHERE ID = 5.
      3. Final Answer:

        SELECT * FROM wp_users WHERE ID = 5 -> Option A
      4. Quick Check:

        Integer placeholder %d replaced by 5 = A [OK]
      Hint: Integer placeholders %d insert numbers without quotes [OK]
      Common Mistakes:
      • Expecting %d to be replaced with quoted '5'
      • Thinking prepare() returns an error without extra arguments
      • Confusing %d with %s for strings
      4. Identify the error in this WordPress SQL preparation code:
      $email = $_POST['email'];
      $query = $wpdb->prepare('SELECT * FROM wp_users WHERE user_email = %d', $email);
      medium
      A. Missing the global $wpdb declaration
      B. Not sanitizing the $_POST input before prepare()
      C. Using %d placeholder for an email string instead of %s
      D. prepare() cannot be used with SELECT queries

      Solution

      1. Step 1: Check placeholder type vs variable type

        %d is for integers, but $email is a string, so %s should be used.
      2. Step 2: Understand prepare() placeholder matching

        Using wrong placeholder causes SQL errors or unexpected behavior.
      3. Final Answer:

        Using %d placeholder for an email string instead of %s -> Option C
      4. Quick Check:

        Placeholder type must match variable type = C [OK]
      Hint: Match %s for strings, %d for numbers in prepare() [OK]
      Common Mistakes:
      • Using %d for strings like emails
      • Assuming prepare() sanitizes input fully
      • Thinking prepare() can't be used with SELECT
      5. You want to safely query posts by author ID and post status using $wpdb->prepare(). Which of the following is the best way to write this query?
      hard
      A. $wpdb->prepare('SELECT * FROM wp_posts WHERE post_author = ? AND post_status = ?', $author_id, $status);
      B. $wpdb->prepare('SELECT * FROM wp_posts WHERE post_author = %s AND post_status = %d', $author_id, $status);
      C. $wpdb->prepare('SELECT * FROM wp_posts WHERE post_author = $author_id AND post_status = '$status'');
      D. $wpdb->prepare('SELECT * FROM wp_posts WHERE post_author = %d AND post_status = %s', $author_id, $status);

      Solution

      1. Step 1: Match placeholders to variable types

        $author_id is a number, so use %d; $status is a string, so use %s.
      2. Step 2: Verify correct usage of prepare()

        $wpdb->prepare('SELECT * FROM wp_posts WHERE post_author = %d AND post_status = %s', $author_id, $status); correctly uses %d and %s placeholders with variables passed as arguments.
      3. Step 3: Identify errors in other options

        Using ? placeholders is unsupported; using %s for post_author (integer) and %d for post_status (string) mismatches types; directly inserting variables into the query string is unsafe.
      4. Final Answer:

        $wpdb->prepare('SELECT * FROM wp_posts WHERE post_author = %d AND post_status = %s', $author_id, $status); -> Option D
      5. Quick Check:

        %d for author_id, %s for status = D [OK]
      Hint: Use %d for numbers, %s for strings in prepare() [OK]
      Common Mistakes:
      • Swapping %d and %s placeholders
      • Using ? instead of % placeholders
      • Injecting variables directly into query string
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard