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

  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. foreach($meta_box as $post_type => $value) {
  43. add_meta_box($value['id'], $value['title'], 'plib_format_box', $post_type, $value['context'], $value['priority']);
  44. }
  45. }
  46.  
  47. add_action('admin_menu', 'plib_add_box');
  48.  
  49. //Format meta boxes
  50. function plib_format_box() {
  51. global $meta_box, $post;
  52. // Use nonce for verification
  53. echo '<input type="hidden" name="plib_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
  54. echo '<table class="form-table">';
  55. foreach ($meta_box[$post->post_type]['fields'] as $field) {
  56. // get current post meta data
  57. $meta = get_post_meta($post->ID, $field['id'], true);
  58. echo '<tr>'.
  59. '<th style="width:20%"><label for="'. $field['id'] .'">'. $field['name']. '</label></th>'.
  60. '<td>';
  61. switch ($field['type']) {
  62. case 'text':
  63. echo '<input type="text" name="'. $field['id']. '" id="'. $field['id'] .'" value="'. ($meta ? $meta : $field['default']) . '" size="30" style="width:97%" />'. '<br />'. $field['desc'];
  64. break;
  65. case 'textarea':
  66. echo '<textarea name="'. $field['id']. '" id="'. $field['id']. '" cols="60" rows="4" style="width:97%">'. ($meta ? $meta : $field['default']) . '</textarea>'. '<br />'. $field['desc'];
  67. break;
  68. case 'select':
  69. echo '<select name="'. $field['id'] . '" id="'. $field['id'] . '">';
  70. foreach ($field['options'] as $option) {
  71. echo '<option '. ( $meta == $option ? ' selected="selected"' : '' ) . '>'. $option . '</option>';
  72. }
  73. echo '</select>';
  74. break;
  75. case 'radio':
  76. foreach ($field['options'] as $option) {
  77. echo '<input type="radio" name="' . $field['id'] . '" value="' . $option['value'] . '"' . ( $meta == $option['value'] ? ' checked="checked"' : '' ) . ' />' . $option['name'];
  78. }
  79. break;
  80. case 'checkbox':
  81. echo '<input type="checkbox" name="' . $field['id'] . '" id="' . $field['id'] . '"' . ( $meta ? ' checked="checked"' : '' ) . ' />';
  82. break;
  83. }
  84. echo '<td>'.'</tr>';
  85. }
  86. echo '</table>';
  87. }
  88.  
  89. // Save data from meta box
  90. function plib_save_data($post_id) {
  91. global $meta_box, $post;
  92. //Verify nonce
  93. if (!wp_verify_nonce($_POST['plib_meta_box_nonce'], basename(__FILE__))) {
  94. return $post_id;
  95. }
  96. //Check autosave
  97. if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
  98. return $post_id;
  99. }
  100. //Check permissions
  101. if ('page' == $_POST['post_type']) {
  102. if (!current_user_can('edit_page', $post_id)) {
  103. return $post_id;
  104. }
  105. } elseif (!current_user_can('edit_post', $post_id)) {
  106. return $post_id;
  107. }
  108. foreach ($meta_box[$post->post_type]['fields'] as $field) {
  109. $old = get_post_meta($post_id, $field['id'], true);
  110. $new = $_POST[$field['id']];
  111. if ($new && $new != $old) {
  112. update_post_meta($post_id, $field['id'], $new);
  113. } elseif ('' == $new && $old) {
  114. delete_post_meta($post_id, $field['id'], $old);
  115. }
  116. }
  117. }
  118. add_action('save_post', 'plib_save_data');
  119.  
  120.  
  121.  
  122. ?>