Want more features on Pastebin? Sign Up, it's FREE!
Guest

Product CPT

By: a guest on Jul 4th, 2013  |  syntax: PHP  |  size: 5.33 KB  |  views: 40  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. add_action('init', 'wlg_cstm_register');  
  3.    
  4.  function wlg_cstm_register() {  
  5.        
  6.         $product_labels = array(
  7.     'name' => _x('Products', 'post type general name'),
  8.     'singular_name' => _x('product', 'post type singular name'),
  9.     'add_new' => _x('Add New', 'product'),
  10.     'add_new_item' => __('Add New Products'),
  11.     'edit_item' => __('Edit Products'),
  12.     'new_item' => __('New product'),
  13.     'view_item' => __('View Products'),
  14.     'search_items' => __('Search Products'),
  15.     'not_found' =>  __('No Products found'),
  16.     'not_found_in_trash' => __('No Products found in Trash'),
  17.     'parent_item_colon' => ''
  18. );
  19.  
  20.         $product_args = array(  
  21.          'labels' => $product_labels,  
  22.          'public' => true,  
  23.          'show_ui' => true,  
  24.          'capability_type' => 'post',  
  25.          'hierarchical' => false,  
  26.          'rewrite' => true,
  27.          'supports' => array('title', 'editor', 'thumbnail'),
  28.                 'taxonomies' => array( '' ),
  29.         'menu_icon' => get_bloginfo('template_directory') . '/images/products-icon.png',  // Icon Path
  30.         'has_archive' => true            
  31.         );
  32.  
  33.  
  34.      register_post_type( 'product' , $product_args );
  35.          register_taxonomy( 'brands', 'product', array( 'hierarchical' => true, 'label' => __('Product Brands'), 'query_var' => 'brands' ) );
  36.  }
  37.  
  38.  add_action("admin_init", "admin_init");
  39. add_action('save_post', 'save_points', 1, 2);
  40. function admin_init(){
  41.   add_meta_box("productInfo-meta", "Product Details", "product_meta_options", 'product', "normal", "high");
  42. }
  43.  
  44. function product_meta_options(){
  45.   global $post;
  46.   $custom = get_post_custom($post->ID);
  47.   $category = (!empty($custom["_category"][0])) ? $custom["_category"][0] : '';
  48.   $brand = (!empty($custom["_brand"][0])) ? $custom["_brand"][0]  : '';
  49.   $id = (!empty($custom["_productid"][0])) ? $custom["_productid"][0] : '';
  50.   $features = (!empty($custom["_features"][0])) ? $custom["_features"][0]  : '';
  51.   $holds = (!empty($custom["_holds"][0])) ? $custom["_holds"][0]  : '';
  52. ?>
  53. <table>
  54.     <tr>
  55.         <td>Category</td>
  56.         <td> <input type="text" size="100" name="category" value="<?php echo $category; ?>" /> </td>
  57.     </tr>
  58.     <tr>
  59.         <td>Brand</td>
  60.         <td> <input type="text" size="100" name="brand" value="<?php echo $brand; ?>" /> </td>
  61.     </tr>
  62.         <tr>
  63.         <td>Product ID</td>
  64.         <td> <input type="text" size="100" name="productid" value="<?php echo $productid; ?>" /> </td>
  65.     </tr>
  66.     <tr>
  67.         <td>Features</td>
  68.         <td><?php wp_editor( $features, 'features', $settings = array('textarea_rows'=>20) ); ?></td>
  69.     </tr>
  70.     <tr>
  71.         <td>Holds</td>
  72.         <td><?php wp_editor( $holds, 'holds', $settings = array('textarea_rows'=>20) ); ?></td>
  73.     </tr>
  74. </table>
  75.  
  76. <?php
  77. }
  78.  
  79. function save_points($postid,$post){
  80.   global $_POST;
  81.   // set the ID to the parent post, not the revision
  82.   $postid = (wp_is_post_revision( $postid )) ? wp_is_post_revision( $post ) : $postid;
  83.   $post_type = get_post_type( $postid );
  84.   if ('product' == $post_type) {
  85.     update_post_meta($postid, "_category", $_POST["category"]);
  86.     update_post_meta($postid, "_brand", $_POST["brand"]);
  87.         update_post_meta($postid, "_productid", $_POST["productid"]);
  88.     update_post_meta($postid, "_features", $_POST["features"]);
  89.     update_post_meta($postid, "_holds", $_POST["holds"]); // save the data
  90.   }
  91. }
  92.  
  93.  
  94.  
  95. function todo_restrict_manage_posts() {
  96.     global $typenow;
  97.     $args=array( 'public' => true, '_builtin' => false );
  98.     $post_types = get_post_types($args);
  99.     if ( in_array($typenow, $post_types) ) {
  100.     $filters = get_object_taxonomies($typenow);
  101.         foreach ($filters as $tax_slug) {
  102.             $tax_obj = get_taxonomy($tax_slug);
  103.             wp_dropdown_categories(array(
  104.                 'show_option_all' => __('Show All '.$tax_obj->label ),
  105.                 'taxonomy' => $tax_slug,
  106.                 'name' => $tax_obj->name,
  107.                 'orderby' => 'term_order',
  108.                 'selected' => $_GET[$tax_obj->query_var],
  109.                 'hierarchical' => $tax_obj->hierarchical,
  110.                 'show_count' => false,
  111.                 'hide_empty' => true
  112.             ));
  113.         }
  114.     }
  115. }
  116. function todo_convert_restrict($query) {
  117.     global $pagenow;
  118.     global $typenow;
  119.     if ($pagenow=='edit.php') {
  120.         $filters = get_object_taxonomies($typenow);
  121.         foreach ($filters as $tax_slug) {
  122.             $var = &$query->query_vars[$tax_slug];
  123.             if ( isset($var) ) {
  124.                 $term = get_term_by('id',$var,$tax_slug);
  125.                 $var = $term->slug;
  126.             }
  127.         }
  128.     }
  129.     return $query;
  130. }
  131. add_action( 'restrict_manage_posts', 'todo_restrict_manage_posts' );
  132. add_filter('parse_query','todo_convert_restrict');
  133.  
  134.  
  135. add_filter( 'manage_edit-product_columns', 'my_columns' );
  136. function my_columns( $columns ) {
  137.     $columns['brands'] = 'Brands';
  138.     $columns['category'] = 'Category';
  139.     unset( $columns['comments'] );
  140.     return $columns;
  141. }
  142. add_action( 'manage_posts_custom_column', 'populate_columns' );
  143. function populate_columns( $column ) {
  144.     if ( 'brands' == $column ) {
  145.         $brand = esc_html( get_post_meta( get_the_ID(), 'brand', true ) );
  146.         echo $brand;
  147.     }
  148.     elseif ( 'category' == $column ) {
  149.         $category = get_post_meta( get_the_ID(), 'category', true );
  150.         echo $category;
  151.     }
  152. }
clone this paste RAW Paste Data