wp_slash( string|array $value ): string|array

Adds slashes to a string or recursively adds slashes to strings within an array.


Description

This should be used when preparing data for core API that expects slashed data.
This should not be used to escape data going directly into an SQL query.


Top ↑

Parameters

$value string|array Required
String or array of data to slash.

Top ↑

Return

string|array Slashed $value.


Top ↑

Source

File: wp-includes/formatting.php. View all references

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

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

	return $value;
}


Top ↑

Changelog

Changelog
Version Description
5.5.0 Non-string values are left untouched.
3.6.0 Introduced.

Top ↑

User Contributed Notes

  1. Skip to note 1 content
    Contributed by Codex

    Usage with a string

    How to use wp_slash with a string within your plugin.

    function wpdocs_toolset_string_add_slashes() {
        $name = __( "O'Reilly & Associates", 'textdomain' );
        $name = wp_slash( $name );
        echo "name={$name}";
    }
    add_action( 'pre_get_posts', 'wpdocs_toolset_string_add_slashes' );
  2. Skip to note 2 content
    Contributed by Codex

    Usage with an array

    How to use wp_slash with an array within your plugin.

    function wpdocs_toolset_array_add_slashes() {
        $names = array( __( "Baba O'Reilly", 'textdomain' ), __( 'class of '99', 'textdomain' ) );
        $names = wp_slash( $names );
        print_r( $names );
    }
    add_action( 'pre_get_posts', 'wpdocs_toolset_array_add_slashes' );

You must log in before being able to contribute a note or feedback.