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.000001 sec (speed of light) | 50000 times — 0.02 sec (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 data 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
)
*/

Changelog

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

Code of wp_slash() WP 5.7.2

<?php
function wp_slash( $value ) {
	if ( is_array( $value ) ) {
		$value = array_map( 'wp_slash', $value );
	}

	if ( is_string( $value ) ) {
		return addslashes( $value );
	}

	return $value;
}

From tag: Supporting (helper utility auxiliary)

More from category: Helper Functions

vladlu 100vlad.lu
Editors: Kama 100
No comments
    Log In