wppaste
WordPress

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().

Properly strips all HTML tags including 'script' and 'style'.

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?

The function only accepts scalar values. If $text fails is_scalar(), it calls wp_trigger_error() to raise an E_USER_WARNING and returns '' immediately, before any tag stripping happens.

Why did the text inside my script and style tags disappear completely?

Before calling strip_tags(), the source runs a regex (@<(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()?

The $remove_breaks parameter defaults to false, so only tags are stripped; line breaks, tabs and repeated spaces are left untouched unless you opt in.

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

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–24

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 38.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
35

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

What it touches

  • hookthird-party callbacksdo_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.

WhenInstructionsCalls it makes
$text === null5none
$text !== null && is_scalar($text)16–19strip_tags()
$text !== null && !is_scalar($text)24__(), sprintf(), wp_trigger_error()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev385–243
8.5385–243
8.4385–2438 fewer instructions than PHP 8.3
8.3465–273
8.2465–273
8.1465–2732 fewer instructions than PHP 7.4
7.4485–293

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

Show all 35

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.

  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.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.