Limit Words in Category / Term Description – Admin Panel

I’m working with a Taxonomy where the Terms have lengthy descriptions. It makes the Admin Panel look really ugly when WordPress tries to display these descriptions for 10 categories. I’m trying to figure out a way to limit the number of words or characters displayed in the Category / Term Description Column but I cannot find the correct hook to get to the category description as it’s displaying it. I’m using this list of Hooks as a guide, and the two hooks that seem to point to the right direction are:

filter: manage_taxonomies_for_{$post_type}_columns

function funcy_test($empty = '', $column, $term_id) 
{
    return '';
}
add_filter('manage_taxonomies_for_cpt_products_columns', 'funcy_test', 10, 3);

I expected it to return empty columns all around but instead I didn’t see a result. One possible solution is to remove the Description cateogry entirely and use the custom_columns hook to add my own description column but that just seems too much of a hassle.

Is there a way to limit the words / characters for a term description in the Admin Panel?

Solutions Collecting From Web of "Limit Words in Category / Term Description – Admin Panel"

Ok, I revisited this and revised my answer..

But like I said before:

the hook you are (trying) to use won’t/can’t work for the purpose you are aiming for, simply because its only occurrence is in class-wp-posts-list-table.php, which nomen est omen is used to show the post list, the right place should be class-wp-terms-list-table.php.

1. What you can do is the following:

  • Making use of the admin_head-$hook_suffix and get_terms hook

    add_action(
        'admin_head-edit-tags.php',
        'wpse152619_edit_tags_trim_description'
    );
    function wpse152619_edit_tags_trim_description() {
        add_filter(
            'get_terms',
            'wpse152619_trim_description_callback',
            100,
            2
        );
    }
    
    function wpse152619_trim_description_callback( $terms, $taxonomies ) {
        // print_r($taxonomies);
        if( 'category' == $taxonomies[ 0 ] ) {
            foreach( $terms as $key => $term ) {
                $terms[ $key ]->description =
                    wp_trim_words(
                        $term->description,
                        12,
                        ' [...]'
                    );
            }
        }
        return $terms;
    }
    

    This trims the description to a specified word count.

2. Another often found possibility is removing the default column and create a new one:

  • Making use of the manage_{$this->screen->taxonomy}_custom_column and manage_{$this->screen->id}_columns hook

    function wpse152619_create_new_description_column( $columns ) {
        if ( $_GET[ 'taxonomy' ] == 'category' ) {
            // uncomment next line to remove default description column
            //unset( $columns[ 'description' ] );
            // create new description column
            $columns[ 'new_description' ] = 'New Description';
            return $columns;
        }
        return $columns;
    }
    add_filter(
        'manage_edit-category_columns',
        'wpse152619_create_new_description_column'
    );
    
    
    function wpse152619_new_description_column_content(
        $deprecated,
        $column_name,
        $term_id
    ) {
        if ( $column_name == 'new_description' ) {
            $new_description =
                wp_trim_words(
                    term_description(
                        $term_id,
                        $_GET[ 'taxonomy' ]
                    ),
                    12,
                    ' [...]'
                );
            echo $new_description;
        }
    }
    add_filter(
        'manage_category_custom_column',
        'wpse152619_new_description_column_content',
        10,
        3
    );
    

3. Also possible would be:

  • Extend the class WP_List_Table yourself, like WP_Terms_List_Table it does;
  • In your case the main goal would be redefining the column_description method;
  • Additionally, create a new template, similar to edit-tags.php, but using your newly create class;
  • Then you would have to make sure that your new template is loaded instead of the default one;
  • Ok, but that’s just a outline of would be possible too. Makes no sense in this simple case, but for advanced use cases this would be an approach to consider.

I’m sure the above should solve your problem. Note that I did the examples for category edit page, but how to do this for custom taxonomies should be apparent. Depending on what is needed a simple or elaborate approach can be chosen. In you case the first one should be just fine.

Last but not least the following function is handy to get the information you need:

add_action( 'admin_head', 'wpse152619_dbg_dev' );
function wpse152619_dbg_dev() {
    global $pagenow;
    print_r( $pagenow );
    echo '<br>';
    print_r( $_GET[ 'taxonomy' ] );
    echo '<br>';
    $current_screen = get_current_screen();
    print_r( $current_screen->id );
}