How to Truncate Description within Post Tags in the WordPress Admin Panel

Home » Snippets » How to Truncate Description within Post Tags in the WordPress Admin Panel

Created with:



Creator: WPTurbo Team

Customize with WPTurbo AI

Add Snippet To Project

New Project
Add To Existing Project

If you’ve ever worked with post tags in WordPress, you may have noticed that the description field for each tag can appear quite lengthy when displayed in the admin panel. This can make it difficult to navigate and can even slow down the load time for your site. In this article, we’ll show you how to truncate the descriptions for your post tags in the admin panel to keep things tidy and organized.

					function wpturbo_truncate_post_tags_desc() {
    global $post;
    if ( $post && $post->post_type === 'post' && $post->post_status === 'draft' ) {
        $tags = wp_get_post_tags( $post->ID );
        foreach ( $tags as $tag ) {
            if ( strlen( $tag->description ) > 150 ) {
                $tag->description = substr( $tag->description, 0, 150 ) . '...';
add_action( 'save_post', 'wpturbo_truncate_post_tags_desc' );

This code snippet is used to truncate the description within post tags in the WordPress admin panel. It retrieves the description of all tags associated with a post and truncates it if it is longer than 150 characters. The truncation appends "…" to the end of the description. The maximum length of the description that needs to be displayed can be changed by modifying the number 150 in the code to a different value.

The function wpturbo_truncate_post_tags_desc() is used to retrieve and truncate the tag descriptions. It first checks if the post is a draft using the global $post and $post->post_status === 'draft' conditions. If the post is a draft, it then gets all the tags associated with the post using the wp_get_post_tags() function and stores them in an array called $tags.

The function then loops through each of the tags using a foreach loop. For each tag, it checks if the length of the tag’s description is longer than 150 characters using the strlen() function and truncates the description if it is longer. The truncation is done using the substr() function which takes the first 150 characters of the description, appends "…" and then assigns the truncated string back to the tag’s description.

Finally, the wpturbo_truncate_post_tags_desc() function is hooked into the save_post action using the add_action() function. This ensures that the tag descriptions are truncated and saved whenever a post is saved in the WordPress admin panel.

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