Adding new taxonomy and admin columns
authorSteve Sutton <steve@gaslightmedia.com>
Mon, 21 Dec 2015 19:12:26 +0000 (14:12 -0500)
committerSteve Sutton <steve@gaslightmedia.com>
Mon, 21 Dec 2015 19:12:26 +0000 (14:12 -0500)
Add the category and tags columns to list view.
Add the new tags taxonomy.

index.php

index db01542..cec3482 100644 (file)
--- a/index.php
+++ b/index.php
@@ -51,7 +51,7 @@ function michsci_custom_post_type() {
         'label'                 => __( 'Product', 'michsci' ),
         'description'           => __( 'Custom Products', 'michsci' ),
         'labels'                => $labels,
-        'supports'              => array( 'title', 'editor', 'excerpt', 'thumbnail', 'revisions', 'page-attributes' ),
+        'supports'              => array( 'title', 'editor', 'excerpt', 'thumbnail', 'revisions' ),
         'hierarchical'          => true,
         'public'                => true,
         'show_ui'               => true,
@@ -76,6 +76,7 @@ function michsci_custom_post_type() {
 function michsci_taxonomies()
 {
 
+    // Product Categories
     $labels = array(
         'name'              => _x( 'Category', 'taxonomy general name'),
         'singular_name'     => _x( 'Category', 'taxonomy singular name'),
@@ -98,8 +99,8 @@ function michsci_taxonomies()
     );
 
     $args = array(
-        'label'                => __('Product Category'),
-        'rewrite'              => array( 'slug' => 'micategory' ),
+        'label'                => __('Categories'),
+        'rewrite'              => array( 'slug' => 'mitag' ),
         'capabilities'         => $capabilities,
         'public '              => true,
         'show_in_quick_edit'   => true,
@@ -113,6 +114,43 @@ function michsci_taxonomies()
         $args
     );
 
+    // Product Tags
+    $labels = array(
+        'name'              => _x( 'Tag', 'taxonomy general name'),
+        'singular_name'     => _x( 'Tag', 'taxonomy singular name'),
+        'search_items'      => __( 'Search Tags'),
+        'all_items'         => __( 'All Tags'),
+        'parent_item'       => __( 'Parent Tag'),
+        'parent_item_colon' => __( 'Parent Tag'),
+        'edit_item'         => __( 'Edit Tag'),
+        'update_item'       => __( 'Update Tag'),
+        'add_new_item'      => __( 'Add New Tag'),
+        'new_item_name'     => __( 'New Tag Name'),
+        'menu_name'         => __( 'Product Tag'),
+    );
+
+    $capabilities = array(
+        'manage_terms' => 'manage_categories',
+        'edit_terms'   => 'manage_categories',
+        'delete_terms' => 'manage_categories',
+        'assign_terms' => 'edit_posts',
+    );
+
+    $args = array(
+        'label'                => __('Tags'),
+        'rewrite'              => array( 'slug' => 'mitag' ),
+        'capabilities'         => $capabilities,
+        'public '              => true,
+        'show_in_quick_edit'   => true,
+        'show_in_admin_column' => true,
+        'hierarchical'         => false
+    );
+
+     register_taxonomy(
+        'michsci_tag',
+        'miproduct',
+        $args
+    );
 }
 
 function michsci_taxonomy_filter_restrict_manage_posts()
@@ -166,19 +204,68 @@ add_filter( 'parse_query', 'michsci_taxonomy_filter_post_type_request' );
 add_action( 'init', 'michsci_taxonomies' );
 add_action( 'init', 'michsci_custom_post_type' );
 
+function michsci_set_admin_columns()
+{
+    add_filter( 'manage_edit-miproduct_columns', 'michsci_admin_edit_columns' );
+    add_action( 'manage_miproduct_posts_custom_column', 'michsci_admin_custom_columns' );
+}
+
+add_action( 'admin_init', 'michsci_set_admin_columns' );
+
+function michsci_admin_edit_columns($columns)
+{
+    $columns = array(
+        'cb'                  => '<input type="checkbox">',
+        'title'               => 'Product Title',
+        'michproduct_col_cat' => 'Category',
+        'michproduct_col_tag' => 'Tag'
+    );
+    return $columns;
+}
+
+function michsci_admin_custom_columns($column)
+{
+    global $post;
+
+    switch ($column) {
+    case 'michproduct_col_cat':
+        $product_cats      = get_the_terms( $post->ID, 'michsci_category' );
+        $product_cats_html = array();
+        if ( $product_cats ) {
+            foreach ( $product_cats as $product_cat ) {
+                array_push( $product_cats_html, $product_cat->name );
+            }
+            echo implode( $product_cats_html, ', ' );
+        } else {
+             _e( 'None', 'themeforce' );
+        }
+        break;
+    case 'michproduct_col_tag':
+        $product_tags      = get_the_terms( $post->ID, 'michsci_tag' );
+        $product_tags_html = array();
+        if ( $product_tags ) {
+            foreach ( $product_tags as $product_tag ) {
+                array_push( $product_tags_html, $product_tag->name );
+            }
+            echo implode($product_tags_html, ', ' );
+        } else {
+             _e( 'None', 'themeforce' );
+        }
+        break;
+    }
+}
+
 function michsci_shortcode($atts)
 {
     extract( shortcode_atts( array( 'limit' => '10' ), $atts ) );
     ob_start();
 
-    echo '<form id="catSubmit" action=""><div class="row catSearch"><div id="viewBtns" class="12-small medium-3 columns"> <button id="grid" type="button">Grid</button> <button id="list"type="button">list</button> 
+    echo '<form id="catSubmit" action=""><div class="row catSearch"><div id="viewBtns" class="12-small medium-3 columns"> <button id="grid" type="button">Grid</button> <button id="list"type="button">list</button>
                 </div><div id="catDropdown" class="small-12 medium-6 columns">';
     echo michsci_get_terms_dropdown();
     echo '</div><div id="catSearchBtn" class="small-12 medium-3 columns"><input type="submit" value="search" class="button"></div></div>';
     echo '</form>';
-    
-  
-    
+
     if ( $product_id = filter_var( $_REQUEST['product'], FILTER_VALIDATE_INT ) ) {
         michsci_show_product($product_id);
     } else if ( $cat_id = filter_var( $_REQUEST['category'], FILTER_VALIDATE_INT ) ) {
@@ -196,7 +283,7 @@ function michsci_shortcode($atts)
 function michsci_get_terms_dropdown()
 {
     $taxonomyName = "michsci_category";
-    $parent_terms = get_terms($taxonomyName, array('parent' => 0, 'orderby' => 'name', 'hide_empty' => false)); 
+    $parent_terms = get_terms($taxonomyName, array('parent' => 0, 'orderby' => 'name', 'hide_empty' => false));
     $output ="<select id='categoryDropdown' name=\"michsci_category\"><option value=\"\">Select Category</option>";
 
         // get parent categories
@@ -224,7 +311,7 @@ function michsci_get_terms_dropdown()
 //                    $output .= '>' . "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp" .  $grandchild->name . '</option>';
 //                }
             }
-        } 
+        }
     $output .="</select>";
     return $output;
 }
@@ -335,3 +422,4 @@ function michsci_show_category($cat_id)
     return;
 }
 add_shortcode( 'michproducts', 'michsci_shortcode' );
+