Which of the following best describes why using raw SQL queries with user input in WordPress is risky?
Think about what happens if a user types something unexpected into a search box.
Raw SQL queries with user input can be manipulated to run harmful commands if the input is not handled safely. This is called SQL injection.
What does the $wpdb->prepare() function do when used with user input in WordPress?
Think about how placeholders work in SQL queries.
The $wpdb->prepare() function replaces placeholders with safely escaped user input, preventing attackers from injecting harmful SQL.
Which of the following code snippets correctly uses $wpdb->prepare() to safely query posts by title?
Remember the placeholder types: %s for strings, %d for numbers.
Option C correctly uses %s as a placeholder for a string and passes the variable separately. Option C is unsafe because it injects the variable directly. Option C uses an invalid placeholder '?'. Option C uses %d which is for integers, not strings.
Given the following WordPress code, what is the main security issue?
$title = $_GET['title']; $query = "SELECT * FROM wp_posts WHERE post_title = '$title'"; $results = $wpdb->get_results($query);
$title = $_GET['title']; $query = "SELECT * FROM wp_posts WHERE post_title = '$title'"; $results = $wpdb->get_results($query);
Look at how the variable $title is used inside the query string.
Directly inserting user input into SQL queries without escaping or preparing allows attackers to inject malicious SQL code.
What will be the output of the following WordPress code if the user inputs "anything' OR '1'='1" as $title?
$title = "anything' OR '1'='1";
$query = $wpdb->prepare("SELECT * FROM wp_posts WHERE post_title = %s", $title);
echo $query;$title = "anything' OR '1'='1"; $query = $wpdb->prepare("SELECT * FROM wp_posts WHERE post_title = %s", $title); echo $query;
Think about how $wpdb->prepare() escapes quotes inside strings.
The $wpdb->prepare() function escapes single quotes inside the input by adding backslashes, so the malicious input is treated as a string literal, not SQL code.