WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/wp slash

Description

Add slashes to a string or array of strings.

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

$value
(array or string) (required) String or array of strings to slash.
Default: None

Return

(array or string) 
the slashed $value.

Usage

 <?php  $value wp_slash($value);  ?> 

Example

How to use wp_slash with a string within your plugin.

add_action('pre_get_posts', 'toolset_string_add_slashes');

function toolset_string_add_slashes(){

    $name = "O'Reilly & Associates";

    $name = wp_slash($name);

    echo "name=$name";
}

How to use wp_slash with an array within your plugin.

add_action('pre_get_posts', 'toolset_array_add_slashes');

function toolset_array_add_slashes(){

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

    $names = wp_slash($names);

    print_r($names);
}

Change Log

Source File

wp_slash() is located in wp-includes/formatting.php.

Related

Slashing Functions: wp_slash(), wp_unslash(), stripslashes_deep(), addslashes_gpc()

This article is marked as in need of editing. You can help Codex by editing it.