Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function register_cpt_category()
- {
- $labels = array(
- 'name' => _x('Categories', 'category'),
- 'singular_name' => _x('category', 'category'),
- 'add_new' => _x('Add New', 'category'),
- 'add_new_item' => _x('Add New category', 'category'),
- 'edit_item' => _x('Edit Category', 'category'),
- 'new_item' => _x('New Category', 'category'),
- 'view_item' => _x('View Category', 'category'),
- 'search_items' => _x('Search Categories', 'category'),
- 'not_found' => _x('No categories found', 'category'),
- 'not_found_in_trash' => _x('No categories found in Trash', 'category'),
- 'parent_item_colon' => _x('Parent Category:', 'category'),
- 'menu_name' => _x('Categories', 'category'),
- );
- $args = array(
- 'labels' => $labels,
- 'hierarchical' => true,
- 'description' => 'Categories filterable by genre',
- 'supports' => array('title', 'editor', 'thumbnail'),
- 'taxonomies' => array('genres'),
- 'public' => true,
- 'show_ui' => true,
- 'show_in_menu' => true,
- 'menu_position' => 5,
- 'menu_icon' => 'dashicons-smiley',
- 'show_in_nav_menus' => true,
- 'publicly_queryable' => true,
- 'exclude_from_search' => false,
- 'has_archive' => true,
- 'query_var' => true,
- 'can_export' => true,
- 'rewrite' => true,
- 'capability_type' => 'post'
- );
- register_post_type('category', $args);
- }
- add_action('init', 'register_cpt_category');
- add_action('admin_init', 'hhs_add_meta_boxes', 1);
- function hhs_add_meta_boxes()
- {
- add_meta_box('repeatable-fields', 'Repeatable Fields', 'hhs_repeatable_meta_box_display', 'category', 'normal', 'default');
- wp_enqueue_script('custom-js', get_template_directory_uri().'/js/custom-js.js');
- }
- function hhs_repeatable_meta_box_display()
- {
- global $post;
- $repeatable_fields = get_post_meta($post->ID, 'repeatable_fields', true);
- wp_nonce_field('hhs_repeatable_meta_box_nonce', 'hhs_repeatable_meta_box_nonce');
- ?>
- <script type="text/javascript">
- jQuery(document).ready(function ($) {
- $('#add-row').on('click', function () {
- var row = $('.empty-row.screen-reader-text').clone(true);
- row.removeClass('empty-row screen-reader-text');
- row.insertBefore('#repeatable-fieldset-one tbody>tr:last');
- return false;
- });
- $('.remove-row').on('click', function () {
- $(this).parents('tr').remove();
- return false;
- });
- });
- </script>
- <table id="repeatable-fieldset-one" width="100%">
- <thead>
- <tr>
- <th width="25%">Name</th>
- <th width="10%">Date</th>
- <th width="40%">Text</th>
- <th width="25%">Image</th>
- </tr>
- </thead>
- <tbody>
- <?php $meta = get_post_meta($post->ID, $repeatable_fields['image'], true); ?>
- <?php
- if ($repeatable_fields) :
- foreach ($repeatable_fields as $field) {
- ?>
- <!-- FRONT - EDITABLE -->
- <tr>
- <td><input type="text" class="widefat" name="name[]"
- value="<?php if ($field['name'] != '') echo esc_attr($field['name']); ?>"/></td>
- <td><input type="text" class="widefat" name="date[]"
- value="<?php if ($field['date'] != '') echo esc_attr($field['date']); ?>"/></td>
- <td><input type="text" class="widefat" name="text[]"
- value="<?php if ($field['text'] != '') echo esc_attr($field['text']); ?>"/></td>
- <td>
- <?php
- if ($meta) { $image = wp_get_attachment_image_src($meta, 'medium'); $image = $image[0]; }
- ?>
- <input type="hidden" class="custom_upload_image" name="image[]"
- value="<?php if ($field['image'] != '') echo esc_attr($field['image']); ?>"/>
- <img src=" <?php echo $image ?> " class="custom_preview_image" alt="" /><br /> <!-- BROKEN IMAGE (temp fix "echo") -->
- <input class="custom_upload_image_button button" type="button" value="Choose Image" />
- <small> <a href="#" class="custom_clear_image_button">Remove Image</a></small></td>
- <td><a class="button remove-row" href="#">Remove</a></td>
- </tr>
- <?php
- }
- else :
- // BLANK ONE
- ?>
- <tr>
- <td><input type="text" class="widefat" name="name[]"/></td>
- <td><input type="text" class="widefat" name="date[]"/></td>
- <td><input type="text" class="widefat" name="text[]"/></td>
- <td>
- <?php
- $image = get_template_directory_uri().'/images/image.png';
- echo '<span class="custom_default_image" style="display:none">'.$image.'</span>';
- if ($meta) { $image = wp_get_attachment_image_src($meta, 'medium'); $image = $image[0]; }
- ?>
- <input type="hidden" class="custom_upload_image" name="image[]"/>
- <img src=" <?php echo $image ?> " class="custom_preview_image" alt="" /><br /> <!-- BROKEN IMAGE (temp fix "echo") -->
- <input class="custom_upload_image_button button" type="button" value="Choose Image" />
- <small> <a href="#" class="custom_clear_image_button">Remove Image</a></small></td>
- <td><a class="button remove-row" href="#">Remove</a></td>
- </tr>
- <?php endif; ?>
- <!-- empty hidden one for jQuery
- LOAD THIS ONE IN !
- -->
- <tr class="empty-row screen-reader-text">
- <td><input type="text" class="widefat" name="name[]"/></td>
- <td><input type="text" class="widefat" name="date[]"/></td>
- <td><input type="textarea" class="widefat" name="text[]"/></td>
- <td>
- <?php
- $image = get_template_directory_uri().'/images/image.png';
- echo '<span class="custom_default_image" style="display:none">'.$image.'</span>';
- if ($meta) { $image = wp_get_attachment_image_src($meta, 'medium'); $image = $image[0]; }
- ?>
- <input type="hidden" class="custom_upload_image" name="image[]"
- value="<?php if ($field['image'] != '') echo esc_attr($field['image']); ?>"/>
- <img src=" <?php echo $image ?> " class="custom_preview_image" alt="" /><br /> <!-- BROKEN IMAGE (temp fix "echo") -->
- <input class="custom_upload_image_button button" type="button" value="Choose Image" />
- <small> <a href="#" class="custom_clear_image_button">Remove Image</a></small></td>
- <td><a class="button remove-row" href="#">Remove</a></td>
- </tr>
- </tbody>
- </table>
- <p><a id="add-row" class="button" href="#">Add another</a></p>
- <?php
- }
- add_action('save_post', 'hhs_repeatable_meta_box_save');
- function hhs_repeatable_meta_box_save($post_id)
- {
- if (!isset($_POST['hhs_repeatable_meta_box_nonce']) ||
- !wp_verify_nonce($_POST['hhs_repeatable_meta_box_nonce'], 'hhs_repeatable_meta_box_nonce')
- )
- return;
- if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
- return;
- if (!current_user_can('edit_post', $post_id))
- return;
- $old = get_post_meta($post_id, 'repeatable_fields');
- $new = array();
- $names = $_POST['name'];
- $dates = $_POST['date'];
- $texts = $_POST['text'];
- $images = $_POST['image'];
- $count = count($names);
- for ($i = 0; $i < $count; $i++) {
- if ($names[$i] != '') :
- $new[$i]['name'] = $names[$i];
- if ($dates[$i] != '') {
- $new[$i]['date'] = $dates[$i];
- };
- if ($texts[$i] != '') {
- $new[$i]['text'] = $texts[$i];
- };
- if ($images[$i] != '' ) {
- $new[$i]['image'] = $images[$i];
- };
- endif;
- if (!empty($new) && $new != $old)
- update_post_meta($post_id, 'repeatable_fields', $new);
- elseif (empty($new) && $old)
- delete_post_meta($post_id, 'repeatable_fields', $old);
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment