Advertisement
Guest User

Untitled

a guest
Aug 21st, 2013
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 16.19 KB | None | 0 0
  1. /************* Product Post Type *****************/
  2.  
  3. // let's create the function for the (custom) product type
  4. function product_post() {
  5.     // creating (registering) the (custom) product type
  6.     register_post_type( 'product', /* (http://codex.wordpress.org/Function_Reference/register_post_type) */
  7.         // let's now add all the options for this post type
  8.         array('labels' => array(
  9.             'name' => __('Albums & Merch', 'bonestheme'), /* This is the Title of the Group */
  10.             'singular_name' => __('Product', 'bonestheme'), /* This is the individual type */
  11.             'all_items' => __('All Products', 'bonestheme'), /* the all items menu item */
  12.             'add_new' => __('Add New Product', 'bonestheme'), /* The add new menu item */
  13.             'add_new_item' => __('Add New Product', 'bonestheme'), /* Add New Display Title */
  14.             'edit' => __( 'Edit', 'bonestheme' ), /* Edit Dialog */
  15.             'edit_item' => __('Edit Product', 'bonestheme'), /* Edit Display Title */
  16.             'new_item' => __('New Product', 'bonestheme'), /* New Display Title */
  17.             'view_item' => __('View Product', 'bonestheme'), /* View Display Title */
  18.             'search_items' => __('Search Product', 'bonestheme'), /* Search Custom Type Title */
  19.             'not_found' =>  __('Nothing found in the Database. Freakin\' nutting, man. No Products AT ALL.', 'bonestheme'), /* This displays if there are no entries yet */
  20.             'not_found_in_trash' => __('Nothing found in Trash but a cockroach', 'bonestheme'), /* This displays if there is nothing in the trash */
  21.             'parent_item_colon' => ''
  22.             ), /* end of arrays */
  23.             'description' => __( 'This is the post type for products', 'bonestheme' ), /* Custom Type Description */
  24.             'public' => true,
  25.             'publicly_queryable' => true,
  26.             'exclude_from_search' => false,
  27.             'show_ui' => true,
  28.             'query_var' => true,
  29.             'menu_position' => 5, /* this is what order you want it to appear in on the left hand side menu */
  30.             'menu_icon' => get_stylesheet_directory_uri() . '/library/images/product-post-icon.png', /* the icon for the custom post type menu */
  31.             'rewrite'   => array( 'slug' => 'product', 'with_front' => false ), /* you can specify its url slug */
  32.             'has_archive' => 'product', /* you can rename the slug here */
  33.             'capability_type' => 'post',
  34.             'hierarchical' => false,
  35.             'taxonomies' => array( 'category'),
  36.             'register_meta_box_cb' => 'add_album_info_meta_box',
  37.             /* the next one is important, it tells what's enabled in the post editor
  38.                          i.e. 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'sticky') */
  39.             'supports' => array( 'title', 'editor', 'thumbnail', 'sticky')
  40.         ) /* end of options */
  41.     ); /* end of register post type */
  42.    
  43.     /* this adds your post categories to your custom post type */
  44.     register_taxonomy_for_object_type('category', 'product');
  45.     /* this adds your post tags to your custom post type */
  46.     register_taxonomy_for_object_type('post_tag', 'product');
  47.  
  48. }
  49.  
  50. // Remove default category and tag box
  51. function remove_product_default_fields() {
  52.     remove_meta_box( 'categorydiv' , 'product' , 'side' );          // Category Box
  53. //  remove_meta_box( 'tagsdiv-post_tag' , 'product' , 'side' );     // Tag Box (uncomment to hide)
  54. }
  55. add_action( 'admin_menu' , 'remove_product_default_fields' );
  56.  
  57. // Save Product to "Products" category
  58. function save_to_product_category($post_ID) {
  59.  
  60.     //PARENT CATEGORY
  61.     //Create parent category based on Singular Name
  62.         $get_cat_name = get_post_type_object( 'product' );
  63.         $cat_name = $get_cat_name->labels->singular_name;
  64.         if ( ! category_exists($cat_name) ) {
  65.             wp_create_category( $cat_name );
  66.         }
  67.  
  68.     //Assign post to parent category
  69.         global $wpdb;
  70.         if(wp_is_post_autosave($post_ID) || wp_is_post_revision($post_ID)) {
  71.           return $post_ID;
  72.         }
  73.         $ccat_id = intval( get_cat_ID( $cat_name ) );
  74.         wp_set_object_terms( $post_ID, $ccat_id, 'category' );
  75.  
  76.     //CHILD CATEGORY
  77.     //Create child category based on Format
  78.         $get_child_cat_name = $post_meta_data['album_info_format'][0];
  79.         foreach ($get_child_cat_name as $child_cat_string) {
  80.                                                                     switch($child_cat_string) {
  81.                                                                         //Cassette
  82.                                                                             case 'mc':
  83.                                                                             $child_cat_name = array('Record','Cassette');
  84.                                                                             break;
  85.                                                                         //CD
  86.                                                                             case 'cd':
  87.                                                                             $child_cat_name = array('Record','CD');
  88.                                                                             break;
  89.                                                                         //Vinyl
  90.                                                                             case 'lp':
  91.                                                                             $child_cat_name = array('Record','Vinyl');
  92.                                                                             break;
  93.                                                                         //Digital
  94.                                                                             case 'mp':
  95.                                                                             $child_cat_name = array('Record','Digital');
  96.                                                                             break;
  97.                                                                         //Magazine
  98.                                                                             case 'zine':
  99.                                                                             $child_cat_name = array('Print','Magazine');
  100.                                                                             break;
  101.                                                                         //Print
  102.                                                                             case 'print':
  103.                                                                             $child_cat_name = array('Print');
  104.                                                                             break;
  105.                                                                         //Video
  106.                                                                             case 'video':
  107.                                                                             $child_cat_name = array('Video');
  108.                                                                             break;
  109.                                                                         //Apparel
  110.                                                                             case 'apparel':
  111.                                                                             $child_cat_name = array('Apparel');
  112.                                                                             break;
  113.                                                                         //Textile
  114.                                                                             case 'textile':
  115.                                                                             $child_cat_name = array('Textile');
  116.                                                                             break;
  117.                                                                         //Merch
  118.                                                                             case 'merch':
  119.                                                                             $child_cat_name = array('Merchandise');
  120.                                                                             break;
  121.                                                                         //Other
  122.                                                                             case 'other':
  123.                                                                             $child_cat_name = array('Other Formats');
  124.                                                                             break;
  125.                                                                     }
  126.                                                             }
  127.         foreach ($child_cat_name as $child_cat_name_string) {
  128.             if ( ! category_exists($child_cat_name_string) ) {
  129.                 wp_create_category( $child_cat_name_string, $ccat_id );
  130.             }
  131.         }
  132.  
  133.     //Assign post to child category
  134.         global $wpdb;
  135.         if(wp_is_post_autosave($post_ID) || wp_is_post_revision($post_ID)) {
  136.           return $post_ID;
  137.         }
  138.         foreach ($child_cat_name as $child_cat_cat_string) {
  139.             $child_ccat_id = intval( get_cat_ID( $child_cat_cat_string ) );
  140.             wp_set_object_terms( $post_ID, $child_ccat_id, 'category' );
  141.         }
  142.  
  143. }
  144.  
  145. add_action('publish_product', 'save_to_product_category');
  146.  
  147.  
  148.  
  149. // adding the (custom) product function to the Wordpress init
  150. add_action( 'init', 'product_post');
  151.    
  152.  
  153. /************* Album Info Meta Box *****************/
  154.  
  155. // Add the Album Info Meta Box
  156.  
  157. function add_album_info_meta_box() {
  158.     add_meta_box(
  159.         'album_info_meta_box',      // $id
  160.         'Album Info',               // $title
  161.         'show_album_info_meta_box', // $callback
  162.         'product',                  // $page
  163.         'normal',                   // $context
  164.         'high'                      // $priority   
  165.     );
  166. }
  167.  
  168. add_action('add_meta_boxes', 'add_album_info_meta_box');
  169.  
  170. // Field Array
  171. $prefix = 'album_info_';
  172. $album_info_meta_fields = array(
  173.     array (
  174.         'label' => 'Product Type',
  175.         'desc'  => 'Is this product music (cassette, album, mp3 etc.) or anything else (book, magazine, tote bag etc.)?',
  176.         'id'    => $prefix.'product_type',
  177.         'type'  => 'radio',
  178.         'options' => array (
  179.             'music' => array (
  180.                 'label' => 'Music',
  181.                 'value' => 'music'
  182.             ),
  183.             'else' => array (
  184.                 'label' => 'Something Else',
  185.                 'value' => 'else'
  186.             )
  187.         )
  188.     ),
  189.     array(
  190.         'label'=> 'Catalog Number',
  191.         'desc'  => 'Catalog Number; i.e. eco_024 (must be lowercase!)',
  192.         'id'    => $prefix.'catalog',
  193.         'type'  => 'catalog'
  194.     ),
  195.     array(
  196.         'label'=> 'Bandcamp ID',
  197.         'desc'  => 'Bandcamp ID of the album; as given i.e. in Bandcamp\'s embed code. ',
  198.         'id'    => $prefix.'bandcamp',
  199.         'type'  => 'text'
  200.     ),
  201.     array(
  202.         'label'=> 'Discogs.com URL',
  203.         'desc'  => 'If Discogs has an entry for the album, place URL here; i.e. http://www.discogs.com/Zoul-Pig-Nest/release/2524164',
  204.         'id'    => $prefix.'discogs',
  205.         'type'  => 'text'
  206.     ),
  207.     array(
  208.         'label'=> 'Artist',
  209.         'desc'  => 'The artist\'s name or "Various" if there is more than one artist, i.e. for compilations',
  210.         'id'    => $prefix.'artist',
  211.         'type'  => 'text'
  212.     ),
  213.     array(
  214.         'label'=> 'Title',
  215.         'desc'  => 'The album title',
  216.         'id'    => $prefix.'album',
  217.         'type'  => 'text'
  218.     ),
  219.     array(
  220.         'label'=> 'Specials',
  221.         'desc'  => 'Notes about special packaging etc.',
  222.         'id'    => $prefix.'special',
  223.         'type'  => 'textarea'
  224.     ),
  225.     array(
  226.         'label'=> 'Number of Tracks/Pages',
  227.         'desc'  => 'How many Tracks/Pages does the album/magazine have?<br>(Leave empty if this is not making sense for this specific product!)',
  228.         'id'    => $prefix.'tracks',
  229.         'type'  => 'number'
  230.     ),
  231.     array(
  232.         'label'=> 'Length',
  233.         'desc'  => 'How long is the album? dd:hh:mm:ss, i.e. 27:00',
  234.         'id'    => $prefix.'length',
  235.         'type'  => 'text'
  236.     ),
  237.     array(
  238.         'label'=> 'Copies',
  239.         'desc'  => 'How many copies?',
  240.         'id'    => $prefix.'copies',
  241.         'type'  => 'number'
  242.     ),
  243.     array(
  244.         'label' => 'Release Date',
  245.         'desc'  => 'Pick the day the item was released. yyyy-mm-dd',
  246.         'id'    => $prefix.'release_date',
  247.         'type'  => 'date'
  248.     ),
  249.     array (
  250.         'label' => 'Format',
  251.         'desc'  => 'In which format is this product available?<br>Examples:<br>1. For a cassette with an artist zine check "Cassette" and "Magazine".<br>2. For an album that can be purchased as a CD and/or downloaded on Bandcamp check "CD" and "Digital".<br>3. For a 12" that comes <u>in</u> a tote bag check "Vinyl" and "Textile".<br>4. For a tote bag that every fan girl urgently needs to have check "Merch".',
  252.         'id'    => $prefix.'format',
  253.         'type'  => 'checkbox_group',
  254.         'options' => array (
  255.             'mc' => array (
  256.                 'label' => 'Cassette',
  257.                 'value' => 'mc'
  258.             ),
  259.             'cd' => array (
  260.                 'label' => 'CD',
  261.                 'value' => 'cd'
  262.             ),
  263.             'lp' => array (
  264.                 'label' => 'Vinyl',
  265.                 'value' => 'lp'
  266.             ),
  267.             'mp' => array (
  268.                 'label' => 'Digital',
  269.                 'value' => 'mp'
  270.             ),
  271.             'zine' => array (
  272.                 'label' => 'Magazine',
  273.                 'value' => 'zine'
  274.             ),
  275.             'print' => array (
  276.                 'label' => 'Print',
  277.                 'value' => 'print'
  278.             ),
  279.             'video' => array (
  280.                 'label' => 'Video',
  281.                 'value' => 'video'
  282.             ),
  283.             'apparel' => array (
  284.                 'label' => 'Apparel',
  285.                 'value' => 'apparel'
  286.             ),
  287.             'textile' => array (
  288.                 'label' => 'Textile',
  289.                 'value' => 'textile'
  290.             ),
  291.             'merch' => array (
  292.                 'label' => 'Merchandise',
  293.                 'value' => 'merch'
  294.             ),
  295.             'other' => array (
  296.                 'label' => 'Other',
  297.                 'value' => 'other'
  298.             )
  299.         )
  300.     )
  301. );
  302.  
  303.  
  304. // The Callback
  305. function show_album_info_meta_box() {
  306. global $album_info_meta_fields, $post;
  307. // Use nonce for verification
  308. echo '<input type="hidden" name="album_info_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';
  309.    
  310.     // Begin the field table and loop
  311.     echo '<table class="form-table">';
  312.     foreach ($album_info_meta_fields as $field) {
  313.         // get value of this field if it exists for this post
  314.         $meta = get_post_meta($post->ID, $field['id'], true);
  315.         // begin a table row with
  316.         echo '<tr>
  317.                 <th><label for="'.$field['id'].'">'.$field['label'].'</label></th>
  318.                 <td>';
  319.                 switch($field['type']) {
  320.  
  321.                     // radio
  322.                     case 'radio':
  323.                     foreach ( $field['options'] as $option ) {
  324.                         echo '<input type="radio" name="'.$field['id'].'" id="'.$option['value'].'" value="'.$option['value'].'" ',$meta == $option['value'] ? ' checked="checked"' : '',' />
  325.                         <label for="'.$option['value'].'">'.$option['label'].'</label><br>';
  326.                     }
  327.                     echo '<span class="description">'.$field['desc'].'</span>';
  328.                     break;
  329.  
  330.                     // catalog
  331.                     case 'catalog':
  332.                     echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="10" />
  333.                     <br><span class="description">'.$field['desc'].'</span>';
  334.                     break;
  335.  
  336.                     // text
  337.                     case 'text':
  338.                     echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" />
  339.                     <br><span class="description">'.$field['desc'].'</span>';
  340.                     break;
  341.  
  342.                     // textarea
  343.                     case 'textarea':
  344.                     echo '<textarea name="'.$field['id'].'" id="'.$field['id'].'" cols="60" rows="4">'.$meta.'</textarea>
  345.                     <br><span class="description">'.$field['desc'].'</span>';
  346.                     break;
  347.  
  348.                     // number
  349.                     case 'number':
  350.                     echo '<input type="number" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" />
  351.                     <br><span class="description">'.$field['desc'].'</span>';
  352.                     break;
  353.  
  354.                     // checkbox_group
  355.                     case 'checkbox_group':
  356.                         foreach ($field['options'] as $option) {
  357.                             echo '<input type="checkbox" value="'.$option['value'].'" name="'.$field['id'].'[]" id="'.$option['value'].'"',$meta && in_array($option['value'], $meta) ? ' checked="checked"' : '',' />
  358.                                     <label for="'.$option['value'].'">'.$option['label'].'</label><br>';
  359.                         }
  360.                         echo '<span class="description">'.$field['desc'].'</span>';
  361.                     break;
  362.  
  363.                     // date
  364.                     case 'date':
  365.                     echo '<input type="text" class="datepicker" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" />
  366.                     <br><span class="description">'.$field['desc'].'</span>';
  367.                     break;
  368.  
  369.                 } //end switch
  370.         echo '</td></tr>';
  371.     } // end foreach
  372.     echo '<tr><td colspan="2"><strong>For ALBUM ARTWORK set Featured Image!</strong></td></tr></table>'; // end table
  373. }
  374.  
  375. // Datepicker
  376.     // load jQuery Datepicker
  377.         add_action( 'admin_print_scripts-post.php', 'product_admin_script' , 11 );
  378.         add_action( 'admin_print_scripts-post-new.php', 'product_admin_script', 11 );
  379.  
  380.         function product_admin_script() {
  381.             global $post_type;
  382.             if( $post_type != 'product' ) { return; }
  383.             wp_enqueue_script( 'jquery-ui-datepicker' );
  384.             wp_enqueue_style('jquery-ui-custom', get_template_directory_uri().'/library/css/jquery-ui-custom.css');
  385.         }
  386.  
  387.  
  388.     // assign jQuery to date fields
  389.     add_action('admin_head','add_album_info_scripts');
  390.     function add_album_info_scripts() {
  391.         global $album_info_meta_fields, $post, $post_type;
  392.             if( $post_type != 'product' ) { return; }
  393.             $output = '<script type="text/javascript">
  394.                         jQuery(function() {';
  395.  
  396.             foreach ($album_info_meta_fields as $field) { // loop through the fields looking for certain types
  397.                 if($field['type'] == 'date')
  398.                     $output .= 'jQuery(".datepicker").datepicker({ dateFormat: \'yy-mm-dd\' });';
  399.             }
  400.  
  401.             $output .= '});</script>
  402.         ';
  403.  
  404.             echo $output;
  405.        
  406.     }
  407.  
  408.  
  409. // Save the Data
  410. function save_album_info_meta($post_id) {
  411.     global $album_info_meta_fields;
  412.    
  413.     // verify nonce
  414.     if (!wp_verify_nonce($_POST['album_info_meta_box_nonce'], basename(__FILE__)))
  415.         return $post_id;
  416.     // check autosave
  417.     if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
  418.         return $post_id;
  419.     // check permissions
  420.     if ('page' == $_POST['post_type']) {
  421.         if (!current_user_can('edit_page', $post_id))
  422.             return $post_id;
  423.         } elseif (!current_user_can('edit_post', $post_id)) {
  424.             return $post_id;
  425.     }
  426.    
  427.     // loop through fields and save the data
  428.     foreach ($album_info_meta_fields as $field) {
  429.         $old = get_post_meta($post_id, $field['id'], true);
  430.         $new = $_POST[$field['id']];
  431.         if ($new && $new != $old) {
  432.             update_post_meta($post_id, $field['id'], $new);
  433.         } elseif ('' == $new && $old) {
  434.             delete_post_meta($post_id, $field['id'], $old);
  435.         }
  436.     } // end foreach
  437. } // don't remove this bracket!
  438.  
  439. add_action('save_post', 'save_album_info_meta');
  440.  
  441.  
  442. // Add Products to Archive.php and Search
  443.  
  444.     //Archive
  445.     function namespace_add_custom_types( $query ) {
  446.       if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
  447.         $query->set( 'post_type', array(
  448.          'post', 'product'
  449.             ));
  450.           return $query;
  451.         }
  452.     }
  453.     //The hook
  454.     add_filter( 'pre_get_posts', 'namespace_add_custom_types' );
  455.  
  456.     //Search
  457.         // Define what post types to search
  458.         function searchAll( $query ) {
  459.             if ( $query->is_search ) {
  460.                 $query->set( 'post_type', array( 'post', 'page', 'feed', 'product'));
  461.             }
  462.             return $query;
  463.         }
  464.  
  465.         // The hook needed to search ALL post types
  466.         add_filter( 'the_search_query', 'searchAll' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement