1

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?

3 Answers 3

4

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 );
}
0

After some look around I found that you can put anything inside wp_trim_words:

<?php echo wp_trim_words( get_the_content(), 40, '...' ); ?>

Replace get_the_content() with your function, in your case you can use this:

<?php echo wp_trim_words( get_the_archive_description(), 30, '...' ); ?>

For more documentation check wp_trim_words()

0

A little cleaner reading solution.

    add_action( 'admin_head-edit-tags.php', 'admin_edit_tags' );
    function admin_edit_tags() {
        add_filter( 'get_terms', 'admin_trim_category_description', 10, 2 );
    }
    function admin_trim_category_description( $terms, $taxonomies ) {
        if ( !is_admin()) // only allow in admin
            return;
    
        if( 'category' != $taxonomies[0] )
            return $terms;
        foreach( $terms as $key=>$term )
            // change 150 to whatever character length you want the description to be
            $terms[$key]->description = substr( $term->description, 0, 150 ); 
        return $terms;
}

orig. from https://www.isitwp.com/truncate-description-within-categories-admin-panel/

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.