Dealing with Long Taxonomy Descriptions in the WordPress Administration Panels

Posted on

By Michael Fields

If you’re like me, you try to take full advantage of the taxonomy system the the latest version of WordPress. Unfortunately, the taxonomy administration panels tend to get quickly bloated where terms have descriptions that are longer than a few words. Here is a quick and easy method to help put an end to long descriptions messing with your work flow.

Shorten the Term Descriptions

/**
 * Create actions for all taxonomies.
 *
 * @return    void
 *
 * @since     2010-05-31
 * @alter     2011-01-09
 */
function mfields_taxonomy_description_add_actions() {
	global $wp_taxonomies;
	foreach ( $wp_taxonomies as $taxonomy => $taxonomies ) {
		add_action( 'manage_' . $taxonomy . '_custom_column', 'mfields_taxonomy_description_rows', 10, 3 );
		add_action( 'manage_edit-' . $taxonomy . '_columns',  'mfields_taxonomy_description_columns' );
	}
}
add_action( 'admin_init', 'mfields_taxonomy_description_add_actions' );


/**
 * Filter the taxonomy tables columns.
 *
 * Remove the default "Description" column.
 * Add a custom "Short Description" column.
 * 
 * @param     array     Unfiltered columns for the taxonomy's edit screen.
 * @return    array     Modified columns for the taxonomy's edit screen.
 *
 * @since     2010-05-31
 * @alter     2011-01-09
 */
function mfields_taxonomy_description_columns( $c ) {
	unset( $c['description'] );
	$c['short_description'] = 'Description';
	return $c;
}


/**
 * Display the shortened description in each row's custom column.
 *
 * @param     string    Should be empty.
 * @return    string    Name of the column.
 * @return    string    Term id. Integer represented as string.
 *
 * @since     2010-05-31
 * @alter     2011-01-09
 */
function mfields_taxonomy_description_rows( $string, $column_name, $term ) {
	$description = '';
	if ( 'short_description' == $column_name ) {
		global $taxonomy;
		$description = term_description( $term, $taxonomy );
	}
	return $string . mfields_shorten( $description, 40 );
}


/**
 * Shorten a string to a given length.
 *
 * @param     string    The string to shorten.
 * @return    int       Number of characters allowed in $string. Defaults value is 23.
 * @return    string    Text to append to the shortened string.
 *
 * @since     2010-05-31
 * @alter     2011-01-09
 */
function mfields_shorten( $string, $length = 23, $append = '...' ) {	
	$string = strip_tags( $string );
	$string = trim( $string );
	$string = html_entity_decode( $string, ENT_QUOTES, 'UTF-8' );
	$string = rtrim( $string, '-' );
	return ( strlen( $string ) > absint( $length ) ) ? substr_replace( $string, $append, absint( $length ) ) : $string;
}

Hope someone finds this useful.

7 Comments Comments are closed

  1. Saad Bassi June 1, 2010 at 5:51 am

    Brilliant article boy. Thanks for writing that up.:)

  2. Michael Fields June 2, 2010 at 5:58 am

    Thanks Saad!

  3. Peeter Marvet July 21, 2010 at 7:00 pm

    thanks for code :-)

    but… looks like mfields_taxonomy_remove_admin_term_description (first snippet, also start of second) should be also called using “foreach( $wp_taxonomies …” to remove it from all taxonomies (seems that it currently fails to remove descriptions even from categories). also, the first snippet sets the Short description header which is presumably unneccessary…

  4. Michael Fields July 21, 2010 at 7:08 pm

    Peeter,
    No problem. You are right on both accounts! This code was written before 3.0 was officially released and they changed the hook names for accessing table columns a few times during the cycle. This code is a bit outdated at this point. I have rolled this into a plugin that I need to get around to releasing. It allows all of this functionality + the ability to remove the post count and slug as well – which some people may or may not need to see all of the time. Will update this post when I get a free minute. Thanks for the accuracy check!
    -Mike

  5. Michael June 3, 2011 at 1:33 pm

    Great source of help Michael. I am trying to echo onto a template page the taxonomy name from the same form- the default field. I can not find ANYWHERE where I can find the name to use to echo its value out.I have looked in the wp-includes jungle with out success and have tried term-name,term-title.even term-slug,tag_name all without success. Every tutorial on the subject just talks about ADDING fields. I just want to echo out the name default field from this form !!! – Can you point me in the right direction? Thank you

  6. Michael Fields June 3, 2011 at 1:46 pm

    Best case scenario is the following code:

    <?php
    	$taxonomy = get_taxonomy( get_query_var( 'taxonomy' ) );
    	$taxonomy_name = 'Taxonomy';
    	if ( isset( $taxonomy->labels->singular_name ) ) {
    		$taxonomy_name = $taxonomy->labels->singular_name;
    	}
    	print esc_html( $taxonomy_name );
    ?>
    

    Works in term archive templates like taxonomy.php and the like.

  7. Michael June 3, 2011 at 4:11 pm

    Thanks Michael – a great help to keep me moving forward on a busy day :)
    The plugin for this post worked a treat and I rated it 5 :) – great work
    If anyone passing through needs it – http://wordpress.org/extend/plugins/taxonomy-short-description

Fork me on GitHub