Public paste
Undefined
By: Guest | Date: Apr 19 2012 20:24 | Format: PHP | Expires: never | Size: 4.73 KB | Hits: 939

  1. <?php
  2. $meta_box['activiteiten'] = array(
  3.     'id' => 'kerkdienst-info',
  4.         'title' => 'Aanvullende kerkdienst informatie [ sla over bij gewone activiteit ]',  
  5.     'context' => 'normal',    
  6.     'priority' => 'high',
  7.     'fields' => array(
  8.         array(
  9.             'name' => 'Voorganger',
  10.             'desc' => '',
  11.             'id' => 'act_kerkdienst_voorganger',
  12.             'type' => 'text',
  13.             'default' => ''
  14.         ),
  15.         array(
  16.             'name' => 'Collecte',
  17.             'desc' => '',
  18.             'id' => 'act_kerkdienst_collecte',
  19.             'type' => 'text',
  20.             'default' => ''
  21.         ),
  22.         array(
  23.             'name' => 'Bijzonderheden',
  24.             'desc' => '',
  25.             'id' => 'act_kerkdienst_bijz',
  26.             'type' => 'textarea',
  27.             'default' => ''
  28.         ),
  29.         array(
  30.             'name' => 'Liturgie',
  31.             'desc' => '',
  32.             'id' => 'act_kerkdienst_liturgie',
  33.             'type' => 'textarea',
  34.             'default' => ''
  35.         )
  36.     )
  37. );
  38.  
  39. //Add meta boxes to post types
  40. function plib_add_box() {
  41.     global $meta_box;
  42.    
  43.     foreach($meta_box as $post_type => $value) {
  44.         add_meta_box($value['id'], $value['title'], 'plib_format_box', $post_type, $value['context'], $value['priority']);
  45.     }
  46. }
  47.  
  48. add_action('admin_menu', 'plib_add_box');
  49.  
  50. //Format meta boxes
  51. function plib_format_box() {
  52.   global $meta_box, $post;
  53.  
  54.   // Use nonce for verification
  55.   echo '<input type="hidden" name="plib_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
  56.  
  57.   echo '<table class="form-table">';
  58.  
  59.   foreach ($meta_box[$post->post_type]['fields'] as $field) {
  60.       // get current post meta data
  61.       $meta = get_post_meta($post->ID, $field['id'], true);
  62.  
  63.       echo '<tr>'.
  64.               '<th style="width:20%"><label for="'. $field['id'] .'">'. $field['name']. '</label></th>'.
  65.               '<td>';
  66.       switch ($field['type']) {
  67.           case 'text':
  68.               echo '<input type="text" name="'. $field['id']. '" id="'. $field['id'] .'" value="'. ($meta ? $meta : $field['default']) . '" size="30" style="width:97%" />'. '<br />'. $field['desc'];
  69.               break;
  70.           case 'textarea':
  71.               echo '<textarea name="'. $field['id']. '" id="'. $field['id']. '" cols="60" rows="4" style="width:97%">'. ($meta ? $meta : $field['default']) . '</textarea>'. '<br />'. $field['desc'];
  72.               break;
  73.           case 'select':
  74.               echo '<select name="'. $field['id'] . '" id="'. $field['id'] . '">';
  75.               foreach ($field['options'] as $option) {
  76.                   echo '<option '. ( $meta == $option ? ' selected="selected"' : '' ) . '>'. $option . '</option>';
  77.               }
  78.               echo '</select>';
  79.               break;
  80.           case 'radio':
  81.               foreach ($field['options'] as $option) {
  82.                   echo '<input type="radio" name="' . $field['id'] . '" value="' . $option['value'] . '"' . ( $meta == $option['value'] ? ' checked="checked"' : '' ) . ' />' . $option['name'];
  83.               }
  84.               break;
  85.           case 'checkbox':
  86.               echo '<input type="checkbox" name="' . $field['id'] . '" id="' . $field['id'] . '"' . ( $meta ? ' checked="checked"' : '' ) . ' />';
  87.               break;
  88.       }
  89.       echo     '<td>'.'</tr>';
  90.   }
  91.  
  92.   echo '</table>';
  93.  
  94. }
  95.  
  96. // Save data from meta box
  97. function plib_save_data($post_id) {
  98.     global $meta_box,  $post;
  99.    
  100.     //Verify nonce
  101.     if (!wp_verify_nonce($_POST['plib_meta_box_nonce'], basename(__FILE__))) {
  102.         return $post_id;
  103.     }
  104.  
  105.     //Check autosave
  106.     if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
  107.         return $post_id;
  108.     }
  109.  
  110.     //Check permissions
  111.     if ('page' == $_POST['post_type']) {
  112.         if (!current_user_can('edit_page', $post_id)) {
  113.             return $post_id;
  114.         }
  115.     } elseif (!current_user_can('edit_post', $post_id)) {
  116.         return $post_id;
  117.     }
  118.    
  119.     foreach ($meta_box[$post->post_type]['fields'] as $field) {
  120.         $old = get_post_meta($post_id, $field['id'], true);
  121.         $new = $_POST[$field['id']];
  122.        
  123.         if ($new && $new != $old) {
  124.             update_post_meta($post_id, $field['id'], $new);
  125.         } elseif ('' == $new && $old) {
  126.             delete_post_meta($post_id, $field['id'], $old);
  127.         }
  128.     }
  129. }
  130.  
  131. add_action('save_post', 'plib_save_data');
  132.  
  133.  
  134.  
  135. ?>