Removing HTML Tags from the_terms in WordPress: A Simple Guide

Home » Snippets » Removing HTML Tags from the_terms in WordPress: A Simple Guide
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Are you dealing with cluttered, disorganized, or unappealing term lists on your WordPress site because of unwanted HTML tags? Clarity and readability are crucial aspects of any web page and having clean term lists plays a huge part in that. Fortunately, WordPress allows you to strip away unwanted HTML tags from the_terms function. In this article, we will guide you through the process, enabling you to maintain a clean and professional appearance throughout your site.

					function wpturbo_strip_html_tags_from_terms( $term_list, $taxonomy, $before, $sep, $after ) {
    return strip_tags( $term_list );
}
add_filter( 'the_terms', 'wpturbo_strip_html_tags_from_terms', 10, 5 );
```
				

This code is pretty straightforward but can be very useful in certain situations. It is used to strip away HTML tags from terms which are retrieved using the WordPress function the_terms().

The first part of this code defines a new function named wpturbo_strip_html_tags_from_terms(). This function accepts five parameters: $term_list, $taxonomy, $before, $sep and $after.

function wpturbo_strip_html_tags_from_terms( $term_list, $taxonomy, $before, $sep, $after ) {
    return strip_tags( $term_list );
}

$term_list represents the list of terms that are to be displayed. $taxonomy represents the taxonomy name. $before, $sep, and $after are parts of the string returned by the_terms() function, where $before is displayed before the term list, $sep is the separator between terms, and $after is displayed after all the terms.

Inside this newly created function, there is only one command: return strip_tags( $term_list );. strip_tags() is a built-in PHP function that strips a string from HTML tags. In our usage here, this function won’t affect $taxonomy, $before, $sep, and $after because they aren’t passed to strip_tags().

The second part of the code is a call to the WordPress function add_filter(). The function’s purpose is to hook wpturbo_strip_html_tags_from_terms() into WordPress’s the_terms filter. Thus, before the_terms() function outputs the list of terms, our function wpturbo_strip_html_tags_from_terms() will be applied to the term list first – effectively stripping any HTML tags that may be part of the terms.

add_filter( 'the_terms', 'wpturbo_strip_html_tags_from_terms', 10, 5 );

In this function, ‘the_terms’ is the filter hook, wpturbo_strip_html_tags_from_terms is the callback function to be hooked into ‘the_terms’, ’10’ represents the priority of the function (the lower the number the higher the priority and earlier it will be executed), and ‘5’ is the accepted number of parameters for the callback function, which in this case is our wpturbo_strip_html_tags_from_terms. This is the same number of parameters accepted by the_terms() function.

Register an account to save your snippets or go Pro to get more features.