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

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

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.
  • GeoReg thumbnail
    $29

    GeoReg

     (10)
    Captures detailed geographic info with new user registrations.
  • 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.
  • 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

     (14)
    A fast, lightweight, and elegant star rating system for comments, pages, & posts.
  • dear-seven-responsive-multipurpose-theme-thumbnail
    $39

    Envelope

     (1)
    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!
  • critic-icon-2
    $39

    Critic

     (6)
    A professional review and rating system for WordPress.
  • daddy-likes-190x130
    $19

    Daddy Like

     (10)
    A fast, lightweight, and elegant "like" system for comments, pages, posts, & activities.
  • thumbnail-vantage-image-slider
    $19

    Image Slider for Vantage

     (2)
    Replace the Vantage listing and events gallery with an image slider.
  • dear-seven-responsive-multipurpose-theme-thumbnail
    $39

    Creative

     (2)
    A responsive multi-purpose WordPress theme for business or personal portfolio.
  • thumbnail
    $29

    Ad Injector

     (3)
    Inject adverts directly into listings and search results. Works with multiple themes.
  • thumbnail-vantage-user-locator
    $19

    User Locator for Vantage

     (7)
    Auto-detect & add your visitors location using geo coordinates.