public
Last active

'show_admin_column' => true

  • Download Gist
taxonomy-columns.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
<?php
 
add_action( 'registered_taxonomy', array( 'APP_Tax_Admin_Column', 'register_column' ), 10, 3 );
 
 
/**
* Generates a column with the associated terms,
* for any taxonomy with 'show_admin_column' => true
*/
class APP_Tax_Admin_Column {
 
static function register_column( $taxonomy, $object_type, $args ) {
if ( !isset( $args['show_admin_column'] ) )
return;
 
$instance = new APP_Tax_Admin_Column;
$instance->taxonomy = get_taxonomy( $taxonomy );
 
add_filter( 'manage_' . $object_type . '_posts_columns', array( $instance, 'column_headers' ) );
add_action( 'manage_' . $object_type . '_posts_custom_column', array( $instance, 'column_content' ), 10, 2 );
}
 
function column_headers( $columns ) {
$this->column_name = 'tax_' . $this->taxonomy->name;
 
$columns[ $this->column_name ] = $this->taxonomy->labels->singular_name;
 
return $columns;
}
 
function column_content( $column, $post_id ) {
if ( $column != $this->column_name )
return;
 
$terms = get_the_terms( $post_id, $this->taxonomy->name );
 
if ( !empty( $terms ) ) {
$out = array();
foreach ( $terms as $c ) {
$url = add_query_arg( array(
'post_type' => get_current_screen()->post_type,
$this->taxonomy->query_var => $c->slug
), 'edit.php' );
 
$out[] = html( 'a', array( 'href' => $url ),
esc_html( sanitize_term_field( 'name', $c->name, $c->term_id, 'tag', 'display' ) )
);
}
echo implode( ', ', $out );
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.