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.         )
  31.     );
  32. }
  33.  
  34.  
  35.  
  36.  
  37.  
  38. /*********************************
  39. Setup Class
  40. *********************************/
  41.  
  42. foreach ($meta_boxes as $meta_box) {
  43.     $my_box = new My_meta_box($meta_box);
  44. }
  45.  
  46. class My_meta_box {
  47.  
  48.     protected $_meta_box;
  49.  
  50.     // create meta box based on given data
  51.     function __construct($meta_box) {
  52.         if (!is_admin()) return;
  53.    
  54.         $this->_meta_box = $meta_box;
  55.  
  56.         // fix upload bug: http://www.hashbangcode.com/blog/add-enctype-wordpress-post-and-page-forms-471.html
  57.         $current_page = substr(strrchr($_SERVER['PHP_SELF'], '/'), 1, -4);
  58.         if ($current_page == 'page' || $current_page == 'page-new' || $current_page == 'post' || $current_page == 'post-new') {
  59.             add_action('admin_head', array(&$this, 'add_post_enctype'));
  60.         }
  61.        
  62.         add_action('admin_menu', array(&$this, 'add'));
  63.  
  64.         add_action('save_post', array(&$this, 'save'));
  65.     }
  66.    
  67.     function add_post_enctype() {
  68.         echo '
  69.         <script type="text/javascript">
  70.         jQuery(document).ready(function(){
  71.             jQuery("#post").attr("enctype", "multipart/form-data");
  72.             jQuery("#post").attr("encoding", "multipart/form-data");
  73.         });
  74.         </script>';
  75.     }
  76.  
  77.     /// Add meta box for multiple post types
  78.     function add() {
  79.         foreach ($this->_meta_box['pages'] as $page) {
  80.             add_meta_box($this->_meta_box['id'], $this->_meta_box['title'], array(&$this, 'show'), $page, $this->_meta_box['context'], $this->_meta_box['priority']);
  81.         }
  82.     }
  83.  
  84.     // Callback function to show fields in meta box
  85.     function show() {
  86.         global $post;
  87.  
  88.         // Use nonce for verification
  89.         echo '<input type="hidden" name="mytheme_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
  90.    
  91.         echo '<table class="form-table">';
  92.  
  93.         foreach ($this->_meta_box['fields'] as $field) {
  94.             // get current post meta data
  95.             $meta = get_post_meta($post->ID, $field['id'], true);
  96.        
  97.             echo '<tr>',
  98.                     '<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>',
  99.                     '<td>';
  100.             switch ($field['type']) {
  101.                 case 'text':
  102.                     echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />',
  103.                         '<br />', $field['desc'];
  104.                     break;
  105.                 case 'textarea':
  106.                     echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>',
  107.                         '<br />', $field['desc'];
  108.                     break;
  109.                 case 'select':
  110.                     echo '<select name="', $field['id'], '" id="', $field['id'], '">';
  111.                     foreach ($field['options'] as $option) {
  112.                         echo '<option', $meta == $option ? ' selected="selected"' : '', '>', $option, '</option>';
  113.                     }
  114.                     echo '</select>';
  115.                     break;
  116.                 case 'radio':
  117.                     foreach ($field['options'] as $option) {
  118.                         echo '<input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />', $option['name'];
  119.                     }
  120.                     break;
  121.                 case 'checkbox':
  122.                     echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />';
  123.                     break;
  124.                 case 'file':
  125.                     echo $meta ? "$meta<br />" : '', '<input type="file" name="', $field['id'], '" id="', $field['id'], '" />',
  126.                         '<br />', $field['desc'];
  127.                     break;
  128.                 case 'image':
  129.                     echo $meta ? "<img src=\"$meta\" width=\"150\" height=\"150\" /><br />$meta<br />" : '', '<input type="file" name="', $field['id'], '" id="', $field['id'], '" />',
  130.                         '<br />', $field['desc'];
  131.                     break;
  132.             }
  133.             echo    '<td>',
  134.                 '</tr>';
  135.         }
  136.    
  137.         echo '</table>';
  138.     }
  139.  
  140.     // Save data from meta box
  141.     function save($post_id) {
  142.         // verify nonce
  143.         if (!wp_verify_nonce($_POST['mytheme_meta_box_nonce'], basename(__FILE__))) {
  144.             return $post_id;
  145.         }
  146.  
  147.         // check autosave
  148.         if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
  149.             return $post_id;
  150.         }
  151.  
  152.         // check permissions
  153.         if ('page' == $_POST['post_type']) {
  154.             if (!current_user_can('edit_page', $post_id)) {
  155.                 return $post_id;
  156.             }
  157.         } elseif (!current_user_can('edit_post', $post_id)) {
  158.             return $post_id;
  159.         }
  160.  
  161.         foreach ($this->_meta_box['fields'] as $field) {
  162.             $name = $field['id'];
  163.            
  164.             $old = get_post_meta($post_id, $name, true);
  165.             $new = $_POST[$field['id']];
  166.            
  167.             if ($field['type'] == 'file' || $field['type'] == 'image') {
  168.                 $file = wp_handle_upload($_FILES[$name], array('test_form' => false));
  169.                 $new = $file['url'];
  170.             }
  171.            
  172.             if ($new && $new != $old) {
  173.                 update_post_meta($post_id, $name, $new);
  174.             } elseif ('' == $new && $old && $field['type'] != 'file' && $field['type'] != 'image') {
  175.                 delete_post_meta($post_id, $name, $old);
  176.             }
  177.         }
  178.     }
  179. }
  180. ?>