Skip to main content
added 200 characters in body
Source Link
Nicolai Grossherr
  • 18.9k
  • 8
  • 64
  • 109
  • Extend the class WP_List_Table yourself, like WP_Terms_List_Table it does;
  • CreateIn 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.
  • Extend the class WP_List_Table yourself, like WP_Terms_List_Table it does;
  • 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.
  • 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.
added 3020 characters in body
Source Link
Nicolai Grossherr
  • 18.9k
  • 8
  • 64
  • 109

Disclaimer: Haven't done this scenario in a while, so there might have been some changes I missed Ok, besides I could never had the knowledge about the right approachrevisited this and revised my answer..


 

But maybe this is helpful anyway..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`](http://hookr.io/3.9.1/filters/apply_filters/manage_screen_taxonomy_custom_column/) and [`manage_{$this->screen->id}_columns`](http://hookr.io/3.9.1/filters/apply_filters/manage_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;
  • 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.

Firstly,I'm sure the above should solve your problem. Note that I remember it to be way easier to removedid the default column and replacing it with a custom columnexamples for category edit page, so it might notbut how to do this for custom taxonomies should be too much ofapparent. Depending on what is needed a hassle after allsimple or elaborate approach can be chosen. In you case the first one should be just fine.

Secondly, I think the hook you are (trying) to use won't/can't work forLast but not least the purpose you are aiming for, simply because its only occurrence is in class-wp-posts-list-table.php, which nomen est omenfollowing function is usedhandy to show the post list,get the right place should be class-wp-terms-list-table.php.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 );
}

Disclaimer: Haven't done this scenario in a while, so there might have been some changes I missed, besides I could never had the knowledge about the right approach.


 

But maybe this is helpful anyway..

Firstly, I remember it to be way easier to remove the default column and replacing it with a custom column, so it might not be too much of a hassle after all.

Secondly, I think 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.

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`](http://hookr.io/3.9.1/filters/apply_filters/manage_screen_taxonomy_custom_column/) and [`manage_{$this->screen->id}_columns`](http://hookr.io/3.9.1/filters/apply_filters/manage_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;
  • 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.

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 );
}
Source Link
Nicolai Grossherr
  • 18.9k
  • 8
  • 64
  • 109

Disclaimer: Haven't done this scenario in a while, so there might have been some changes I missed, besides I could never had the knowledge about the right approach.


But maybe this is helpful anyway..

Firstly, I remember it to be way easier to remove the default column and replacing it with a custom column, so it might not be too much of a hassle after all.

Secondly, I think 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.