Using WordPress ‘category_description’ PHP filter

The category_description WordPress PHP filter allows you to modify the category description for display.

Usage

add_filter('category_description', 'your_custom_function', 10, 2);

function your_custom_function($description, $category) {
    // Your custom code here
    return $description;
}

Parameters

  • $description (string) – The original category description.
  • $category (WP_Term) – The category object.

More information

See WordPress Developer Resources: category_description

Examples

Add a prefix to category descriptions

Add a custom prefix to all category descriptions.

add_filter('category_description', 'add_prefix_to_category_description', 10, 2);

function add_prefix_to_category_description($description, $category) {
    $prefix = 'About: ';
    return $prefix . $description;
}

Capitalize category descriptions

Capitalize the first letter of each word in the category description.

add_filter('category_description', 'capitalize_category_description', 10, 2);

function capitalize_category_description($description, $category) {
    return ucwords($description);
}

Add a custom suffix to category descriptions

Add a custom suffix to all category descriptions.

add_filter('category_description', 'add_suffix_to_category_description', 10, 2);

function add_suffix_to_category_description($description, $category) {
    $suffix = ' - Enjoy!';
    return $description . $suffix;
}

Replace specific words in category descriptions

Replace specific words in the category description with a custom word.

add_filter('category_description', 'replace_words_in_category_description', 10, 2);

function replace_words_in_category_description($description, $category) {
    $search = 'old-word';
    $replace = 'new-word';
    return str_replace($search, $replace, $description);
}

Remove HTML tags from category descriptions

Strip HTML tags from the category description.

add_filter('category_description', 'strip_html_tags_from_category_description', 10, 2);

function strip_html_tags_from_category_description($description, $category) {
    return strip_tags($description);
}