WordPress at a glance

wp_slash() WP 3.6.0

Add slashes (\) to a string or array of strings.

This should be used when preparing data for WP core API functions that expects slashed data: update_post_meta(), wp_insert_post()

This should NOT be used to escape data passing directly into an SQL query.

Contrary, to remove slashes, use wp_unslash() function.

✈ 1 time = 0.000001s = speed of light | 50000 times = 0.02s = speed of light | PHP 7.2.16, WP 5.1.1

No Hooks.

Return

String/Array. Slashed $value

Usage

wp_slash( $value );
$value(string/array) (required)
String or array of strings to slash.

Examples

#1 Basic example (with a string)

$name = "O'Reilly & Associates";

$name = wp_slash($name);

echo $name;

// output: O\'Reilly & Associates

#2 Basic example (with an array)

$names = array( "Baba O'Reilly", "class of '99" );

$names = wp_slash( $names );

print_r( $names );

/*
output:
Array
(
	[0] => Baba O\'Reilly
	[1] => class of \'99
)
*/

Code of wp slash: wp-includes/formatting.php VER 5.1.1

<?php
function wp_slash( $value ) {
	if ( is_array( $value ) ) {
		foreach ( $value as $k => $v ) {
			if ( is_array( $v ) ) {
				$value[ $k ] = wp_slash( $v );
			} else {
				$value[ $k ] = addslashes( $v );
			}
		}
	} else {
		$value = addslashes( $value );
	}

	return $value;
}

Related Functions

From tag: Supporting (helper utility auxiliary)

More from category: Helper Functions

No comments
    Hello, !     Log In . Register