What is the difference between wp_strip_all_tags and wp_filter_nohtml_kses?

The wp_strip_all_tags() function will remove all HTML, including the content of script and style tags.

The PHP strip_tags() function largely does the same thing, except it won’t eliminate the content of script and style tags. WP’s wp_strip_all_tags() function uses this after eliminating the scripts and styles manually.

The wp_filter_nohtml_kses() function uses kses to remove all HTML. The main difference is the engine used for parsing the HTML. PHP’s strip_tags() doesn’t deal particularly well with broken or intentionally malformed HTML, because it doesn’t perform any validation of the HTML. The kses engine attempts to handle malformed HTML in a better way, but it is also not a fully complete HTML parser. It is also much slower.

The wp_strip_all_tags() function is generally preferred. It’s good enough for the majority of cases. The kses version is mostly included for completeness, but may be useful if you’re dealing with particularly badly formed HTML.

Leave a Comment