Adding HTML and Shortcode support to WordPress Category Descriptions

This is a guest article by Dan Harrison, who’s the director and chief developer for WordPress Doctors. Dan is also an experienced affiliate marketeer, focusing on eco and gadget niches.

Many of us spend a lot of time on our affiliate sites making them damn useful for our website visitors. That includes adding plenty of relevant and useful content. How many of you are adding descriptions to all of your categories? I expect quite a few of you are already doing it.

Are you frustrated by WordPress’s default behaviour of not permitting HTML in Category Descriptions? Do you want to add bold, images, perhaps even video to your categories? Well this article shows you how to fix that, plus another little trick for supporting WordPress shortcodes.

Adding HTML in Category Descriptions

This is a very simple fix, allowing HTML in category descriptions just requires you to add the following code to functions.php for your current theme:

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

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

That code basically tells WordPress not to strip out HTML when saving a category description. So now, HTML will work in your current theme.

HTML Categories description editing

HTML Categories description rendered

Adding Category Descriptions to your WordPress Theme

If you don’t already have category descriptions in your theme, then add the following code to your theme’s category.php. If you don’t have a functions.php file, then copy index.php to category.php, and make some tweaks.

<?php
$category_description = category_description();
if (!empty( $category_description)) {
	echo '<div class="category_description">' 
	      . $category_description . 
	     '</div>';
}
?>

Adding Shortcode Support to Category Descriptions

If you don’t know what a WordPress shortcode is, it’s basically a word inside square brackets that looks a little like HTML, e.g. [example_shortcode /]. In WordPress it’s the best way to insert ‘stuff’ into the main content of a page by a plugin or theme. e.g. this might include inserting YouTube videos without having to insert the nasty embed code.

If you know what they are, then you might appreciate being able to add shortcodes to category descriptions. To add shortcode support, you just need to tweak the code for showing descriptions to the following, using WordPress’ do_shortcode() function:

<?php
$category_description = category_description();
if (!empty( $category_description)) {
	echo '<div class="category_description">' 
	      . do_shortcode($category_description) . 
	     '</div>';
}
?>

HTML Categories with Shortcode editing

HTML Categories with Shortcode rendered

Conclusion

So there you go, some simple code that you can copy’n’paste into your WordPress theme to allow you to create pretty category descriptions. I hope you found that useful!

Please do post your examples in the comments below, we’d love to see what you’ve done with yours. Please only post 1 or 2 links in your comment though, so that the spam filter doesn’t automatically remove your comment.

4 Comments

  1. Cheers for the useful tip Dan!

  2. avatar
    Chritchansays:

    Great ! But is it possible to remove the shortcode of the meta description ?

  3. Thank you!! 🙂

  4. Since I am not a PHP programmer, I think I am missing a few steps:

    1: Where is: using WordPress’ do_shortcode() function:

    Using Ultimate Short codes… I pasted the content into shortcodes-ultimate/shortcodes-ultimate.php

    That yielded diddly…

    Could you please assist on exactly where I find this function to place the additional code?

    Thanx

Leave a comment

Please be polite. We appreciate that. Your email address will not be published and required fields are marked