Advertisement
qlstudio

add_admin_columns to CPT helper class

Nov 4th, 2012
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.09 KB | None | 0 0
  1. // http://wp.tutsplus.com/tutorials/creative-coding/custom-post-type-helper-class/
  2.  
  3. // call to the class ##
  4. $cpt_tree = new Custom_Post_Type( "tree" ); // singular custom post type title ##
  5.  
  6. ....
  7.  
  8. $cpt_tree_admin_column_args = array ( // tree admin colums args ##
  9.     'tag'               => true,
  10.     'category'          => true,
  11. ); 
  12. $cpt_tree->add_admin_columns( $cpt_tree_admin_column_args ); // add admin columns ##
  13.  
  14.  
  15. class Custom_Post_Type {
  16.  
  17. .....
  18.  
  19. // this code is added inside the main Custom Post Type class
  20. public function add_admin_columns( $args )
  21.     {
  22.        
  23.         // We need to know the Post Type name again ##
  24.         $name = strtolower( $this->post_type_name );
  25.        
  26.         // Columns args ##
  27.         $column_args = $args;
  28.        
  29.         // Default arguments, overwitten with the given arguments
  30.         $args = array_merge(
  31.  
  32.             // Default
  33.             array(
  34.                 'tag'       => true,
  35.                 'category'  => false,
  36.             ),
  37.  
  38.             // Given
  39.             $column_args
  40.  
  41.         );
  42.        
  43.         add_filter( 'manage_edit-'.$name.'_columns',
  44.             function() use( $name, $args )
  45.             {                      
  46.                 $new_columns['cb'] = '<input type="checkbox" />';
  47.                 $new_columns['title'] = _x(''.ucwords($name).'', 'column name');
  48.                 $new_columns['author'] = __('Author');
  49.                 if ( $args["category"] === true ) { // add category column ##
  50.                     $new_columns[''.$name.'_category'] = __('Categories');
  51.                 }
  52.                 if ( $args["tag"] === true ) { // add tag column ##
  53.                     $new_columns[''.$name.'_tag'] = __('Tags');
  54.                 }
  55.                 $new_columns['thumbnail'] = __('Thumbnail');
  56.                 $new_columns['date'] = _x('Date', 'column name');
  57.                 return $new_columns;
  58.             }
  59.         , 10, 2 );
  60.        
  61.    
  62.         if ( !function_exists('manage_cpt_columns') ) {
  63.         add_action('manage_'.$name.'_posts_custom_column', 'manage_cpt_columns', 10, 2);
  64.         function manage_cpt_columns( $column_name, $id ) {
  65.            
  66.             global $wpdb, $name, $post;
  67.             #echo ' - C: '.$column_name;
  68.            $name = get_post_type( $post );
  69.             #echo 'N: '.$name;  
  70.            
  71.             switch ( $column_name ) {
  72.            
  73.             case $name.'_tag':
  74.                
  75.                 $tags = get_the_terms( $id, ''.$name.'_tag' ); // cpt_tag is the custom taxonomy slug
  76.                 if ( !empty( $tags ) ) {
  77.                     $out = array();
  78.                     foreach ( $tags as $c )
  79.                         $out[] = "<a href='edit-tags.php?action=edit&taxonomy=".$name."_tag&tag_ID=$c->term_id&post_type=".$name."'> " . esc_html(sanitize_term_field('name', $c->name, $c->term_id, ''.$name.'_tag', 'display')) . "</a>";
  80.                     echo join( ', ', $out );
  81.                 } else {
  82.                     echo '';  //No Taxonomy term defined
  83.                 }      
  84.  
  85.                 break;
  86.  
  87.             case $name.'_category':
  88.  
  89.                 $tags = get_the_terms( $id, ''.$name.'_category' ); // cpt_category is the custom taxonomy slug
  90.                 if ( !empty( $tags ) ) {
  91.                     $out = array();
  92.                     foreach ( $tags as $c )
  93.                         $out[] = "<a href='edit-tags.php?action=edit&taxonomy=".$name."_category&tag_ID=$c->term_id&post_type=".$name."'> " . esc_html(sanitize_term_field('name', $c->name, $c->term_id, ''.$name.'_category', 'display')) . "</a>";
  94.                     echo join( ', ', $out );
  95.                 } else {
  96.                     echo '';  //No Taxonomy term defined
  97.                 }      
  98.  
  99.                 break;
  100.  
  101.             case 'thumbnail':
  102.  
  103.                 if( function_exists('the_post_thumbnail') ) {
  104.                     #echo the_post_thumbnail( 'admin-list-thumb' );
  105.                } else {
  106.                     echo '';
  107.                 }
  108.                 break;
  109.  
  110.                 break;
  111.  
  112.             default:
  113.                 break;
  114.             } // end switch
  115.     }}
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement