wp_slash( string|array $value ): string|array
- Since
- 3.6.0, 5.5.0
- Source
wp-includes/formatting.php:5864
Escapes a string, or every string nested inside an array, using addslashes() so the value matches the slashed format WordPress core APIs expect. It mirrors the legacy magic-quotes style escaping WordPress still applies to $_POST and $_GET, which is why functions like wp_insert_post() call wp_unslash() internally before using the data. It is not a SQL-safe escape, so quoting for a database query still needs $wpdb->prepare() or esc_sql().
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.
Compatibility
- WordPress
- since 5.5.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 slash.
Return value
string|array- Slashed
$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.
Slash a post title before passing it to wp_update_post
wp_update_post() runs wp_unslash() on the array you give it, so a title containing quotes needs to be slashed first or the quotes can be mangled.
$post_id = 2;
$new_title = 'Editor\'s "top picks" for the week';
$slashed_title = wp_slash( $new_title );
wp_update_post(
array(
'ID' => $post_id,
'post_title' => $slashed_title,
)
);
$updated = get_post( $post_id );
echo esc_html( $updated->post_title );get_post() returns the unslashed value for display, so the echoed title shows the original quotes and apostrophe intact.
Slash an entire post array before wp_insert_post
wp_slash() walks every element of an array, which is handy when several fields in a new post array contain quotes.
$postarr = array(
'post_title' => 'Reader\'s Q&A: "What is caching?"',
'post_content' => 'A short answer covering the "basics" of caching.',
'post_status' => 'draft',
);
$slashed_postarr = wp_slash( $postarr );
$post_id = wp_insert_post( $slashed_postarr );
$post = get_post( $post_id );
echo esc_html( $post->post_title ) . '<br>';
echo esc_html( $post->post_content );Common problems and fixes · 3
- Why do apostrophes turn into double backslashes after I save a post?
- Can I use wp_slash to make a value safe for a SQL query?
- Why does wp_slash leave my integers or booleans unchanged?
Why do apostrophes turn into double backslashes after I save a post?
Can I use wp_slash to make a value safe for a SQL query?
Why does wp_slash leave my integers or booleans unchanged?
Alternatives and related functions
wp_unslash- When you need to reverse the effect and strip the slashes back off before displaying or comparing a value.
wp_slash_strings_only- When the input is already known to be a plain string or a flat array and you don't want array_map recursing into nested structures.
wpdb::prepare- When the value is going into a raw SQL query rather than into a core API that expects slashed data.
stripslashes_deep- When you're cleaning up a whole array or object that came from a source (like superglobals) already slashed by WordPress.
Performance profile
How much work a call to wp_slash() 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
- 6–9
- 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, depending on the branch taken. The body compiles to 15.
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 · 3 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_slash() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!is_array($value) && !is_string($value) | 6 | none |
is_array($value) | 8 | array_map() |
!is_array($value) && is_string($value) | 9 | addslashes() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 15 instructions, 6–9 executed per call, 2 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.
Used by · 50
- WP_Customize_Manager::save_changeset_post()Saves the post for the loaded changeset.
- WP_Customize_Nav_Menu_Item_Setting::sanitize()Sanitize an input.
- WP_Customize_Nav_Menu_Item_Setting::update()Creates/updates the nav_menu_item post for this setting.
- WP_Customize_Nav_Menu_Setting::update()Create/update the nav_menu term for this setting.
- WP_Customize_Nav_Menus::insert_auto_draft_post()Adds a new `auto-draft` post.
- WP_Customize_Nav_Menus::save_nav_menus_created_posts()Publishes the auto-draft posts that were created for nav menu items.
- WP_Customize_Widgets::call_widget_update()Finds and invokes the widget update and control callbacks.
- WP_Embed::shortcode()The do_shortcode() callback function.
- WP_Query::get_posts()Retrieves an array of posts based on query variables.
- WP_REST_Application_Passwords_Controller::create_item()Creates an application password.
- WP_REST_Application_Passwords_Controller::update_item()Updates an application password.
- WP_REST_Attachments_Controller::edit_media_item()Applies edits to a media item and creates a new attachment record.
Show all 50
- WP_REST_Attachments_Controller::finalize_item()Finalizes an attachment after client-side media processing.
- WP_REST_Attachments_Controller::insert_attachment()Inserts the attachment post in the database. Does not update the attachment meta.
- WP_REST_Attachments_Controller::sideload_item()Side-loads a media file without creating a new attachment.
- WP_REST_Autosaves_Controller::create_item()Creates, updates or deletes an autosave revision.
- WP_REST_Autosaves_Controller::create_post_autosave()Creates autosave for the specified post.
- WP_REST_Comments_Controller::create_item()Creates a comment.
- WP_REST_Comments_Controller::update_item()Updates a comment.
- WP_REST_Menu_Items_Controller::create_item()Creates a single nav menu item.
- WP_REST_Menu_Items_Controller::update_item()Updates a single nav menu item.
- WP_REST_Menus_Controller::create_item()Creates a single term in a taxonomy.
- WP_REST_Menus_Controller::update_item()Updates a single term from a taxonomy.
- WP_REST_Meta_Fields::delete_meta_value()Deletes a meta value for an object.
- WP_REST_Meta_Fields::update_meta_value()Updates a meta value for an object.
- WP_REST_Meta_Fields::update_multi_meta_value()Updates multiple meta values for an object.
- WP_REST_Posts_Controller::create_item()Creates a single post.
- WP_REST_Posts_Controller::update_item()Updates a single post.
- WP_REST_Templates_Controller::create_item()Creates a single template.
- WP_REST_Templates_Controller::update_item()Updates a single template.
- WP_REST_Terms_Controller::create_item()Creates a single term in a taxonomy.
- WP_REST_Terms_Controller::update_item()Updates a single term from a taxonomy.
- WP_REST_Users_Controller::create_item()Creates a single user.
- WP_REST_Users_Controller::update_item()Updates a single user.
- WP_REST_Widgets_Controller::save_widget()Saves the widget in the request object.
- _wp_copy_post_meta()Copy post meta for the given key from one post to another.
- _wp_expand_nav_menu_post_data()If a JSON blob of navigation menu data is in POST data, expand it and inject it into `$_POST` to avoid PHP `max_input_vars` limitations. See #14134.
- _wp_filter_post_meta_footnotes()Strips all HTML from the content of footnotes, and sanitizes the ID.
- _wp_kses_sanitize_note_mention_classes()Reduces `span` classes in comment content to the note mention tokens.
- _wp_put_post_revision()Inserts post data into the posts table as a post revision.
- add_meta()Adds post meta data defined in the `$_POST` superglobal for a post with given ID.
- addslashes_gpc()Adds slashes to a string or recursively adds slashes to strings within an array.
- edit_post()Updates an existing post with values provided in `$_POST`.
- edit_user()Edit user settings based on contents of $_POST
- media_upload_form_handler()Handles form submissions for the legacy media uploader.
- upgrade_160()Execute changes made in WordPress 2.0.
- upgrade_230()Execute changes made in WordPress 2.3.
- wp_ajax_inline_save()Handles Quick Edit saving a post from a list table via AJAX.
- wp_ajax_replyto_comment()Handles replying to a comment via AJAX.
- wp_ajax_save_attachment()Handles updating attachment attributes via AJAX.
Source code
function wp_slash( $value ) { if ( is_array( $value ) ) { return array_map( 'wp_slash', $value ); } if ( is_string( $value ) ) { return addslashes( $value ); } return $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 7.1.0 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.