Advertisement
Guest User

Radio

a guest
Nov 17th, 2010
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.92 KB | None | 0 0
  1. <?php
  2.  
  3. /* *
  4. * Modified version of this: http://loak.pastebin.com/u9YTQrxf (from http://www.deluxeblogtips.com/2010/05/howto-meta-box-wordpress.html)
  5. * Modification to allow for incremental versions of the same metabox.
  6. */
  7.  
  8. /* Set up some default variables. */
  9. $i = 1;
  10. $prefix = "slide{$i}-";
  11. $number_of_boxes = 15;
  12.  
  13. /* Loop through the boxes to create as many as you want. */
  14. while ( ++$i <= $number_of_boxes ) {
  15.  
  16. $prefix = "lom_slide_{$i}_";
  17.  
  18. $meta_boxes[] = array(
  19. 'id' => "my-meta-box-{$i}",
  20. 'title' => sprintf( 'Slide %s', $i ),
  21. 'pages' => array( 'lom_slideshow','' ), // multiple post types
  22. 'context' => 'normal',
  23. 'priority' => 'high',
  24. 'fields' => array(
  25. array( 'name' => 'Slide Title', 'desc' => 'Enter the title of the slide here', 'id' => $prefix . 'title', 'type' => 'text' ),
  26. array( 'name' => 'Slide Description', 'desc' => 'Provide a brief description of the picture or video', 'id' => $prefix . 'textarea', 'type' => 'textarea' ),
  27. array( 'name' => 'Slide Image', 'desc' => 'Enter the URL of the image here', 'id' => $prefix . 'image', 'type' => 'text' ),
  28. array( 'name' => 'Slide Embed Code', 'desc' => 'Insert embed code for the video', 'id' => $prefix . 'embed', 'type' => 'textarea' ),
  29. array( 'name' => 'Hide Slide', 'id' => $prefix . 'checkbox', 'type' => 'checkbox' )
  30. array( 'name' => 'Slide Type', 'id' => $prefix . 'slidetype', 'type' => 'radio', 'options' => array("Image Slide", "Video Slide"))
  31.  
  32. )
  33. );
  34. }
  35.  
  36.  
  37.  
  38.  
  39.  
  40. /*********************************
  41. Setup Class
  42. *********************************/
  43.  
  44. foreach ($meta_boxes as $meta_box) {
  45. $my_box = new My_meta_box($meta_box);
  46. }
  47.  
  48. class My_meta_box {
  49.  
  50. protected $_meta_box;
  51.  
  52. // create meta box based on given data
  53. function __construct($meta_box) {
  54. if (!is_admin()) return;
  55.  
  56. $this->_meta_box = $meta_box;
  57.  
  58. // fix upload bug: http://www.hashbangcode.com/blog/add-enctype-wordpress-post-and-page-forms-471.html
  59. $current_page = substr(strrchr($_SERVER['PHP_SELF'], '/'), 1, -4);
  60. if ($current_page == 'page' || $current_page == 'page-new' || $current_page == 'post' || $current_page == 'post-new') {
  61. add_action('admin_head', array(&$this, 'add_post_enctype'));
  62. }
  63.  
  64. add_action('admin_menu', array(&$this, 'add'));
  65.  
  66. add_action('save_post', array(&$this, 'save'));
  67. }
  68.  
  69. function add_post_enctype() {
  70. echo '
  71. <script type="text/javascript">
  72. jQuery(document).ready(function(){
  73. jQuery("#post").attr("enctype", "multipart/form-data");
  74. jQuery("#post").attr("encoding", "multipart/form-data");
  75. });
  76. </script>';
  77. }
  78.  
  79. /// Add meta box for multiple post types
  80. function add() {
  81. foreach ($this->_meta_box['pages'] as $page) {
  82. add_meta_box($this->_meta_box['id'], $this->_meta_box['title'], array(&$this, 'show'), $page, $this->_meta_box['context'], $this->_meta_box['priority']);
  83. }
  84. }
  85.  
  86. // Callback function to show fields in meta box
  87. function show() {
  88. global $post;
  89.  
  90. // Use nonce for verification
  91. echo '<input type="hidden" name="mytheme_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
  92.  
  93. echo '<table class="form-table">';
  94.  
  95. foreach ($this->_meta_box['fields'] as $field) {
  96. // get current post meta data
  97. $meta = get_post_meta($post->ID, $field['id'], true);
  98.  
  99. echo '<tr>',
  100. '<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>',
  101. '<td>';
  102. switch ($field['type']) {
  103. case 'text':
  104. echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />',
  105. '<br />', $field['desc'];
  106. break;
  107. case 'textarea':
  108. echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>',
  109. '<br />', $field['desc'];
  110. break;
  111. case 'select':
  112. echo '<select name="', $field['id'], '" id="', $field['id'], '">';
  113. foreach ($field['options'] as $option) {
  114. echo '<option', $meta == $option ? ' selected="selected"' : '', '>', $option, '</option>';
  115. }
  116. echo '</select>';
  117. break;
  118. case 'radio':
  119. foreach ($field['options'] as $option) {
  120. echo '<input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />', $option['name'];
  121. }
  122. break;
  123. case 'checkbox':
  124. echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />';
  125. break;
  126. case 'file':
  127. echo $meta ? "$meta<br />" : '', '<input type="file" name="', $field['id'], '" id="', $field['id'], '" />',
  128. '<br />', $field['desc'];
  129. break;
  130. case 'image':
  131. echo $meta ? "<img src=\"$meta\" width=\"150\" height=\"150\" /><br />$meta<br />" : '', '<input type="file" name="', $field['id'], '" id="', $field['id'], '" />',
  132. '<br />', $field['desc'];
  133. break;
  134. }
  135. echo '<td>',
  136. '</tr>';
  137. }
  138.  
  139. echo '</table>';
  140. }
  141.  
  142. // Save data from meta box
  143. function save($post_id) {
  144. // verify nonce
  145. if (!wp_verify_nonce($_POST['mytheme_meta_box_nonce'], basename(__FILE__))) {
  146. return $post_id;
  147. }
  148.  
  149. // check autosave
  150. if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
  151. return $post_id;
  152. }
  153.  
  154. // check permissions
  155. if ('page' == $_POST['post_type']) {
  156. if (!current_user_can('edit_page', $post_id)) {
  157. return $post_id;
  158. }
  159. } elseif (!current_user_can('edit_post', $post_id)) {
  160. return $post_id;
  161. }
  162.  
  163. foreach ($this->_meta_box['fields'] as $field) {
  164. $name = $field['id'];
  165.  
  166. $old = get_post_meta($post_id, $name, true);
  167. $new = $_POST[$field['id']];
  168.  
  169. if ($field['type'] == 'file' || $field['type'] == 'image') {
  170. $file = wp_handle_upload($_FILES[$name], array('test_form' => false));
  171. $new = $file['url'];
  172. }
  173.  
  174. if ($new && $new != $old) {
  175. update_post_meta($post_id, $name, $new);
  176. } elseif ('' == $new && $old && $field['type'] != 'file' && $field['type'] != 'image') {
  177. delete_post_meta($post_id, $name, $old);
  178. }
  179. }
  180. }
  181. }
  182. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement