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

  • thumbnail-vantage-user-locator
    $19

    User Locator for Vantage

     (7)
    Auto-detect & add your visitors location using geo coordinates.
  • thumbnail-vantage-image-slider
    $19

    Image Slider for Vantage

     (2)
    Replace the Vantage listing and events gallery with an image slider.
  • pcp-featured
    $39

    Price Comparer

     (3)
    Jump start your Price Comparision site with products from Amazon, Commission Junction, and LinkShare.
  • GeoReg thumbnail
    $29

    GeoReg

     (10)
    Captures detailed geographic info with new user registrations.
  • critic-icon-2
    $39

    Critic

     (6)
    A professional review and rating system for WordPress.
  • adposter-blocker-thumbnail
    $9

    AdPoster Blocker

     (2)
    A simple but powerful plugin that blocks spammy new user regs and/or ads/jobs/posts.
  • featured
    $9

    QR Codes Widget

     (3)
    Add a widget with QR codes to your AppThemes site.
  • daddy-likes-190x130
    $19

    Daddy Like

     (10)
    A fast, lightweight, and elegant "like" system for comments, pages, posts, & activities.
  • autoauto-thumbnail
    $39

    AutoResponder AutoRegistration

     (4)
    Automatically integrates WordPress registration with major autoresponders.
  • dear-seven-responsive-multipurpose-theme-thumbnail
    $39

    Envelope

     (1)
    A responsive multi-purpose WordPress theme for business or personal portfolio.
  • dear-seven-responsive-multipurpose-theme-thumbnail
    $39

    Creative

     (2)
    A responsive multi-purpose WordPress theme for business or personal portfolio.
  • Ad Stuffer for Jobroller by Appthemes
    $19

    Ad Stuffer JobRoller

     (1)
    Stuff your JobRoller Theme with 14 extra Ads & Links!
  • thumbnail
    $29

    Ad Injector

     (3)
    Inject adverts directly into listings and search results. Works with multiple themes.
  • StarStruck WordPress Plugin Thumbnail
    $19

    StarStruck

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