W3cubDocs

/WordPress

wp_strip_all_tags( string $string, bool $remove_breaks = false )

Properly strip 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 ”

Parameters

$string

(string) (Required) String containing HTML tags

$remove_breaks

(bool) (Optional) Whether to remove left over line breaks and white space chars

Default value: false

Return

(string) The processed string.

More Information

wp_strip_all_tags() is added to the following filters by default (see wp-includes/default-filters.php):

  • pre_comment_author_url
  • pre_user_url
  • pre_link_url
  • pre_link_image
  • pre_link_rss
  • pre_post_guid

It is also applied to these filters by default when on the administration side of the site:

  • user_url
  • link_url
  • link_image
  • link_rss
  • comment_url
  • post_guid

Source

File: wp-includes/formatting.php

function wp_strip_all_tags( $string, $remove_breaks = false ) {
	$string = preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $string );
	$string = strip_tags( $string );

	if ( $remove_breaks ) {
		$string = preg_replace( '/[\r\n\t ]+/', ' ', $string );
	}

	return trim( $string );
}

Changelog

Version Description
2.9.0 Introduced.

© 2003–2019 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/wp_strip_all_tags