wp_slash() wordpress function and usage

Home / WordPress / WordPress Wiki / wp_slash() wordpress function and usage

wp_slash( string|array $value )

This function is used to add 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.

Parameters

$value

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

Return

(string|array) Slashed $value.

Usage sample

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' );

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' );

 

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments