wp_slash( string|array $value )

Add slashes to a string or array of strings.


Description 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.


Parameters Parameters

$value

(string|array) (Required) String or array of strings to slash.


Top ↑

Return Return

(string|array) Slashed $value


Top ↑

Source Source

File: wp-includes/formatting.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;
}

Top ↑

Changelog Changelog

Changelog
Version Description
3.6.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 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.