Allow HTML in Taxonomy Descriptions

Setup taxonomy descriptions to include html

Some of the most underutilized fields in WordPress are the category, tag, and taxonomy description fields. These fields can actually be displayed on the front-end and used to really build out your category and tag pages.

The problem is category description fields (or any taxonomy description fields) are limited to just basic html markup (i.e. a href, strong, etc) but what if you want to also include other html?

Here’s a nifty block of code that allows you to do just that. It removes the sanitize filters from any taxonomy description field (i.e. blog category, custom taxonomy).

Add the following code to your functions.php file and then add some html to your category, tag, or taxonomy description fields and then view them on your site (you’ll need to have the correct php code in your theme templates in order for it to show up).

Code to Remove HTML Filters

$filters = array( 'pre_term_description' );
 
foreach ( $filters as $filter ) {
    remove_filter( $filter, 'wp_filter_kses' );
}
 
foreach ( array( 'term_description' ) as $filter ) {
    remove_filter( $filter, 'wp_kses_data' );
}

Now the code is setup to easily pass in other items you’d like to remove the filters from. For example, if you also want to add html to your link description and user profile description fields, just add these two items to the $filters array like so:

Remove Filter From Additional Fields

$filters = array( 'pre_term_description', 'pre_link_description', 'pre_user_description' );

Here’s the code you’ll want to have in your theme template file (i.e. category.php) in order to display the new descriptions. Some themes may not already have this in there which means it won’t show up.

Code to Display Description on Category Page

$category_description = category_description();
 
if ( ! empty( $category_description ) )
    echo apply_filters( 'category_archive_meta', '<div class="category-desc">' . $category_description . '</div>' );

To display the description for other taxonomy pages, you’ll need this code added to your taxonomy-[TAXONOMY_NAME].php template.

Code to Display Description on Custom Taxonomy Page

$term = get_term_by( 'slug', get_query_var('term'), get_query_var('taxonomy') );
 
echo '<h1>' . esc_html( $term->name; ) . '</h1>';
echo '<p class="desc">' . esc_html( $term->description; ) . '</p>';

That’s it. Now you can build out your boring category, tag, and custom taxonomy pages with rich html descriptions.

Here's the final result on the front-end


Here’s the final result. The image path was incorrect which is why the image isn’t showing up correctly.

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

Your rating: none
Rating: 4.5 - 6 votes

Written by: on July 15, 2011. Last modified: July 15, 2011

Popular Marketplace Items

  • daddy-likes-190x130
    $19

    Daddy Like

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

    Price Comparer

     (3)
    Jump start your Price Comparision site with products from Amazon, Commission Junction, and LinkShare.
  • balanced-payments-plugin-sm
    $39

    Balanced Payments

     (3)
    Accept credit cards and setup escrow payments.
  • wp-smart-export-final
    $25

    WP Smart Export

     (4)
    A highly customizable WordPress data exporter plugin.
  • StarStruck WordPress Plugin Thumbnail
    $19

    StarStruck

     (16)
    A fast, lightweight, and elegant star rating system for comments, pages, & posts.
  • 2Checkout Payment Gateway Plugin
    $39

    2Checkout

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

    Marine

     (1)
    A clean, modernĀ and fully responsive marine-based goods WooCommerce Theme.
  • autoauto-thumbnail
    $39

    AutoResponder AutoRegistration

     (4)
    Automatically integrates WordPress registration with major autoresponders.
  • Authorize.Net Thumbnail
    $39

    Authorize.Net

     (3)
    Easily start accepting online payments via credit card and e-check.
  • GeoReg thumbnail
    $29

    GeoReg

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

    Stripe

     (4)
    Process credit cards safely and securely on your AppThemes website.
  • critic-icon-2
    $39

    Critic

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

    AppThemes Coupons

     (9)
    An easy way to start offering coupons and promotions to your customers.
  • screenshot
    $39

    Convert

     (2)
    A conversion focused responsive Clipper child theme with premium features.
  • 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.