wp_strip_all_tags() WordPress Function

The wp_strip_all_tags() function is a quick way to strip all HTML tags from a string. This can be useful when you want to display a string of text without any formatting, or when you want to make sure that no malicious code is being injected into a string of text.

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

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 ”


Top ↑

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


Top ↑

Return

(string) The processed string.


Top ↑

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

Top ↑

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 );
}


Top ↑

Changelog

Changelog
VersionDescription
2.9.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More