Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /************* Product Post Type *****************/
- // let's create the function for the (custom) product type
- function product_post() {
- // creating (registering) the (custom) product type
- register_post_type( 'product', /* (http://codex.wordpress.org/Function_Reference/register_post_type) */
- // let's now add all the options for this post type
- array('labels' => array(
- 'name' => __('Albums & Merch', 'bonestheme'), /* This is the Title of the Group */
- 'singular_name' => __('Product', 'bonestheme'), /* This is the individual type */
- 'all_items' => __('All Products', 'bonestheme'), /* the all items menu item */
- 'add_new' => __('Add New Product', 'bonestheme'), /* The add new menu item */
- 'add_new_item' => __('Add New Product', 'bonestheme'), /* Add New Display Title */
- 'edit' => __( 'Edit', 'bonestheme' ), /* Edit Dialog */
- 'edit_item' => __('Edit Product', 'bonestheme'), /* Edit Display Title */
- 'new_item' => __('New Product', 'bonestheme'), /* New Display Title */
- 'view_item' => __('View Product', 'bonestheme'), /* View Display Title */
- 'search_items' => __('Search Product', 'bonestheme'), /* Search Custom Type Title */
- 'not_found' => __('Nothing found in the Database. Freakin\' nutting, man. No Products AT ALL.', 'bonestheme'), /* This displays if there are no entries yet */
- 'not_found_in_trash' => __('Nothing found in Trash but a cockroach', 'bonestheme'), /* This displays if there is nothing in the trash */
- 'parent_item_colon' => ''
- ), /* end of arrays */
- 'description' => __( 'This is the post type for products', 'bonestheme' ), /* Custom Type Description */
- 'public' => true,
- 'publicly_queryable' => true,
- 'exclude_from_search' => false,
- 'show_ui' => true,
- 'query_var' => true,
- 'menu_position' => 5, /* this is what order you want it to appear in on the left hand side menu */
- 'menu_icon' => get_stylesheet_directory_uri() . '/library/images/product-post-icon.png', /* the icon for the custom post type menu */
- 'rewrite' => array( 'slug' => 'product', 'with_front' => false ), /* you can specify its url slug */
- 'has_archive' => 'product', /* you can rename the slug here */
- 'capability_type' => 'post',
- 'hierarchical' => false,
- 'taxonomies' => array( 'category'),
- 'register_meta_box_cb' => 'add_album_info_meta_box',
- /* the next one is important, it tells what's enabled in the post editor
- i.e. 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'sticky') */
- 'supports' => array( 'title', 'editor', 'thumbnail', 'sticky')
- ) /* end of options */
- ); /* end of register post type */
- /* this adds your post categories to your custom post type */
- register_taxonomy_for_object_type('category', 'product');
- /* this adds your post tags to your custom post type */
- register_taxonomy_for_object_type('post_tag', 'product');
- }
- // Remove default category and tag box
- function remove_product_default_fields() {
- remove_meta_box( 'categorydiv' , 'product' , 'side' ); // Category Box
- // remove_meta_box( 'tagsdiv-post_tag' , 'product' , 'side' ); // Tag Box (uncomment to hide)
- }
- add_action( 'admin_menu' , 'remove_product_default_fields' );
- // Save Product to "Products" category
- function save_to_product_category($post_ID) {
- //PARENT CATEGORY
- //Create parent category based on Singular Name
- $get_cat_name = get_post_type_object( 'product' );
- $cat_name = $get_cat_name->labels->singular_name;
- if ( ! category_exists($cat_name) ) {
- wp_create_category( $cat_name );
- }
- //Assign post to parent category
- global $wpdb;
- if(wp_is_post_autosave($post_ID) || wp_is_post_revision($post_ID)) {
- return $post_ID;
- }
- $ccat_id = intval( get_cat_ID( $cat_name ) );
- wp_set_object_terms( $post_ID, $ccat_id, 'category' );
- //CHILD CATEGORY
- //Create child category based on Format
- $get_child_cat_name = $post_meta_data['album_info_format'][0];
- foreach ($get_child_cat_name as $child_cat_string) {
- switch($child_cat_string) {
- //Cassette
- case 'mc':
- $child_cat_name = array('Record','Cassette');
- break;
- //CD
- case 'cd':
- $child_cat_name = array('Record','CD');
- break;
- //Vinyl
- case 'lp':
- $child_cat_name = array('Record','Vinyl');
- break;
- //Digital
- case 'mp':
- $child_cat_name = array('Record','Digital');
- break;
- //Magazine
- case 'zine':
- $child_cat_name = array('Print','Magazine');
- break;
- //Print
- case 'print':
- $child_cat_name = array('Print');
- break;
- //Video
- case 'video':
- $child_cat_name = array('Video');
- break;
- //Apparel
- case 'apparel':
- $child_cat_name = array('Apparel');
- break;
- //Textile
- case 'textile':
- $child_cat_name = array('Textile');
- break;
- //Merch
- case 'merch':
- $child_cat_name = array('Merchandise');
- break;
- //Other
- case 'other':
- $child_cat_name = array('Other Formats');
- break;
- }
- }
- foreach ($child_cat_name as $child_cat_name_string) {
- if ( ! category_exists($child_cat_name_string) ) {
- wp_create_category( $child_cat_name_string, $ccat_id );
- }
- }
- //Assign post to child category
- global $wpdb;
- if(wp_is_post_autosave($post_ID) || wp_is_post_revision($post_ID)) {
- return $post_ID;
- }
- foreach ($child_cat_name as $child_cat_cat_string) {
- $child_ccat_id = intval( get_cat_ID( $child_cat_cat_string ) );
- wp_set_object_terms( $post_ID, $child_ccat_id, 'category' );
- }
- }
- add_action('publish_product', 'save_to_product_category');
- // adding the (custom) product function to the Wordpress init
- add_action( 'init', 'product_post');
- /************* Album Info Meta Box *****************/
- // Add the Album Info Meta Box
- function add_album_info_meta_box() {
- add_meta_box(
- 'album_info_meta_box', // $id
- 'Album Info', // $title
- 'show_album_info_meta_box', // $callback
- 'product', // $page
- 'normal', // $context
- 'high' // $priority
- );
- }
- add_action('add_meta_boxes', 'add_album_info_meta_box');
- // Field Array
- $prefix = 'album_info_';
- $album_info_meta_fields = array(
- array (
- 'label' => 'Product Type',
- 'desc' => 'Is this product music (cassette, album, mp3 etc.) or anything else (book, magazine, tote bag etc.)?',
- 'id' => $prefix.'product_type',
- 'type' => 'radio',
- 'options' => array (
- 'music' => array (
- 'label' => 'Music',
- 'value' => 'music'
- ),
- 'else' => array (
- 'label' => 'Something Else',
- 'value' => 'else'
- )
- )
- ),
- array(
- 'label'=> 'Catalog Number',
- 'desc' => 'Catalog Number; i.e. eco_024 (must be lowercase!)',
- 'id' => $prefix.'catalog',
- 'type' => 'catalog'
- ),
- array(
- 'label'=> 'Bandcamp ID',
- 'desc' => 'Bandcamp ID of the album; as given i.e. in Bandcamp\'s embed code. ',
- 'id' => $prefix.'bandcamp',
- 'type' => 'text'
- ),
- array(
- 'label'=> 'Discogs.com URL',
- 'desc' => 'If Discogs has an entry for the album, place URL here; i.e. http://www.discogs.com/Zoul-Pig-Nest/release/2524164',
- 'id' => $prefix.'discogs',
- 'type' => 'text'
- ),
- array(
- 'label'=> 'Artist',
- 'desc' => 'The artist\'s name or "Various" if there is more than one artist, i.e. for compilations',
- 'id' => $prefix.'artist',
- 'type' => 'text'
- ),
- array(
- 'label'=> 'Title',
- 'desc' => 'The album title',
- 'id' => $prefix.'album',
- 'type' => 'text'
- ),
- array(
- 'label'=> 'Specials',
- 'desc' => 'Notes about special packaging etc.',
- 'id' => $prefix.'special',
- 'type' => 'textarea'
- ),
- array(
- 'label'=> 'Number of Tracks/Pages',
- 'desc' => 'How many Tracks/Pages does the album/magazine have?<br>(Leave empty if this is not making sense for this specific product!)',
- 'id' => $prefix.'tracks',
- 'type' => 'number'
- ),
- array(
- 'label'=> 'Length',
- 'desc' => 'How long is the album? dd:hh:mm:ss, i.e. 27:00',
- 'id' => $prefix.'length',
- 'type' => 'text'
- ),
- array(
- 'label'=> 'Copies',
- 'desc' => 'How many copies?',
- 'id' => $prefix.'copies',
- 'type' => 'number'
- ),
- array(
- 'label' => 'Release Date',
- 'desc' => 'Pick the day the item was released. yyyy-mm-dd',
- 'id' => $prefix.'release_date',
- 'type' => 'date'
- ),
- array (
- 'label' => 'Format',
- '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".',
- 'id' => $prefix.'format',
- 'type' => 'checkbox_group',
- 'options' => array (
- 'mc' => array (
- 'label' => 'Cassette',
- 'value' => 'mc'
- ),
- 'cd' => array (
- 'label' => 'CD',
- 'value' => 'cd'
- ),
- 'lp' => array (
- 'label' => 'Vinyl',
- 'value' => 'lp'
- ),
- 'mp' => array (
- 'label' => 'Digital',
- 'value' => 'mp'
- ),
- 'zine' => array (
- 'label' => 'Magazine',
- 'value' => 'zine'
- ),
- 'print' => array (
- 'label' => 'Print',
- 'value' => 'print'
- ),
- 'video' => array (
- 'label' => 'Video',
- 'value' => 'video'
- ),
- 'apparel' => array (
- 'label' => 'Apparel',
- 'value' => 'apparel'
- ),
- 'textile' => array (
- 'label' => 'Textile',
- 'value' => 'textile'
- ),
- 'merch' => array (
- 'label' => 'Merchandise',
- 'value' => 'merch'
- ),
- 'other' => array (
- 'label' => 'Other',
- 'value' => 'other'
- )
- )
- )
- );
- // The Callback
- function show_album_info_meta_box() {
- global $album_info_meta_fields, $post;
- // Use nonce for verification
- echo '<input type="hidden" name="album_info_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';
- // Begin the field table and loop
- echo '<table class="form-table">';
- foreach ($album_info_meta_fields as $field) {
- // get value of this field if it exists for this post
- $meta = get_post_meta($post->ID, $field['id'], true);
- // begin a table row with
- echo '<tr>
- <th><label for="'.$field['id'].'">'.$field['label'].'</label></th>
- <td>';
- switch($field['type']) {
- // radio
- case 'radio':
- foreach ( $field['options'] as $option ) {
- echo '<input type="radio" name="'.$field['id'].'" id="'.$option['value'].'" value="'.$option['value'].'" ',$meta == $option['value'] ? ' checked="checked"' : '',' />
- <label for="'.$option['value'].'">'.$option['label'].'</label><br>';
- }
- echo '<span class="description">'.$field['desc'].'</span>';
- break;
- // catalog
- case 'catalog':
- echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="10" />
- <br><span class="description">'.$field['desc'].'</span>';
- break;
- // text
- case 'text':
- echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" />
- <br><span class="description">'.$field['desc'].'</span>';
- break;
- // textarea
- case 'textarea':
- echo '<textarea name="'.$field['id'].'" id="'.$field['id'].'" cols="60" rows="4">'.$meta.'</textarea>
- <br><span class="description">'.$field['desc'].'</span>';
- break;
- // number
- case 'number':
- echo '<input type="number" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" />
- <br><span class="description">'.$field['desc'].'</span>';
- break;
- // checkbox_group
- case 'checkbox_group':
- foreach ($field['options'] as $option) {
- echo '<input type="checkbox" value="'.$option['value'].'" name="'.$field['id'].'[]" id="'.$option['value'].'"',$meta && in_array($option['value'], $meta) ? ' checked="checked"' : '',' />
- <label for="'.$option['value'].'">'.$option['label'].'</label><br>';
- }
- echo '<span class="description">'.$field['desc'].'</span>';
- break;
- // date
- case 'date':
- echo '<input type="text" class="datepicker" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" />
- <br><span class="description">'.$field['desc'].'</span>';
- break;
- } //end switch
- echo '</td></tr>';
- } // end foreach
- echo '<tr><td colspan="2"><strong>For ALBUM ARTWORK set Featured Image!</strong></td></tr></table>'; // end table
- }
- // Datepicker
- // load jQuery Datepicker
- add_action( 'admin_print_scripts-post.php', 'product_admin_script' , 11 );
- add_action( 'admin_print_scripts-post-new.php', 'product_admin_script', 11 );
- function product_admin_script() {
- global $post_type;
- if( $post_type != 'product' ) { return; }
- wp_enqueue_script( 'jquery-ui-datepicker' );
- wp_enqueue_style('jquery-ui-custom', get_template_directory_uri().'/library/css/jquery-ui-custom.css');
- }
- // assign jQuery to date fields
- add_action('admin_head','add_album_info_scripts');
- function add_album_info_scripts() {
- global $album_info_meta_fields, $post, $post_type;
- if( $post_type != 'product' ) { return; }
- $output = '<script type="text/javascript">
- jQuery(function() {';
- foreach ($album_info_meta_fields as $field) { // loop through the fields looking for certain types
- if($field['type'] == 'date')
- $output .= 'jQuery(".datepicker").datepicker({ dateFormat: \'yy-mm-dd\' });';
- }
- $output .= '});</script>
- ';
- echo $output;
- }
- // Save the Data
- function save_album_info_meta($post_id) {
- global $album_info_meta_fields;
- // verify nonce
- if (!wp_verify_nonce($_POST['album_info_meta_box_nonce'], basename(__FILE__)))
- return $post_id;
- // check autosave
- if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
- return $post_id;
- // check permissions
- if ('page' == $_POST['post_type']) {
- if (!current_user_can('edit_page', $post_id))
- return $post_id;
- } elseif (!current_user_can('edit_post', $post_id)) {
- return $post_id;
- }
- // loop through fields and save the data
- foreach ($album_info_meta_fields as $field) {
- $old = get_post_meta($post_id, $field['id'], true);
- $new = $_POST[$field['id']];
- if ($new && $new != $old) {
- update_post_meta($post_id, $field['id'], $new);
- } elseif ('' == $new && $old) {
- delete_post_meta($post_id, $field['id'], $old);
- }
- } // end foreach
- } // don't remove this bracket!
- add_action('save_post', 'save_album_info_meta');
- // Add Products to Archive.php and Search
- //Archive
- function namespace_add_custom_types( $query ) {
- if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
- $query->set( 'post_type', array(
- 'post', 'product'
- ));
- return $query;
- }
- }
- //The hook
- add_filter( 'pre_get_posts', 'namespace_add_custom_types' );
- //Search
- // Define what post types to search
- function searchAll( $query ) {
- if ( $query->is_search ) {
- $query->set( 'post_type', array( 'post', 'page', 'feed', 'product'));
- }
- return $query;
- }
- // The hook needed to search ALL post types
- add_filter( 'the_search_query', 'searchAll' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement