wp_strip_all_tags( string $text, bool $remove_breaks = false ): string
- Since
- 2.9.0
- Source
wp-includes/formatting.php:5600
Strips HTML tags from a string while deleting the entire contents of any <script> or <style> blocks, not just the tags. Optionally collapses leftover line breaks and whitespace into single spaces when $remove_breaks is true. Non-scalar input (arrays, objects) triggers an E_USER_WARNING via wp_trigger_error() and returns an empty string, so validate input types before calling it. For output that still needs to be escaped for HTML display, pair it with esc_html().
Description
This differs from strip_tags() because it removes the contents of the <script> and <style> tags. E.g. strip_tags( '<script>something</script>' ) will return 'something'. wp_strip_all_tags() will return an empty string.
Compatibility
- WordPress
- since 2.9.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
$textstring- String containing HTML tags
$remove_breaksbooloptional- Whether to remove left over line breaks and white space charsDefault:
false
Return value
string- The processed string.
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.
Strip HTML from a post title before sending it in a plain-text email
Wrap the title of post ID 1 in markup to simulate a themed heading, then reduce it to plain text.
$post = get_post( 1 );
$html_title = '<h1 class="entry-title"><a href="#">' . $post->post_title . '</a></h1>';
$plain_title = wp_strip_all_tags( $html_title );
echo esc_html( $plain_title );wp_strip_all_tags() only removes markup; run the result through esc_html() again if you plan to echo it back into HTML.
Remove embedded script and style blocks and collapse whitespace
Build a chunk of markup around the price meta stored on post 2, including a script tag and a style tag, to show the difference $remove_breaks makes.
$price = get_post_meta( 2, 'price', true );
if ( '' === $price ) {
$price = '19.99';
}
$markup = "<div>\n\t<script>console.log('tracking price');</script>\n\tPrice: \$" . $price . "\n\t<style>.price{color:red;}</style>\n</div>";
$stripped = wp_strip_all_tags( $markup );
$stripped_clean = wp_strip_all_tags( $markup, true );
echo 'Without $remove_breaks: [' . esc_html( $stripped ) . "]\n";
echo 'With $remove_breaks: [' . esc_html( $stripped_clean ) . ']';Note that the console.log() call and the CSS rule disappear entirely, unlike strip_tags() which would leave their text content behind.
Common problems and fixes · 3
- Why does wp_strip_all_tags() return an empty string when I pass it an array?
- Why did the text inside my script and style tags disappear completely?
- Why are newlines and tabs still in my string after calling wp_strip_all_tags()?
Why does wp_strip_all_tags() return an empty string when I pass it an array?
Why did the text inside my script and style tags disappear completely?
@<(script|style)[^>]*?>.*?</\1>@si) that deletes the whole or element, tags and inner content together. That is the documented difference from PHP's own strip_tags().Why are newlines and tabs still in my string after calling wp_strip_all_tags()?
Alternatives and related functions
strip_tags- When you want tags removed but need the text inside and blocks to survive.
sanitize_text_field- When you're cleaning a single line of form input and also want extra whitespace collapsed and invalid UTF-8 removed in one call.
wp_kses- When you need to allow a specific set of HTML tags and attributes through instead of stripping everything.
esc_html- When the goal is safe HTML output rather than removing markup, since it encodes special characters instead of deleting tags.
Performance profile
How much work a call to wp_strip_all_tags() 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–24
- Plugin surface
- None
- Called by
- 35
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 38.
Nothing here hands control to plugin code.
35 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
do_action()one call below wp_strip_all_tags()
Further down the call graph this can also reach query, option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
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_strip_all_tags() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
$text === null | 5 | none |
$text !== null && is_scalar($text) | 16–19 | strip_tags() |
$text !== null && !is_scalar($text) | 24 | __(), sprintf(), wp_trigger_error() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 38 | 5–24 | 3 | |
| 8.5 | 38 | 5–24 | 3 | |
| 8.4 | 38 | 5–24 | 3 | 8 fewer instructions than PHP 8.3 |
| 8.3 | 46 | 5–27 | 3 | |
| 8.2 | 46 | 5–27 | 3 | |
| 8.1 | 46 | 5–27 | 3 | 2 fewer instructions than PHP 7.4 |
| 7.4 | 48 | 5–29 | 3 |
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 · 2
- wp_trigger_error()Generates a user-level error/warning/notice/deprecation message.
- __()Retrieves the translation of $text.
Used by · 35
- Plugin_Installer_Skin::do_overwrite()Checks if the plugin can be overwritten and outputs the HTML for overwriting a plugin on upload.
- Theme_Installer_Skin::do_overwrite()Checks if the theme can be overwritten and outputs the HTML for overwriting a theme on upload.
- WP_Application_Passwords_List_Table::print_js_template_row()Prints the JavaScript template for the new row item.
- WP_Font_Face::generate_and_print()Generates and prints the `@font-face` styles for the given fonts.
- WP_Links_List_Table::get_primary_column_aria_label()Returns a clean label for the primary (Name) column's row header `aria-label`.
- WP_List_Table::single_row_columns()Generates the columns for a single row of the table.
- WP_MS_Sites_List_Table::get_primary_column_aria_label()Returns a clean label for the primary (URL) column's row header `aria-label`.
- WP_Media_List_Table::get_primary_column_aria_label()Returns a clean label for the primary (File) column's row header `aria-label`.
- WP_Posts_List_Table::column_title()Handles the title column output.
- WP_REST_Block_Directory_Controller::prepare_item_for_response()Parse block metadata for a block, and prepare it for an API response.
- WP_REST_URL_Details_Controller::prepare_metadata_for_output()Prepares the metadata by: - stripping all HTML tags and tag entities.
- WP_Recovery_Mode_Email_Service::send_recovery_mode_email()Sends the Recovery Mode email to the site admin email address.
Show all 35
- WP_Screen::render_list_table_columns_preferences()Renders the list table columns preferences.
- WP_Style_Engine_CSS_Declarations::filter_declaration()Filters a CSS property + value pair.
- WP_Users_List_Table::single_row()Generates HTML for a single row on the users.php admin panel.
- WP_Widget_RSS::widget()Outputs the content for the current RSS widget instance.
- _sanitize_text_fields()Internal helper function to sanitize a string from user input or from the database.
- block_core_page_list_render_nested_page_list()Outputs Page list markup from an array of pages with nested children.
- block_core_tab_list_render_callback()Render callback for core/tab-list.
- do_meta_boxes()Meta-Box template function.
- edit_post()Updates an existing post with values provided in `$_POST`.
- media_upload_form_handler()Handles form submissions for the legacy media uploader.
- render_block_core_navigation_submenu()Renders the `core/navigation-submenu` block.
- render_block_core_playlist()Renders the `core/playlist` block on server.
- render_block_core_search()Dynamically renders the `core/search` block.
- sanitize_user()Sanitizes a username, stripping out unsafe characters.
- twenty_twenty_one_generate_css()Generates CSS.
- wp_admin_bar_my_account_item()Adds the "My Account" item.
- wp_ajax_save_attachment()Handles updating attachment attributes via AJAX.
- wp_check_comment_disallowed_list()Checks if a comment contains disallowed characters or words.
- wp_get_tooltip_helper()Retrieves the markup for an accessible tooltip or toggletip.
- wp_html_excerpt()Safely extracts not more than the first $count characters from HTML string.
- wp_send_note_notification()Sends a single note mention notification email.
- wp_setup_nav_menu_item()Decorates a menu item object with the shared navigation menu item properties.
- wp_trim_words()Trims text to a certain number of words.
Source code
function wp_strip_all_tags( $text, $remove_breaks = false ) { if ( is_null( $text ) ) { return ''; } if ( ! is_scalar( $text ) ) { /* * To maintain consistency with pre-PHP 8 error levels, * wp_trigger_error() is used to trigger an E_USER_WARNING, * rather than _doing_it_wrong(), which triggers an E_USER_NOTICE. */ wp_trigger_error( '', sprintf( /* translators: 1: The function name, 2: The argument number, 3: The argument name, 4: The expected type, 5: The provided type. */ __( 'Warning: %1$s expects parameter %2$s (%3$s) to be a %4$s, %5$s given.' ), __FUNCTION__, '#1', '$text', 'string', gettype( $text ) ), E_USER_WARNING ); return ''; } $text = preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $text ); $text = strip_tags( $text ); if ( $remove_breaks ) { $text = preg_replace( '/[\r\n\t ]+/', ' ', $text ); } return trim( $text );}Changelog
Introduced in 2.9.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.