Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Unable to echo imgs or links in tag_description(); WordPress


I have been building out my tag.php in my template to offer as much relevant content as possible when an incoming reader lands on this page. Something I wanted to do was provide a picture as well as brief writeup about the topic of the tag.

I hacked the functions.php so that I could input images, links, paragraphs, and other HTML into this field, but when I call it via <?php echo tag_description(); ?>, all i get is just the stripped down text.

Please see the two screen grabs linked below:

[[LINK href=""]]Screen shot of Post Tags page[[/LINK]]

[[LINK href=""]]Screen shot of web page without image showing.[[/LINK]]

Code in my tag description:

<p style="text-align: center;"><img class="size-full wp-image-95014 aligncenter" src="" alt="" width="620" height="413" /></p>
<p>Hyperfuse delivers superior lightweight footwear performance, inspired by the rugged outdoor basketball play in China. The <strong>Nike Hyperfuse</strong> composite system offers a superior stability layer, a breathable mesh layer, and a durable outer skin layer that combine to give footwear new strength, breathability and lightweight (at roughly 12.5 ounces for a size 9). In addition to its high-performance characteristics, <strong>Nike Hyperfuse</strong> dramatically changes the way footwear is made. Significantly increasing the precision of the manufacturing process, Hyperfuse “fuses” its distinct layers into a composite using a unique manufacturing method. Hyperfuse eliminates the need to manually stitch together footwear materials, and provides engineered zones of performance with a level of precision never possible with traditional cut-and-sew construction. The Nike Hyperfuse features Zoom Air cushioning for a responsive ride.</p> <p>The Nike Hyperfuse retails for $100 and will be available at <a href="">Nice Kicks</a>.</p>

I have added the following code to my functions.php file:

remove_filter( ‘term_description’, ‘wp_kses_data’ );
remove_filter( ‘pre_term_description’, ‘wp_filter_kses’ );

Below is the code for my tags.php file:

<?php get_header(); ?>

<div id="container">

<div id="content">

<?php if (have_posts()) : ?>

<?php $post = $posts[0]; // Hack. Set $post so that the_date() works. ?>
<div class="post">
<h1><?php single_tag_title(); ?></h1>

<?php echo tag_description(); ?></div>

<?php while (have_posts()) : the_post(); ?>

<div <?php post_class() ?>>
<div class="post-image">
<span class="tag"><?php the_tags('',''); ?></span>
<a href="<?php the_permalink(); ?>">
<?php $customField = get_post_custom_values('image');
if (isset($customField[0])) { ?>

<img src="<?php echo $customField[0]; ?>" width="325" alt="<?php the_title(); ?>" />

<?php } else { ?>

<img src="<?php echo catch_that_image(); ?>" width="325" alt="<?php the_title(); ?>" />

<?php } ?>

<div class="post-text">

<div class="the-time"><?php the_time('F j, Y'); ?> / <a href="<?php the_permalink(); ?>/#idc-container-parent"><?php comments_number('0 Comments','One Comment','% Comments'); ?></a></div>

<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>

<p><?php the_content_rss('', TRUE, '', 50); ?></p>


<div class="clearing"></div>

<?php endwhile; ?>

<?php else :

echo("<h2>Sorry, but there aren't any posts about '<?php single_tag_title(); ?>'.</h2>");



<?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } ?>


<?php get_sidebar(); ?>

<div class="clearing"></div>


<?php get_footer(); ?>

Answers (3)


wjm answers:

hi Matt.
do you have the code for tag.php?

and also the code that is saved in the tag description

wjm comments:

if you aplied those filters in functions.php

tag_description() should print the image.
i dont know why you are not getting that.

you can try this,
replace echo tag_description();

//echo tag_description();
global $wp_query;
$term = $wp_query->get_queried_object();
$taxonomy = $term->taxonomy;
$term = $term->term_id;
echo get_term_field( 'description', $term, $taxonomy, $context = 'raw' )

tell me if you get the image, when we well have to format that. just to see if it works.


Xavier Faraudo answers:

There is a filter that forbids using (X)HTML in term descriptions. The pre_term_description filter calls the wp_filter_kses function, which strips the tags.

You should add this to your functions.php:

remove_filter( 'pre_term_description', 'wp_filter_kses' );

Xavier Faraudo comments:

Well, there is an easier, not elegant, solution:
1) Get the term (tag)
2) Directly echo its description.
Add this instead of echo tag_description();

$term = get_term_by( 'slug' , get_query_var( 'tag' ), 'post_tag' );
echo $term->description;


Christopher Ross answers:

The problem you're facing is that tag_description() calls term_description() which in turn calls get_term_field(). In get_term_field(), it runs sanitize_term_field() on line 690 of taxonomy.php, this cleans the content and applies filters to strip the img.

There are a couple solutions, the first would be to edit taxonomy.php and at line 690 (return sanitize_term_field($field, $term->$field, $term->term_id, $taxonomy, $context);) and place an if() to check if you're on a tag.php page but that could be dangerous in the long run (and would be wiped on the next upgrade).

The second option would be for you to create a folder in your site and upload the graphics to it. In your tag.php, add the line:

if (file_exists(ABSPATH."/images/".$term->term_id.".png") {echo "<img src='/images/".$term->term_id.".png"'>";}

That way, if there is a file named "1.png" it would appear when the tag is displayed.

Will that do it for you?


----- [edit]

Adding the if() to the taxonomy.php page wouldn't cause a noticeable increase in speed but if you're concerned over the speed, another fairly pain free method to do this would be to simply call the data directly on the tag.php page.

For example:

$tagID = get_query_var('tag_id');
$query = 'SELECT description FROM wp_term_taxonomy WHERE term_id ='.$tagID.' LIMIT 1';
$results = mysql_query($query);
while($line = mysql_fetch_assoc($results)) {
echo $line['description'];

If you replace your current echo tag_description(); with the code above, it should (I've not tested it) return and display the description, skipping the filters.

Matt Halfhill comments:

My site has alot of traffic, but very little traffic to archives, category, or tag pages. Would putting that if() in the taxonomy.php cause the rest of the site to slow much who are usually just visiting the blog (page 1, page 2, page 3, etc) individual posts, and pages?