AppThemes Docs

Allow HTML in WordPress Category & Taxonomy Descriptions

There’s a WordPress field we think is vastly underused. It’s the description field attached to each category and custom taxonomy. Not sure what I mean?

Log in to your site and go edit a post category. There you’ll see a description field which most people think is just for internal use. Well, it’s not!

Enter some text, save the category, and now view that category page on your website. You should see the description below the category name. If not, your theme wasn’t coded to include this.

Wiring up your theme to support category descriptions

You can skip this step if your theme already does this. If not, read on. You’ll first need to edit your category.php. Now pick a spot where you’d like the category description to appear. We usually put it below the category title tag like such:

<h1 class="title"><?php printf( __( 'Category Archives: %s', 'appthemes' ), '<span>' . single_cat_title( '', false ) . '</span>' ); ?></h1>
 
<?php
// this part is new
$cat_desc = category_description();
if ( ! empty( $cat_desc ) ) {
    echo apply_filters( 'category_archive_meta', '<div class="cat-desc">' . $cat_desc . '</div>' );
}
?>

Want the same thing for a custom taxonomy? Replace the category_description() function call with term_description(). For bonus points, you can also do this for your tag page templates. Just use the tag_description() function.

If you aren’t already using the category, taxonomy, and tag description fields, we highly recommend starting now. Not only is this a great way to write descriptive unique text for each of those items, it also adds SEO value by creating unique content and beefing up your pages.

The only limitation is no HTML is allowed in the category or taxonomy descriptions. WordPress strips it out by default. Which now brings us to the reason you’re actually reading this article!

Enabling HTML in your category & taxonomy descriptions

Open up your theme’s functions.php file and add the following code:

// allow html in category and taxonomy descriptions
remove_filter( 'pre_term_description', 'wp_filter_kses' );
remove_filter( 'pre_link_description', 'wp_filter_kses' );
remove_filter( 'pre_link_notes', 'wp_filter_kses' );
remove_filter( 'term_description', 'wp_kses_data' );

Now go back into wp-admin and edit your category. This time, add some html and then save it. Here’s an example:

Visit your category page and you should now see the marked up description!

Caveats

Adding this code removes the native security filters WordPress applies to those fields. If you are running a small site with only a few trusted authors, then you won’t have any problems. On the other hand, if you’re running a large-scale Multisite instance with unknown users, it’s safer not to follow this tutorial.

Besides just HTML, users could add malicious code (on purpose or by accident) which could hurt your site. Exercise with caution, otherwise enjoy your HTML-enabled descriptions!

Like this tutorial? Subscribe and get the latest tutorials delivered straight to your inbox or feed reader.

Your rating: none
Rating: 4.9 - 9 votes