Strip HTML Tags From the_terms Function Output

How to Strip HTML Tags From the_terms Function Output

in Functions
Google+ LinkedIn

If you need to display the terms list of the current post on your website then you must use the_terms function. WordPress function the_term displays the linked terms of a taxonomy separated by user defined characters. It’s very powerful function to get list of categories, tags or any other custom defined taxonomies in WordPress. The only issue I can see is that it returns anchor HTML links of the terms which is fine in most general cases since most bloggers just want a way to list terms on their WordPress website. But on a project you may want to display the list of terms without anchor links, in just plain text. So in this article I will show you how we can strip HTML tags from the_terms output.

It is relatively easy since we can hook the WordPress filter to alter the output of the function. We will use strip_tags PHP function to simply strip any HTML tags in terms list. So here is the code snippet to do just that. Adding this snippet to the functions.php file of your WordPress theme will strip HTML tags from the_terms. Now you can display all your terms in just plain text and without any anchor links.

// strip HTML tags from the_terms
function wpcs_strip_html_from_terms( $term_list, $taxonomy, $before, $sep, $after ) {
    return strip_tags( $them_list );
}
add_filter( 'the_terms', 'wpcs_strip_html_from_terms', 10, 5 );

Simple. Right!

Share the love

If you like this snippet, share it with others!

Leave a Reply

Your email address will not be published. Required fields are marked *