1. <?php
  2.  
  3. /**
  4.  * Modified Version of deluxeblogtips code
  5.  * http://chrisburbridge.com/?attachment_id=316
  6.  *
  7.  * Original:
  8.  *
  9.  * Create meta boxes for editing pages in WordPress
  10.  * Compatible with custom post types in WordPress 3.0
  11.  *
  12.  * Support input types: text, textarea, checkbox, radio box, select, file, image
  13.  *
  14.  * @author: Rilwis
  15.  * @url: http://www.deluxeblogtips.com/2010/05/howto-meta-box-wordpress.html
  16.  * @url: http://www.deluxeblogtips.com/2010/04/how-to-create-meta-box-wordpress-post.html
  17.  * @version: 2.2
  18.  *
  19.  * Changelog:
  20.  * - 2.2: add enctype to post form (fix upload bug), thanks to http://www.hashbangcode.com/blog/add-enctype-wordpress-post-and-page-forms-471.html
  21.  * - 2.1: add file upload, image upload support
  22.  * - 2.0: oop code, support multiple post types, multiple meta boxes
  23.  * - 1.0: procedural code
  24.  */
  25.  
  26. /******************************
  27.  
  28. Edit meta box settings here
  29.  
  30. ******************************/
  31.  
  32. $prefix = 'dbt_';
  33.  
  34. $meta_boxes = array();
  35.  
  36. // first meta box
  37. $meta_boxes[] = array(
  38.     'id' => 'my-meta-box-1',
  39.     'title' => 'Custom meta box 1',
  40.     'pages' => array('post', 'page', 'link'), // multiple post types
  41.     'context' => 'normal',
  42.     'priority' => 'high',
  43.     'fields' => array(
  44.         array(
  45.             'name' => 'Text box',
  46.             'desc' => 'Enter something here',
  47.             'id' => $prefix . 'text',
  48.             'type' => 'text', // text box
  49.             'std' => 'Default value 1'
  50.         ),
  51.         array(
  52.             'name' => 'Textarea',
  53.             'desc' => 'Enter big text here',
  54.             'id' => $prefix . 'textarea',
  55.             'type' => 'textarea', // text area
  56.             'std' => 'Default value 2'
  57.         ),
  58.         array(
  59.             'name' => 'Tiny MCE',
  60.             'desc' => 'Enter big text here',
  61.             'id' => $prefix . 'tinymce',
  62.             'type' => 'tinymce', // tinymce
  63.             'std' => 'Default value 2'
  64.         ),
  65.         array(
  66.             'name' => 'Select box',
  67.             'id' => $prefix . 'select',
  68.             'type' => 'select', // select box
  69.             'options' => array('Option 1', 'Option 2', 'Option 3') // array of options for select box
  70.         ),
  71.         array(
  72.             'name' => 'Radio',
  73.             'id' => $prefix . 'radio',
  74.             'type' => 'radio', // radio box
  75.             'options' => array( // array of name, value pairs for radio options
  76.                 array('name' => 'Name 1', 'value' => 'Value 1'),
  77.                 array('name' => 'Name 2', 'value' => 'Value 2')
  78.             )
  79.         ),
  80.         array(
  81.             'name' => 'Checkbox', // check box
  82.             'id' => $prefix . 'checkbox',
  83.             'type' => 'checkbox'
  84.         )
  85.     )
  86. );
  87.  
  88. // second meta box
  89. $meta_boxes[] = array(
  90.     'id' => 'my-meta-box-2',
  91.     'title' => 'Custom meta box 2',
  92.     'pages' => array('post', 'film', 'album'), // custom post types, since WordPress 3.0
  93.     'context' => 'normal',
  94.     'priority' => 'high',
  95.     'fields' => array(
  96.         array(
  97.             'name' => 'File upload',
  98.             'desc' => 'For file upload',
  99.             'id' => $prefix . 'file',
  100.             'type' => 'file' // file upload
  101.         ),
  102.         array(
  103.             'name' => 'Image upload',
  104.             'desc' => 'For image upload',
  105.             'id' => $prefix . 'image',
  106.             'type' => 'image' // image upload
  107.         ),
  108.        
  109.     )
  110. );
  111.  
  112. /*********************************
  113.  
  114. You should not edit the code below
  115.  
  116. *********************************/
  117.  
  118. foreach ($meta_boxes as $meta_box) {
  119.     $my_box = new My_meta_box($meta_box);
  120. }
  121.  
  122. class My_meta_box {
  123.  
  124.     protected $_meta_box;
  125.  
  126.     // create meta box based on given data
  127.     function __construct($meta_box) {
  128.         if (!is_admin()) return;
  129.    
  130.         $this->_meta_box = $meta_box;
  131.  
  132.         // fix upload bug: http://www.hashbangcode.com/blog/add-enctype-wordpress-post-and-page-forms-471.html
  133.         $current_page = substr(strrchr($_SERVER['PHP_SELF'], '/'), 1, -4);
  134.         if ($current_page == 'page' || $current_page == 'page-new' || $current_page == 'post' || $current_page == 'post-new') {
  135.             add_action('admin_head', array(&$this, 'add_post_enctype'));
  136.         }
  137.        
  138.         add_action('admin_menu', array(&$this, 'add'));
  139.  
  140.         add_action('save_post', array(&$this, 'save'));
  141.     }
  142.    
  143.     function add_post_enctype() {
  144.         echo '
  145.         <script type="text/javascript">
  146.         jQuery(document).ready(function(){
  147.             jQuery("#post").attr("enctype", "multipart/form-data");
  148.             jQuery("#post").attr("encoding", "multipart/form-data");
  149.         });
  150.         </script>';
  151.     }
  152.  
  153.     /// Add meta box for multiple post types
  154.     function add() {
  155.         foreach ($this->_meta_box['pages'] as $page) {
  156.             add_meta_box($this->_meta_box['id'], $this->_meta_box['title'], array(&$this, 'show'), $page, $this->_meta_box['context'], $this->_meta_box['priority']);
  157.         }
  158.     }
  159.  
  160.     // Callback function to show fields in meta box
  161.     function show() {
  162.         global $post;
  163.        
  164.         $mce_ids = array();
  165.  
  166.         // Use nonce for verification
  167.         echo '<input type="hidden" name="mytheme_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
  168.    
  169.         echo '<table class="form-table">';
  170.  
  171.         foreach ($this->_meta_box['fields'] as $field) {
  172.             // get current post meta data
  173.             $meta = get_post_meta($post->ID, $field['id'], true);
  174.        
  175.             echo '<tr>',
  176.                     '<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>',
  177.                     '<td>';
  178.             switch ($field['type']) {
  179.                 case 'text':
  180.                     echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />',
  181.                         '<br />', $field['desc'];
  182.                     break;
  183.                 case 'textarea':
  184.                     echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>',
  185.                         '<br />', $field['desc'];
  186.                     break;
  187.                 case 'tinymce':  
  188.                    array_push($mce_ids, $field['id']);
  189.                     echo '<div style="border: 1px solid #dfdfdf;">';
  190.                     echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>',
  191.                         '<br />';
  192.                     echo '</div>';
  193.                     echo $field['desc'];
  194.                     break;
  195.                 case 'select':
  196.                     echo '<select name="', $field['id'], '" id="', $field['id'], '">';
  197.                     foreach ($field['options'] as $option) {
  198.                         echo '<option', $meta == $option ? ' selected="selected"' : '', '>', $option, '</option>';
  199.                     }
  200.                     echo '</select>';
  201.                     break;
  202.                 case 'radio':
  203.                     foreach ($field['options'] as $option) {
  204.                         echo '<input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />', $option['name'];
  205.                     }
  206.                     break;
  207.                 case 'checkbox':
  208.                     echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />';
  209.                     break;
  210.                 case 'file':
  211.                     echo $meta ? "$meta<br />" : '', '<input type="file" name="', $field['id'], '" id="', $field['id'], '" />',
  212.                         '<br />', $field['desc'];
  213.                     break;
  214.                 case 'image':
  215.                     echo $meta ? "<img src=\"$meta\" width=\"150\" height=\"150\" /><br />$meta<br />" : '', '<input type="file" name="', $field['id'], '" id="', $field['id'], '" />',
  216.                         '<br />', $field['desc'];
  217.                     break;
  218.             }
  219.             echo    '<td>',
  220.                 '</tr>';
  221.         }
  222.    
  223.         echo '</table>';
  224.  
  225.         // If we have accumulated any TinyMCE type items, then active TinyMCE on them
  226.         if (count($mce_ids) > 0) {
  227.             ?>
  228.                 <script type="text/javascript">
  229.                 /* <![CDATA[ */
  230.  
  231.                 jQuery(document).ready( function () {
  232.                     if ( typeof( tinyMCE ) == "object" && typeof( tinyMCE.execCommand ) == "function" ) {
  233.                         <?php foreach ($mce_ids as $id) : ?>
  234.                         tinyMCE.execCommand("mceAddControl", false, "<?php echo $id; ?>");    
  235.                         <?php endforeach; ?>
  236.                     }
  237.                 });
  238.  
  239.                 /* ]]> */
  240.                 </script>
  241.             <?php      
  242.         }
  243.        
  244.     }
  245.  
  246.     // Save data from meta box
  247.     function save($post_id) {
  248.         // verify nonce
  249.         if (!wp_verify_nonce($_POST['mytheme_meta_box_nonce'], basename(__FILE__))) {
  250.             return $post_id;
  251.         }
  252.  
  253.         // check autosave
  254.         if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
  255.             return $post_id;
  256.         }
  257.  
  258.         // check permissions
  259.         if ('page' == $_POST['post_type']) {
  260.             if (!current_user_can('edit_page', $post_id)) {
  261.                 return $post_id;
  262.             }
  263.         } elseif (!current_user_can('edit_post', $post_id)) {
  264.             return $post_id;
  265.         }
  266.  
  267.         foreach ($this->_meta_box['fields'] as $field) {
  268.             $name = $field['id'];
  269.            
  270.             $old = get_post_meta($post_id, $name, true);
  271.             $new = $_POST[$field['id']];
  272.            
  273.             if ($field['type'] == 'file' || $field['type'] == 'image') {
  274.                 $file = wp_handle_upload($_FILES[$name], array('test_form' => false));
  275.                 $new = $file['url'];
  276.             }
  277.            
  278.             if ($new && $new != $old) {
  279.                 update_post_meta($post_id, $name, $new);
  280.             } elseif ('' == $new && $old && $field['type'] != 'file' && $field['type'] != 'image') {
  281.                 delete_post_meta($post_id, $name, $old);
  282.             }
  283.         }
  284.     }
  285. }
  286. ?>