Advertisement
Guest User

Wordpress custom metabox

a guest
Apr 4th, 2013
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. <?php
  2. // Add the Meta Box
  3. function add_custom_meta_box() {
  4. add_meta_box(
  5. 'custom_meta_box', // $id
  6. 'Custom Meta Box', // $title
  7. 'show_custom_meta_box', // $callback
  8. 'event', // $page
  9. 'normal', // $context
  10. 'high'); // $priority
  11. }
  12. add_action('add_meta_boxes', 'add_custom_meta_box');
  13.  
  14. // Field Array
  15. $prefix = 'custom_';
  16. $custom_meta_fields = array(
  17.  
  18.  
  19. array(
  20. 'label' => 'Post List',
  21. 'desc' => 'A description for the field.',
  22. 'id' => $prefix.'post_id',
  23. 'type' => 'post_list',
  24. 'post_type' => array('customer')
  25. )
  26.  
  27. );
  28.  
  29. // The Callback
  30. function show_custom_meta_box() {
  31. global $custom_meta_fields, $post;
  32. // Use nonce for verification
  33. echo '<input type="hidden" name="custom_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';
  34.  
  35. // Begin the field table and loop
  36. echo '<table class="form-table">';
  37. foreach ($custom_meta_fields as $field) {
  38. // get value of this field if it exists for this post
  39. $meta = get_post_meta($post->ID, $field['id'], true);
  40. // begin a table row with
  41. echo '<tr>
  42. <th><label for="'.$field['id'].'">'.$field['label'].'</label></th>
  43. <td>';
  44. switch($field['type']) {
  45.  
  46. // post_list
  47. case 'post_list':
  48. $items = get_posts( array (
  49. 'post_type' => $field['post_type'],
  50. 'posts_per_page' => -1
  51. ));
  52. echo '<select name="'.$field['id'].'" id="'.$field['id'].'">
  53. <option value="">Select One</option>'; // Select One
  54. foreach($items as $item) {
  55. echo '<option value="'.$item->ID.'"',$meta == $item->ID ? ' selected="selected"' : '','>'.$item->post_type.': '.$item->post_title.'</option>';
  56. } // end foreach
  57. echo '</select><br /><span class="description">'.$field['desc'].'</span>';
  58. break;
  59.  
  60.  
  61. } //end switch
  62. echo '</td></tr>';
  63. } // end foreach
  64. echo '</table>'; // end table
  65. }
  66.  
  67. function remove_taxonomy_boxes() {
  68. remove_meta_box('categorydiv', 'post', 'side');
  69. }
  70. add_action( 'admin_menu' , 'remove_taxonomy_boxes' );
  71.  
  72. // Save the Data
  73. function save_custom_meta($post_id) {
  74. global $custom_meta_fields;
  75.  
  76. // verify nonce
  77. if (!wp_verify_nonce($_POST['custom_meta_box_nonce'], basename(__FILE__)))
  78. return $post_id;
  79. // check autosave
  80. if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
  81. return $post_id;
  82. // check permissions
  83. if ('page' == $_POST['post_type']) {
  84. if (!current_user_can('edit_page', $post_id))
  85. return $post_id;
  86. } elseif (!current_user_can('edit_post', $post_id)) {
  87. return $post_id;
  88. }
  89.  
  90. // loop through fields and save the data
  91. foreach ($custom_meta_fields as $field) {
  92. if($field['type'] == 'tax_select') continue;
  93. $old = get_post_meta($post_id, $field['id'], true);
  94. $new = $_POST[$field['id']];
  95. if ($new && $new != $old) {
  96. update_post_meta($post_id, $field['id'], $new);
  97. } elseif ('' == $new && $old) {
  98. delete_post_meta($post_id, $field['id'], $old);
  99. }
  100. } // enf foreach
  101.  
  102. // save taxonomies
  103. $post = get_post($post_id);
  104. $category = $_POST['category'];
  105. wp_set_object_terms( $post_id, $category, 'category' );
  106. }
  107. add_action('save_post', 'save_custom_meta');
  108.  
  109. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement