From 1af8c3608689989eb06a8b53cfa9f07a2362ac65 Mon Sep 17 00:00:00 2001 From: Andrew Nacin Date: Sat, 8 Sep 2012 03:18:36 +0000 Subject: [PATCH] Allow easy registration of taxonomy columns on post (and custom post type) list table screens. To register a column for a list table, use the new manage_taxonomies_for_{$post_type}_columns filter. Introduces show_admin_column => true for register_taxonomy(), which automatically displays that column on all associated post types. props jtsternberg, SergeyBiryukov for initial patches. fixes #21240. git-svn-id: http://core.svn.wordpress.org/trunk@21788 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- .../includes/class-wp-posts-list-table.php | 104 ++++++++++-------- wp-includes/taxonomy.php | 2 + 2 files changed, 59 insertions(+), 47 deletions(-) diff --git a/wp-admin/includes/class-wp-posts-list-table.php b/wp-admin/includes/class-wp-posts-list-table.php index e7e15b0642..723f6ea6fc 100644 --- a/wp-admin/includes/class-wp-posts-list-table.php +++ b/wp-admin/includes/class-wp-posts-list-table.php @@ -269,11 +269,24 @@ class WP_Posts_List_Table extends WP_List_Table { if ( post_type_supports( $post_type, 'author' ) ) $posts_columns['author'] = __( 'Author' ); - if ( empty( $post_type ) || is_object_in_taxonomy( $post_type, 'category' ) ) - $posts_columns['categories'] = __( 'Categories' ); + $taxonomies = array(); - if ( empty( $post_type ) || is_object_in_taxonomy( $post_type, 'post_tag' ) ) - $posts_columns['tags'] = __( 'Tags' ); + $taxonomies = get_object_taxonomies( $post_type, 'objects' ); + $taxonomies = wp_filter_object_list( $taxonomies, array( 'show_admin_column' => true ), 'and', 'name' ); + + $taxonomies = apply_filters( "manage_taxonomies_for_{$post_type}_columns", $taxonomies, $post_type ); + $taxonomies = array_filter( $taxonomies, 'taxonomy_exists' ); + + foreach ( $taxonomies as $taxonomy ) { + if ( 'category' == $taxonomy ) + $column_key = 'categories'; + elseif ( 'post_tag' == $taxonomy ) + $column_key = 'tags'; + else + $column_key = 'taxonomy-' . $taxonomy; + + $posts_columns[ $column_key ] = get_taxonomy( $taxonomy )->labels->name; + } $post_status = !empty( $_REQUEST['post_status'] ) ? $_REQUEST['post_status'] : 'all'; if ( post_type_supports( $post_type, 'comments' ) && !in_array( $post_status, array( 'pending', 'draft', 'future' ) ) ) @@ -602,48 +615,6 @@ class WP_Posts_List_Table extends WP_List_Table { echo ''; break; - case 'categories': - ?> - >%s', - esc_url( add_query_arg( array( 'post_type' => $post->post_type, 'category_name' => $c->slug ), 'edit.php' ) ), - esc_html( sanitize_term_field( 'name', $c->name, $c->term_id, 'category', 'display' ) ) - ); - } - /* translators: used between list items, there is a space after the comma */ - echo join( __( ', ' ), $out ); - } else { - _e( 'Uncategorized' ); - } - ?> - - >ID ); - if ( !empty( $tags ) ) { - $out = array(); - foreach ( $tags as $c ) { - $out[] = sprintf( '%s', - esc_url( add_query_arg( array( 'post_type' => $post->post_type, 'tag' => $c->slug ), 'edit.php' ) ), - esc_html( sanitize_term_field( 'name', $c->name, $c->term_id, 'tag', 'display' ) ) - ); - } - /* translators: used between list items, there is a space after the comma */ - echo join( __( ', ' ), $out ); - } else { - _e( 'No Tags' ); - } - ?> - >
@@ -668,6 +639,45 @@ class WP_Posts_List_Table extends WP_List_Table { break; default: + if ( 'categories' == $column_name ) + $taxonomy = 'category'; + elseif ( 'tags' == $column_name ) + $taxonomy = 'post_tag'; + elseif ( 0 === strpos( $column_name, 'taxonomy-' ) ) + $taxonomy = substr( $column_name, 9 ); + + if ( ! empty( $taxonomy ) ) { + $taxonomy_object = get_taxonomy( $taxonomy ); + echo ''; + if ( $terms = get_the_terms( $post->ID, $taxonomy ) ) { + $out = array(); + foreach ( $terms as $t ) { + $posts_in_term_qv = array(); + if ( 'post' != $post->post_type ) + $posts_in_term_qv['post_type'] = $post->post_type; + if ( $taxonomy_object->query_var ) { + $posts_in_term_qv[ $taxonomy_object->query_var ] = $t->slug; + } else { + $posts_in_term_qv['taxonomy'] = $taxonomy; + $posts_in_term_qv['term'] = $t->slug; + } + + $out[] = sprintf( '%s', + esc_url( add_query_arg( $posts_in_term_qv, 'edit.php' ) ), + esc_html( sanitize_term_field( 'name', $t->name, $t->term_id, $taxonomy, 'display' ) ) + ); + } + /* translators: used between list items, there is a space after the comma */ + echo join( __( ', ' ), $out ); + } else { + if ( 'category' == $taxonomy ) + echo __( 'Uncategorized' ); + else + echo '—'; + } + echo ''; + break; + } ?> >post_type ) ) @@ -678,8 +688,8 @@ class WP_Posts_List_Table extends WP_List_Table { ?> $rewrite['category'], 'public' => true, 'show_ui' => true, + 'show_admin_column' => true, '_builtin' => true, ) ); @@ -56,6 +57,7 @@ function create_initial_taxonomies() { 'rewrite' => $rewrite['post_tag'], 'public' => true, 'show_ui' => true, + 'show_admin_column' => true, '_builtin' => true, ) );