wp_unslash( string|array $value ): string|array
- Since
- 3.6.0
- Source
wp-includes/formatting.php:5778
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.
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?
- Should I unslash $_POST data before passing it to update_post_meta or wp_insert_post?
- Why does wp_unslash return an array when I only passed in one value?
Why do I still see backslashes after calling wp_unslash on my data?
Should I unslash $_POST data before passing it to update_post_meta or wp_insert_post?
Why does wp_unslash return an array when I only passed in one 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
- Scaling
- Constant
- Instructions
- 5
- Plugin surface
- None
- Called by
- 50
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5. The body compiles to 5.
Nothing here hands control to plugin code.
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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 5 | stripslashes_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
- Plugin_Installer_Skin::after()Performs an action following a plugin install.
- WP::send_headers()Sends additional HTTP headers for caching, content type, etc.
- WP_Automatic_Updater::has_fatal_error()Performs a loopback request to check for potential fatal errors.
- WP_Customize_Manager::__construct()Constructor.
- WP_Customize_Manager::customize_preview_settings()Prints JavaScript settings for preview frame.
- WP_Customize_Manager::doing_ajax()Returns true if it's an Ajax request.
- WP_Customize_Manager::handle_load_themes_request()Loads themes into the theme browsing/installation UI.
- WP_Customize_Manager::save()Handles customize_save WP Ajax request to save/update a changeset.
- WP_Customize_Manager::unsanitized_post_values()Gets dirty pre-sanitized setting values in the current customized state.
- WP_Customize_Nav_Menu_Item_Setting::sanitize()Sanitize an input.
- WP_Customize_Nav_Menus::ajax_insert_auto_draft_post()Ajax handler for adding a new auto-draft post.
- WP_Customize_Nav_Menus::ajax_load_available_items()Ajax handler for loading available menu items.
Show all 50
- WP_Customize_Nav_Menus::ajax_search_available_items()Ajax handler for searching available menu items.
- WP_Customize_Selective_Refresh::handle_render_partials_request()Handles the Ajax request to return the rendered partials for the requested placements.
- WP_Customize_Widgets::get_post_value()Retrieves an unslashed post value or return a default.
- WP_Customize_Widgets::register_settings()Inspects the incoming customized data for any widget settings, and dynamically adds them up-front so widgets will be initialized properly.
- WP_MS_Sites_List_Table::get_views()Gets links to filter sites by status.
- WP_MS_Sites_List_Table::prepare_items()Prepares the list of sites for display.
- WP_MS_Sites_List_Table::site_states()Determines whether to output comma-separated site states.
- WP_MS_Themes_List_Table::_search_callback()
- WP_MS_Users_List_Table::column_username()Handles the username column output.
- WP_MS_Users_List_Table::handle_row_actions()Generates and displays row action links.
- WP_MS_Users_List_Table::prepare_items()
- WP_Plugin_Dependencies::check_plugin_dependencies_during_ajax()Checks plugin dependencies after a plugin is installed via AJAX.
- WP_Plugin_Install_List_Table::prepare_items()
- WP_Plugins_List_Table::__construct()Constructor.
- WP_Plugins_List_Table::no_items()
- WP_Privacy_Requests_Table::process_bulk_action()Process bulk actions.
- WP_REST_Server::serve_request()Handles serving a REST API request.
- WP_Session_Tokens::create()Generates a session token and attaches session information to it.
- WP_Site_Health::can_perform_loopback()Runs a loopback test on the site.
- WP_Site_Health::check_for_page_caching()Checks if site has page cache enabled or not.
- WP_Site_Health::get_test_rest_availability()Tests if the REST API is accessible.
- WP_Terms_List_Table::column_name()
- WP_Terms_List_Table::handle_row_actions()Generates and displays row action links.
- WP_Terms_List_Table::prepare_items()
- WP_Theme_Install_List_Table::prepare_items()
- WP_Themes_List_Table::_js_vars()Send required variables to JavaScript land
- WP_Themes_List_Table::prepare_items()
- WP_User_Search::__construct()PHP5 Constructor - Sets up the object properties.
- WP_Users_List_Table::prepare_items()Prepares the users list for display.
- WP_Users_List_Table::single_row()Generates HTML for a single row on the users.php admin panel.
- _admin_search_query()Displays the search query.
- _wp_customize_include()Includes and instantiates the WP_Customize_Manager class.
- _wp_filter_post_meta_footnotes()Strips all HTML from the content of footnotes, and sanitizes the ID.
- _wp_link_page()Helper function for wp_link_pages().
- _wp_personal_data_handle_actions()Handle list table actions.
- add_meta()Adds post meta data defined in the `$_POST` superglobal for a post with given ID.
- add_metadata()Adds metadata for the specified object.
- check_comment()Checks whether a comment passes internal checks to be allowed to add.
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.7.7 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.