Public paste
Undefined
By: Guest | Date: Apr 19 2012 20:38 | Format: None | Expires: never | Size: 5.57 KB | Hits: 747

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