How to Truncate Descriptions within Categories Admin Panel in WordPress

Home » Snippets » How to Truncate Descriptions within Categories Admin Panel in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

If you’re a WordPress site owner, you may have faced the issue of having long category descriptions that mess up the layout of your Categories admin panel. Fortunately, there’s a simple solution to this problem: truncating the description to a specific number of characters. In this article, we’ll guide you through the process of truncating category descriptions within the Categories admin panel, keeping your panel clean and organized.

					function wpturbo_truncate_category_description( $columns ) {
    $columns['description'] = 'Description';
    add_filter( 'manage_edit-category_columns', function() use ( $columns ) {
        unset( $columns['description'] );
        return $columns;
    }, 11 );
    
    add_action( 'manage_category_custom_column', function( $column_name, $term_id ) {
        if ( 'description' === $column_name ) {
            $desc = term_description( $term_id, 'category' );
            $desc_length = 60;
            if ( strlen( $desc ) > $desc_length ) {
                echo substr( $desc, 0, $desc_length ) . '...';
            } else {
                echo $desc;
            }
        }
    }, 10, 2 );
    return $columns;
}
add_filter( 'manage_edit-category_columns', 'wpturbo_truncate_category_description' );
				

The purpose of this code snippet is to truncate the category description within the categories admin panel. By default, the category description field in the admin panel can display a large amount of text, which can make the table difficult to read and navigate. With this code snippet, the category description will be truncated to a certain length, making it more manageable.

The first part of the code defines a function called wpturbo_truncate_category_description(). This function adds a new column called "Description" to the category admin panel and filters out the default "description" column.

function wpturbo_truncate_category_description( $columns ) {
    $columns['description'] = 'Description';
    add_filter( 'manage_edit-category_columns', function() use ( $columns ) {
        unset( $columns['description'] );
        return $columns;
    }, 11 );
    ...

The second part of the code adds an action that displays the truncated category description in the new "Description" column. The manage_category_custom_column action is used to filter the output of a custom column for a taxonomy term in the admin panel.

    ...
    add_action( 'manage_category_custom_column', function( $column_name, $term_id ) {
        if ( 'description' === $column_name ) {
            $desc = term_description( $term_id, 'category' );
            $desc_length = 60;
            if ( strlen( $desc ) > $desc_length ) {
                echo substr( $desc, 0, $desc_length ) . '...';
            } else {
                echo $desc;
            }
        }
    }, 10, 2 );

Inside the manage_category_custom_column action, we first check if the $column_name parameter is equal to ‘description’. If it is, we get the category description using the term_description() function, and define a maximum length for the description to be truncated.

Then, we use an if/else statement to determine whether or not the description should be truncated. If the description is longer than the defined length, we use the substr() function to truncate the description and append ‘…’ to the end. Otherwise, we simply output the full description.

Finally, we return the modified $columns array from the wpturbo_truncate_category_description() function, which updates the admin panel with the new "Description" column and truncated descriptions. The add_filter() function is used to apply this function to the manage_edit-category_columns filter.

Register an account to save your snippets or go Pro to get more features.