wppaste
WordPress

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

Since
3.6.0
Source
wp-includes/formatting.php:5804

Strips backslashes added by WordPress's magic-quotes emulation from a string or array, preserving the original type. Use it before handing $_GET, $_POST, or $_REQUEST values to APIs such as update_post_meta() or wp_insert_post() that document their input as unslashed. It is a thin wrapper around stripslashes_deep(), so nested arrays are unslashed recursively without extra code.

Removes slashes from a string or recursively removes slashes from strings within an array.

Description

This should be used to remove slashes from data passed to core API that expects data to be unslashed.

Compatibility

WordPress
since 3.6.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$valuestring|array
String or array of data to unslash.

Return value

string|array
Unslashed $value, in the same type as supplied.

Code examples

Every example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.

Unslash a submitted value before saving it as post meta

A form field value arrives through $_POST with WordPress's added slashes still in it, so it needs cleaning before it is stored.

$_POST['price'] = "O\\'Brien's Widget - $19.99";

$clean_price = wp_unslash( $_POST['price'] );
update_post_meta( 2, 'price', $clean_price );

echo esc_html( get_post_meta( 2, 'price', true ) );

get_post_meta() does not add slashes back, so the stored value stays clean until it is echoed into HTML with proper escaping.

Recursively unslash an array of submitted fields

A settings-style array with nested string values, mimicking what a bulk form submission might look like, needs every level unslashed at once.

$submitted = array(
	'title' => "Editor\\'s Pick",
	'tags'  => array( "Today\\'s News", 'Breaking \\"Story\\"' ),
);

$clean = wp_unslash( $submitted );
print_r( $clean );

Common problems and fixes · 3

Why do I still see backslashes after calling wp_unslash on my data?

wp_unslash() calls stripslashes_deep(), which removes exactly one layer of slashes, the layer WordPress itself adds to $_GET, $_POST, $_COOKIE, and $_REQUEST to emulate magic quotes. If a string was escaped more than once before it reached your code, one call only removes one layer.

Should I unslash $_POST data before passing it to update_post_meta or wp_insert_post?

Core superglobals are slashed by WordPress, but functions like update_post_meta() and wp_insert_post() document their arguments as expecting unslashed input. Passing raw $_POST straight in leaves stray backslashes in saved content.

Why does wp_unslash return an array when I only passed in one value?

The function's return type matches whatever was passed in: give it a string and it returns a string, give it an array and it recurses through every element and returns an array of the same shape. Passing a nested array like $_POST directly returns a nested array, not a flat value.

Alternatives and related functions

wp_slash
When you need to add slashes back to a value before writing it somewhere that expects the WordPress-slashed format, the reverse operation.
stripslashes_deep
When you want the underlying recursive strip behavior directly without going through the wp_unslash() wrapper, though wp_unslash() is the documented entry point for this task.
sanitize_text_field
When the value also needs trimming, tag stripping, and whitespace normalization, not just backslash removal.

Performance profile

How much work a call to wp_unslash() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
5

Executed per call on PHP 8.5. The body compiles to 5.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
50

50 places in core call this, so the cost is paid more often than your own code shows.

What one call costs · 1 distinct outcome

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_unslash() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always5stripslashes_deep()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 5 instructions, 5 executed per call, 0 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 1

  • stripslashes_deep()Navigates through an array, object, or scalar, and removes slashes from the values.

Used by · 50

Show all 50

Source code

function wp_unslash( $value ) {	return stripslashes_deep( $value );}

Changelog

Introduced in 3.6.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.0.4 tag, from src/wp-includes/formatting.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.