add_magic_quotes()WP 0.71

Escape ', ", \ symbols in the elements of the array. Works with multidimensional arrays too.

This is a recursive function that works based on the addslashes() PHP function. Walks the array and escapes the values of its elements.

This is almost a copy of the wp_slash() function. The difference is that wp_slash() can process strings not only arrays.

No Hooks.

Return

Array. Sanitized $input_array.

Usage

add_magic_quotes( $input_array );
$input_array(array) (required)
Array to walk while sanitizing contents.

Examples

0

#1 Function operation example:

$array = array(
	"single quote - '",
	array('a nested array, with double quotes - "'),
	'slash - \ text'
);

$new_array = add_magic_quotes( $array );

print_r( $new_array );

Get this result:

Array
(
	[0] => single quote - \'
	[1] => Array
		(
			[0] => a nested array, with double quotes - \"
		)

	[2] => slash - \ text
)

Changelog

Since 0.71 Introduced.
Since 5.5.0 Non-string values are left untouched.

add_magic_quotes() code WP 6.4.3

function add_magic_quotes( $input_array ) {
	foreach ( (array) $input_array as $k => $v ) {
		if ( is_array( $v ) ) {
			$input_array[ $k ] = add_magic_quotes( $v );
		} elseif ( is_string( $v ) ) {
			$input_array[ $k ] = addslashes( $v );
		}
	}

	return $input_array;
}