Guest User

Untitled

a guest
Oct 9th, 2014
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.66 KB | None | 0 0
  1. function register_cpt_category()
  2. {
  3.  
  4. $labels = array(
  5. 'name' => _x('Categories', 'category'),
  6. 'singular_name' => _x('category', 'category'),
  7. 'add_new' => _x('Add New', 'category'),
  8. 'add_new_item' => _x('Add New category', 'category'),
  9. 'edit_item' => _x('Edit Category', 'category'),
  10. 'new_item' => _x('New Category', 'category'),
  11. 'view_item' => _x('View Category', 'category'),
  12. 'search_items' => _x('Search Categories', 'category'),
  13. 'not_found' => _x('No categories found', 'category'),
  14. 'not_found_in_trash' => _x('No categories found in Trash', 'category'),
  15. 'parent_item_colon' => _x('Parent Category:', 'category'),
  16. 'menu_name' => _x('Categories', 'category'),
  17. );
  18.  
  19. $args = array(
  20. 'labels' => $labels,
  21. 'hierarchical' => true,
  22. 'description' => 'Categories filterable by genre',
  23. 'supports' => array('title', 'editor', 'thumbnail'),
  24. 'taxonomies' => array('genres'),
  25. 'public' => true,
  26. 'show_ui' => true,
  27. 'show_in_menu' => true,
  28. 'menu_position' => 5,
  29. 'menu_icon' => 'dashicons-smiley',
  30. 'show_in_nav_menus' => true,
  31. 'publicly_queryable' => true,
  32. 'exclude_from_search' => false,
  33. 'has_archive' => true,
  34. 'query_var' => true,
  35. 'can_export' => true,
  36. 'rewrite' => true,
  37. 'capability_type' => 'post'
  38.  
  39. );
  40. register_post_type('category', $args);
  41.  
  42.  
  43. }
  44.  
  45. add_action('init', 'register_cpt_category');
  46.  
  47. add_action('admin_init', 'hhs_add_meta_boxes', 1);
  48. function hhs_add_meta_boxes()
  49. {
  50. add_meta_box('repeatable-fields', 'Repeatable Fields', 'hhs_repeatable_meta_box_display', 'category', 'normal', 'default');
  51. wp_enqueue_script('custom-js', get_template_directory_uri().'/js/custom-js.js');
  52. }
  53.  
  54. function hhs_repeatable_meta_box_display()
  55. {
  56. global $post;
  57.  
  58. $repeatable_fields = get_post_meta($post->ID, 'repeatable_fields', true);
  59.  
  60. wp_nonce_field('hhs_repeatable_meta_box_nonce', 'hhs_repeatable_meta_box_nonce');
  61. ?>
  62. <script type="text/javascript">
  63. jQuery(document).ready(function ($) {
  64. $('#add-row').on('click', function () {
  65. var row = $('.empty-row.screen-reader-text').clone(true);
  66. row.removeClass('empty-row screen-reader-text');
  67. row.insertBefore('#repeatable-fieldset-one tbody>tr:last');
  68. return false;
  69. });
  70.  
  71. $('.remove-row').on('click', function () {
  72. $(this).parents('tr').remove();
  73. return false;
  74. });
  75. });
  76. </script>
  77.  
  78. <table id="repeatable-fieldset-one" width="100%">
  79. <thead>
  80. <tr>
  81. <th width="25%">Name</th>
  82. <th width="10%">Date</th>
  83. <th width="40%">Text</th>
  84. <th width="25%">Image</th>
  85. </tr>
  86. </thead>
  87. <tbody>
  88.  
  89. <?php $meta = get_post_meta($post->ID, $repeatable_fields['image'], true); ?>
  90. <?php
  91.  
  92. if ($repeatable_fields) :
  93.  
  94. foreach ($repeatable_fields as $field) {
  95. ?>
  96.  
  97. <!-- FRONT - EDITABLE -->
  98. <tr>
  99. <td><input type="text" class="widefat" name="name[]"
  100. value="<?php if ($field['name'] != '') echo esc_attr($field['name']); ?>"/></td>
  101.  
  102.  
  103. <td><input type="text" class="widefat" name="date[]"
  104. value="<?php if ($field['date'] != '') echo esc_attr($field['date']); ?>"/></td>
  105.  
  106. <td><input type="text" class="widefat" name="text[]"
  107. value="<?php if ($field['text'] != '') echo esc_attr($field['text']); ?>"/></td>
  108.  
  109. <td>
  110.  
  111. <?php
  112. if ($meta) { $image = wp_get_attachment_image_src($meta, 'medium'); $image = $image[0]; }
  113.  
  114. ?>
  115. <input type="hidden" class="custom_upload_image" name="image[]"
  116.  
  117. value="<?php if ($field['image'] != '') echo esc_attr($field['image']); ?>"/>
  118.  
  119. <img src=" <?php echo $image ?> " class="custom_preview_image" alt="" /><br /> <!-- BROKEN IMAGE (temp fix "echo") -->
  120.  
  121. <input class="custom_upload_image_button button" type="button" value="Choose Image" />
  122.  
  123. <small> <a href="#" class="custom_clear_image_button">Remove Image</a></small></td>
  124.  
  125. <td><a class="button remove-row" href="#">Remove</a></td>
  126. </tr>
  127.  
  128. <?php
  129.  
  130. }
  131. else :
  132. // BLANK ONE
  133. ?>
  134. <tr>
  135. <td><input type="text" class="widefat" name="name[]"/></td>
  136.  
  137. <td><input type="text" class="widefat" name="date[]"/></td>
  138.  
  139. <td><input type="text" class="widefat" name="text[]"/></td>
  140.  
  141.  
  142. <td>
  143.  
  144. <?php
  145. $image = get_template_directory_uri().'/images/image.png';
  146. echo '<span class="custom_default_image" style="display:none">'.$image.'</span>';
  147. if ($meta) { $image = wp_get_attachment_image_src($meta, 'medium'); $image = $image[0]; }
  148.  
  149. ?>
  150. <input type="hidden" class="custom_upload_image" name="image[]"/>
  151.  
  152. <img src=" <?php echo $image ?> " class="custom_preview_image" alt="" /><br /> <!-- BROKEN IMAGE (temp fix "echo") -->
  153.  
  154. <input class="custom_upload_image_button button" type="button" value="Choose Image" />
  155.  
  156. <small> <a href="#" class="custom_clear_image_button">Remove Image</a></small></td>
  157.  
  158. <td><a class="button remove-row" href="#">Remove</a></td>
  159.  
  160.  
  161. </tr>
  162. <?php endif; ?>
  163.  
  164. <!-- empty hidden one for jQuery
  165. LOAD THIS ONE IN !
  166. -->
  167.  
  168. <tr class="empty-row screen-reader-text">
  169. <td><input type="text" class="widefat" name="name[]"/></td>
  170.  
  171. <td><input type="text" class="widefat" name="date[]"/></td>
  172.  
  173. <td><input type="textarea" class="widefat" name="text[]"/></td>
  174.  
  175. <td>
  176.  
  177. <?php
  178. $image = get_template_directory_uri().'/images/image.png';
  179. echo '<span class="custom_default_image" style="display:none">'.$image.'</span>';
  180. if ($meta) { $image = wp_get_attachment_image_src($meta, 'medium'); $image = $image[0]; }
  181.  
  182. ?>
  183. <input type="hidden" class="custom_upload_image" name="image[]"
  184.  
  185. value="<?php if ($field['image'] != '') echo esc_attr($field['image']); ?>"/>
  186.  
  187.  
  188.  
  189. <img src=" <?php echo $image ?> " class="custom_preview_image" alt="" /><br /> <!-- BROKEN IMAGE (temp fix "echo") -->
  190.  
  191. <input class="custom_upload_image_button button" type="button" value="Choose Image" />
  192.  
  193. <small> <a href="#" class="custom_clear_image_button">Remove Image</a></small></td>
  194.  
  195.  
  196. <td><a class="button remove-row" href="#">Remove</a></td>
  197. </tr>
  198. </tbody>
  199. </table>
  200.  
  201. <p><a id="add-row" class="button" href="#">Add another</a></p>
  202. <?php
  203. }
  204.  
  205. add_action('save_post', 'hhs_repeatable_meta_box_save');
  206. function hhs_repeatable_meta_box_save($post_id)
  207. {
  208. if (!isset($_POST['hhs_repeatable_meta_box_nonce']) ||
  209. !wp_verify_nonce($_POST['hhs_repeatable_meta_box_nonce'], 'hhs_repeatable_meta_box_nonce')
  210. )
  211. return;
  212.  
  213. if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
  214. return;
  215.  
  216. if (!current_user_can('edit_post', $post_id))
  217. return;
  218.  
  219. $old = get_post_meta($post_id, 'repeatable_fields');
  220. $new = array();
  221.  
  222. $names = $_POST['name'];
  223. $dates = $_POST['date'];
  224. $texts = $_POST['text'];
  225. $images = $_POST['image'];
  226.  
  227. $count = count($names);
  228.  
  229. for ($i = 0; $i < $count; $i++) {
  230.  
  231. if ($names[$i] != '') :
  232. $new[$i]['name'] = $names[$i];
  233.  
  234. if ($dates[$i] != '') {
  235. $new[$i]['date'] = $dates[$i];
  236. };
  237. if ($texts[$i] != '') {
  238. $new[$i]['text'] = $texts[$i];
  239. };
  240.  
  241. if ($images[$i] != '' ) {
  242. $new[$i]['image'] = $images[$i];
  243. };
  244. endif;
  245.  
  246.  
  247. if (!empty($new) && $new != $old)
  248. update_post_meta($post_id, 'repeatable_fields', $new);
  249. elseif (empty($new) && $old)
  250. delete_post_meta($post_id, 'repeatable_fields', $old);
  251. }
  252.  
  253. }
  254.  
  255. ?>
Advertisement
Add Comment
Please, Sign In to add comment