Advertisement
Syrehn

Meta Box Creation

Mar 5th, 2013
1,243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. // META BOX CODE
  2.  
  3. $my_info_metabox = array(
  4. 'id' => 'my-info',
  5. 'title' => 'My Info',
  6. 'page' => array('my_info'),
  7. 'context' => 'normal',
  8. 'priority' => 'default',
  9. 'fields' => array(
  10. array(
  11. 'name' => 'Box 1',
  12. 'desc' => '',
  13. 'id' => 'myinfo-box1',
  14. 'class' => 'myinfo-box1',
  15. 'type' => 'textarea',
  16. 'rich_editor' => 1,
  17. 'max' => 0
  18. ),
  19. array(
  20. 'name' => 'Box 2',
  21. 'desc' => '',
  22. 'id' => 'myinfo-box2',
  23. 'class' => 'myinfo-box2',
  24. 'type' => 'textarea',
  25. 'rich_editor' => 1,
  26. 'max' => 0
  27. ),
  28. array(
  29. 'name' => 'Box 3',
  30. 'desc' => '',
  31. 'id' => 'myinfo-box3',
  32. 'class' => 'myinfo-box3',
  33. 'type' => 'textarea',
  34. 'rich_editor' => 1,
  35. 'max' => 0
  36. ),
  37. )
  38. );
  39.  
  40. add_action('admin_init', 'add_my_info_meta_box');
  41.  
  42. function add_my_info_meta_box() {
  43.  
  44. global $my_info_metabox;
  45.  
  46. foreach($my_info_metabox['page'] as $page) {
  47. add_meta_box($my_info_metabox['id'], $my_info_metabox['title'], 'show_my_info_box', $page, 'normal', 'default', $my_info_metabox);
  48. }
  49. }
  50.  
  51. // function to show meta boxes
  52. function show_my_info_box() {
  53. global $post;
  54. global $my_info_metabox;
  55. global $wp_version;
  56.  
  57. // Use nonce for verification
  58. echo '<input type="hidden" name="my_info_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
  59.  
  60. echo '<table class="form-table">';
  61.  
  62. foreach ($my_info_metabox['fields'] as $field) {
  63. // get current post meta data
  64.  
  65. $meta = get_post_meta($post->ID, $field['id'], true);
  66.  
  67. echo '<tr>',
  68. '<th style="width:20%"><label for="', $field['id'], '">', stripslashes($field['name']), '</label></th>',
  69. '<td class="myinfo_field_type_' . str_replace(' ', '_', $field['type']) . '">';
  70. switch ($field['type']) {
  71. case 'textarea':
  72.  
  73. if($field['rich_editor'] == 1) {
  74. if($wp_version >= 3.3) {
  75. echo wp_editor($meta, $field['id'], array('textarea_name' => $field['id']));
  76. } else {
  77. // older versions of WP
  78. $editor = '';
  79. if(!post_type_supports($post->post_type, 'editor')) {
  80. $editor = wp_tiny_mce(true, array('editor_selector' => $field['class'], 'remove_linebreaks' => false) );
  81. }
  82. $field_html = '<div style="width: 97%; border: 1px solid #DFDFDF;"><textarea name="' . $field['id'] . '" class="' . $field['class'] . '" id="' . $field['id'] . '" cols="60" rows="8" style="width:100%">'. $meta . '</textarea></div><br/>' . __(stripslashes($field['desc']));
  83. echo $editor . $field_html;
  84. }
  85. } else {
  86. echo '<div style="width: 100%;"><textarea name="', $field['id'], '" class="', $field['class'], '" id="', $field['id'], '" cols="60" rows="8" style="width:97%">', $meta ? $meta : $field['std'], '</textarea></div>', '', stripslashes($field['desc']);
  87. }
  88.  
  89. break;
  90.  
  91. }
  92. echo '<td>',
  93. '</tr>';
  94. }
  95.  
  96. echo '</table>';
  97. }
  98.  
  99. // Save data from meta box
  100. add_action('save_post', 'my_info_metabox_save');
  101. function my_info_metabox_save($post_id) {
  102. global $post;
  103. global $my_info_metabox;
  104.  
  105. // verify nonce
  106. if ( ! isset( $_POST['my_info_meta_box_nonce'] ) || ! wp_verify_nonce($_POST['my_info_meta_box_nonce'], basename(__FILE__))) {
  107. return $post_id;
  108. }
  109.  
  110. // check autosave
  111. if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
  112. return $post_id;
  113. }
  114.  
  115. // check permissions
  116. if ('page' == $_POST['post_type']) {
  117. if (!current_user_can('edit_page', $post_id)) {
  118. return $post_id;
  119. }
  120. } elseif (!current_user_can('edit_post', $post_id)) {
  121. return $post_id;
  122. }
  123.  
  124. foreach ($my_info_metabox['fields'] as $field) {
  125.  
  126. $old = get_post_meta($post_id, $field['id'], true);
  127. $new = $_POST[$field['id']];
  128.  
  129. if ($new && $new != $old) {
  130. if($field['type'] == 'date') {
  131. $new = ssv_locations_format_date($new);
  132. update_post_meta($post_id, $field['id'], $new);
  133. } else {
  134. if(is_string($new)) {
  135. $new = $new;
  136. }
  137. update_post_meta($post_id, $field['id'], $new);
  138.  
  139.  
  140. }
  141. } elseif ('' == $new && $old) {
  142. delete_post_meta($post_id, $field['id'], $old);
  143. }
  144. }
  145. }
  146.  
  147.  
  148. // converts a time stamp to date string for meta fields
  149. if(!function_exists('my_info_timestamp_to_date')) {
  150. function my_info_timestamp_to_date($date) {
  151.  
  152. return date('m/d/Y', $date);
  153. }
  154. }
  155. // was ecpt_format_date
  156. if(!function_exists('my_info_format_date')) {
  157. function my_info_format_date($date) {
  158.  
  159. $date = strtotime($date);
  160.  
  161. return $date;
  162. }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement