Advertisement
Guest User

Original DBT Code

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