Allow HTML in WordPress Category & Taxonomy Descriptions

html-badge

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.8 - 6 votes

Written by: on July 26, 2012. Last modified: July 27, 2012

Popular Marketplace Items

  • adposter-blocker-thumbnail
    $9

    AdPoster Blocker

     (2)
    A simple but powerful plugin that blocks spammy new user regs and/or ads/jobs/posts.
  • 2Checkout Payment Gateway Plugin
    $39

    2Checkout

     (4)
    Accept up to eight payment methods, fifteen languages, & twenty six currencies.
  • stripe-plugin
    $39

    Stripe

     (4)
    Process credit cards safely and securely on your AppThemes website.
  • Authorize.Net Thumbnail
    $39

    Authorize.Net

     (3)
    Easily start accepting online payments via credit card and e-check.
  • critic-icon-2
    $39

    Critic

     (6)
    A professional review and rating system for WordPress.
  • AppThemes Coupon Plugin
    $29

    AppThemes Coupons

     (8)
    An easy way to start offering coupons and promotions to your customers.
  • daddy-likes-190x130
    $19

    Daddy Like

     (10)
    A fast, lightweight, and elegant "like" system for comments, pages, posts, & activities.
  • featured
    $9

    QR Codes Widget

     (3)
    Add a widget with QR codes to your AppThemes site.
  • autoauto-thumbnail
    $39

    AutoResponder AutoRegistration

     (4)
    Automatically integrates WordPress registration with major autoresponders.
  • GeoReg thumbnail
    $29

    GeoReg

     (10)
    Captures detailed geographic info with new user registrations.
  • pcp-featured
    $39

    Price Comparer

     (3)
    Jump start your Price Comparision site with products from Amazon, Commission Junction, and LinkShare.
  • StarStruck WordPress Plugin Thumbnail
    $19

    StarStruck

     (15)
    A fast, lightweight, and elegant star rating system for comments, pages, & posts.