Advertisement
Guest User

wp metabox

a guest
Feb 7th, 2013
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.04 KB | None | 0 0
  1. <?php
  2.  
  3. function get_sample_options() {
  4. $options = array (
  5. '3d Glasses' => '3dglasses',
  6. 'Airplane' => 'airplane',
  7. 'Alarm' => 'alarm',
  8. 'Appliance' => 'appliance',
  9. );
  10. return $options;
  11. }
  12.  
  13. add_action('admin_init', 'add_meta_boxes', 1);
  14. function add_meta_boxes() {
  15. add_meta_box( 'repeatable-fields', 'Audio Playlist', 'repeatable_meta_box_display', 'rooms', 'normal', 'high');
  16. }
  17.  
  18. function repeatable_meta_box_display() {
  19. global $post;
  20.  
  21. $repeatable_fields = get_post_meta($post->ID, 'repeatable_fields', true);
  22. $options = get_sample_options();
  23.  
  24.  
  25. wp_nonce_field( 'repeatable_meta_box_nonce', 'repeatable_meta_box_nonce' );
  26. ?>
  27. <script type="text/javascript">
  28. jQuery(document).ready(function($) {
  29. $('.metabox_submit').click(function(e) {
  30. e.preventDefault();
  31. $('#publish').click();
  32. });
  33. $('#add-row').on('click', function() {
  34. var row = $('.empty-row.screen-reader-text').clone(true);
  35. row.removeClass('empty-row screen-reader-text');
  36. row.insertBefore('#repeatable-fieldset-one tbody>tr:last');
  37. return false;
  38. });
  39. $('.remove-row').on('click', function() {
  40. $(this).parents('tr').remove();
  41. return false;
  42. });
  43.  
  44. $('#repeatable-fieldset-one tbody').sortable({
  45. opacity: 0.6,
  46. revert: true,
  47. cursor: 'move',
  48. handle: '.sort'
  49. });
  50.  
  51. });
  52. </script>
  53.  
  54. <table id="repeatable-fieldset-one" width="100%">
  55. <thead>
  56. <tr>
  57. <th width="2%"></th>
  58. <th width="10%">Select</th>
  59. <th width="40%">Name</th>
  60. <th width="2%"></th>
  61. </tr>
  62. </thead>
  63. <tbody>
  64. <?php
  65.  
  66. if ( $repeatable_fields ) :
  67.  
  68. foreach ( $repeatable_fields as $field ) {
  69. ?>
  70. <tr>
  71. <td><a class="sort">|||</a></td>
  72.  
  73. <td>
  74. <select name="select[]">
  75. <?php foreach ( $options as $label => $value ) : ?>
  76. <option value="<?php echo $value; ?>"<?php selected( $field['select'], $value ); ?>><?php echo $label; ?></option>
  77. <?php endforeach; ?>
  78. </select>
  79. </td>
  80.  
  81. <td><input type="text" class="widefat" name="name[]" value="<?php if($field['name'] != '') echo esc_attr( $field['name'] ); ?>" /></td>
  82.  
  83. <td><a class="button remove-row" href="#">Remove</a></td>
  84.  
  85. </tr>
  86. <?php
  87. }
  88. else :
  89. // show a blank one
  90. ?>
  91. <tr>
  92. <td><a class="sort">|||</a></td>
  93.  
  94. <td>
  95. <select name="select[]">
  96. <?php foreach ( $options as $label => $value ) : ?>
  97. <option value="<?php echo $value; ?>"><?php echo $label; ?></option>
  98. <?php endforeach; ?>
  99. </select>
  100. </td>
  101.  
  102. <td><input type="text" class="widefat" name="name[]" /></td>
  103.  
  104. <td><a class="button remove-row" href="#">Remove</a></td>
  105.  
  106. </tr>
  107. <?php endif; ?>
  108.  
  109. <!-- empty hidden one for jQuery -->
  110. <tr class="empty-row screen-reader-text">
  111. <td><a class="sort">|||</a></td>
  112.  
  113. <td>
  114. <select name="select[]">
  115. <?php foreach ( $options as $label => $value ) : ?>
  116. <option value="<?php echo $value; ?>"><?php echo $label; ?></option>
  117. <?php endforeach; ?>
  118. </select>
  119. </td>
  120.  
  121. <td><input type="text" class="widefat" name="name[]" /></td>
  122.  
  123. <td><a class="button remove-row" href="#">Remove</a></td>
  124.  
  125. </tr>
  126. </tbody>
  127. </table>
  128.  
  129. <p><a id="add-row" class="button" href="#">Add another</a>
  130. <input type="submit" class="metabox_submit button" value="Save" />
  131. </p>
  132. <?php
  133. }
  134.  
  135. add_action('save_post', 'repeatable_meta_box_save');
  136. function repeatable_meta_box_save($post_id) {
  137. if ( ! isset( $_POST['repeatable_meta_box_nonce'] ) ||
  138. ! wp_verify_nonce( $_POST['repeatable_meta_box_nonce'], 'repeatable_meta_box_nonce' ) )
  139. return;
  140.  
  141. if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
  142. return;
  143.  
  144. if (!current_user_can('edit_post', $post_id))
  145. return;
  146.  
  147. $old = get_post_meta($post_id, 'repeatable_fields', true);
  148. $new = array();
  149. $options = get_sample_options();
  150.  
  151. $selects = $_POST['select'];
  152. $names = $_POST['name'];
  153.  
  154. $count = count( $names );
  155.  
  156. for ( $i = 0; $i < $count; $i++ ) {
  157. if ( $names[$i] != '' ) :
  158.  
  159. if ( in_array( $selects[$i], $options ) )
  160. $new[$i]['select'] = $selects[$i];
  161. else
  162. $new[$i]['select'] = '';
  163. $new[$i]['name'] = stripslashes( strip_tags( $names[$i] ) );
  164.  
  165. endif;
  166. }
  167.  
  168. if ( !empty( $new ) && $new != $old )
  169. update_post_meta( $post_id, 'repeatable_fields', $new );
  170. elseif ( empty($new) && $old )
  171. delete_post_meta( $post_id, 'repeatable_fields', $old );
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement