The add_magic_quotes() WordPress PHP function sanitizes an array’s content by adding slashes before predefined characters.
Usage
To use add_magic_quotes(), you simply pass an array to it. This function then returns the sanitized array.
$input_array = array("Hello", "It's me", "Hello from the 'other' side"); $sanitized_array = add_magic_quotes($input_array);
Parameters
- $input_array (array) – Required parameter, an array to walk while sanitizing contents.
More information
See WordPress Developer Resources: add_magic_quotes
This function is part of the WordPress core and has not been deprecated as of the latest update. It’s source code can be found in the wp-includes/formatting.php
file.
Examples
Basic Usage
This example demonstrates a basic use case for the add_magic_quotes() function.
$input_array = array("Hello", "It's me", "Hello from the 'other' side"); $sanitized_array = add_magic_quotes($input_array); print_r($sanitized_array); // This will output: Array ( [0] => Hello [1] => It\'s me [2] => Hello from the \'other\' side )
Nested Arrays
This example demonstrates how add_magic_quotes() handles nested arrays.
$input_array = array("Hello", array("It's me", "Hello from the 'other' side")); $sanitized_array = add_magic_quotes($input_array); print_r($sanitized_array); // This will output: Array ( [0] => Hello [1] => Array ( [0] => It\'s me [1] => Hello from the \'other\' side ) )
Non-String Values
This example shows how add_magic_quotes() handles non-string values.
$input_array = array("Hello", 123, true); $sanitized_array = add_magic_quotes($input_array); print_r($sanitized_array); // This will output: Array ( [0] => Hello [1] => 123 [2] => 1 )
Array with Keys
This example shows how add_magic_quotes() handles arrays with keys.
$input_array = array("greeting" => "Hello", "message" => "It's me"); $sanitized_array = add_magic_quotes($input_array); print_r($sanitized_array); // This will output: Array ( [greeting] => Hello [message] => It\'s me )
Multidimensional Array
This example demonstrates how add_magic_quotes() handles multidimensional arrays.
$input_array = array("greeting" => "Hello", "messages" => array("It's me", "Hello from the 'other' side")); $sanitized_array = add_magic_quotes($input_array); print_r($sanitized_array); // This will output: Array ( [greeting] => Hello [messages] => Array ( [0] => It\'s me [1] => Hello from the \'other\' side ) )