wp_unslash()WPSCache 3.6.0

Removes slashes from a string or from strings of an array, or properties of an object. The array can be of any nesting level.

For example, this function should be used when adding $_POST data to the database using $wpdb->insert() because $wpdb->insert() expects unslashed data, whereas the data in $_POST is always slashed in WP.

This function is used in API functions such as update_post_meta(), wp_update_post(), wp_insert_post(), etc. All these functions expect passed data to be slashed. The exception is the options functions: update_option(), add_option() etc.

Use this function instead of stripslashes_deep().

If, on the contrary, you need to add slashes, use wp_slash().

1 time — 0.000027 sec (very fast) | 50000 times — 0.21 sec (very fast) | PHP 7.0.5, WP 4.5.2

No Hooks.

Returns

String. Possibly unslashed $value.

Usage

wp_unslash( $value );
$value(string) (required)
String of data to unslash.

Examples

0

#1 Remove slashes from a string

$val = 'Jerome D\'ambrosio - unknown driver.';
$val = wp_unslash( $val );
echo $val;

// returns:
// Jerome D'ambrosio - unknown driver.
0

#2 Remove slashes from array elements

This function can be used instead of stripslashes_deep(). Since this is a recursive function, it will remove slashes from all nested arrays.

$arr = array(
	"Did you read \'the man in the case\'?",
	array( 
		"\'the man in the case\' very interesting story"
	)
);

$arr = wp_unslash( $arr );
/*
 return: 
 array(
	"Did you read 'the man in the case'?",
	array( 
		"'the man in the case' very interesting story"
	)
 );
*/

wp_unslash() code WPSCache 3.1.0

function wp_unslash( $value ) {
	if ( function_exists( '\\wp_unslash' ) ) {
		return \wp_unslash( $value );
	} elseif ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
		return stripslashes( $value );
	} else {
		return $value;
	}
}