trying to filter the output of term_description to include a class https://codex.wordpress.org/Function_Reference/term_description

function add_class_to_term_description() { ?>

<?php  echo '<div class="cell">' . term_description() . '</div>'; ?>

<?php  }
add_filter( 'term_description', 'add_class_to_term_description' ); ?>

im getting and empty cell class returned hundreds of times which is exhausting the memory. what am i doing wrong?

  • Are you sure this hook term_description exists? I just couldn't find it online.. If it does, to proper way to apply a filter to it is return instead of echo, but you're missing what to filter as the function's first param. add_class_to_term_description( $term_description ) { return '<div class="cell">' . $term_description . '</div>'; } THAT if the hook exists.
    – Ismail
    May 27, 2016 at 11:54
  • thanks. its working to some extent. It returns <div class="term-description"><div class="cell"><p>content</p></div></div> so im thinking i need to use a filter to remove the container div. something like remove_filter($term_desciption, 'the_wrapper_div'); May 27, 2016 at 23:05

4 Answers 4


this does the trick

<?php function add_class_to_term_description($term_description) {
  echo '<div class="cell">' . $term_description. '</div>';
add_filter( 'term_description', 'add_class_to_term_description' ); ?>

Daniel is on the right track, using the value that is passed into the function. However, you should not be echoing anything because this is a filter. You should instead return the value.

function add_class_to_term_description( $term_description ) {
  return '<div class="cell">' . $term_description. '</div>';
add_filter( 'term_description', 'add_class_to_term_description' ); ?>

you have to use passed $value instead of term_description() function:

function add_class_to_term_description() {
  echo '<div class="cell">' . $value . '</div>';
add_filter( 'term_description', 'add_class_to_term_description' );

I tested many potential solutions, including all the answers above. They either did not work or did not work as expected - some output empty paragraphs, other do nothing. The best solution for me was to use the PHP function preg_replace to perform a search for the opening paragraph HTML tag and to insert the class attribute with the value I need like this:

function add_class_to_term_descr($content){
    return preg_replace('/<p([^>]+)?>/', '<p$1 class="lead">', $content, 1);
add_filter('term_description', 'add_class_to_term_descr');

This solution can be easily modified to match other types of WordPress output, like "div" tag for example. I based my solution on this answer and modified it according to my needs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.