Guest User

Untitled

a guest
Jun 28th, 2013
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. <?php
  2. // Add the Meta Box
  3. function vs_add_custom_meta_box() {
  4. add_meta_box(
  5. 'custom_meta_box', // $id
  6. 'Custom Meta Box', // $title
  7. 'vs_show_custom_meta_box', // $callback
  8. 'page', // $page
  9. 'normal', // $context
  10. 'high'); // $priority
  11. }
  12. add_action('add_meta_boxes', 'vs_add_custom_meta_box');
  13.  
  14. // Field Array
  15. $prefix = 'custom_';
  16. $custom_meta_fields = array(
  17. array(
  18. 'label'=> 'Background Color',
  19. 'desc' => 'Select the color you want to appear as the page background.',
  20. 'id' => $prefix.'select',
  21. 'type' => 'select',
  22. 'options' => array (
  23. 'one' => array (
  24. 'label' => 'Orange',
  25. 'value' => 'blog'
  26. ),
  27. 'two' => array (
  28. 'label' => 'Light red',
  29. 'value' => 'services'
  30. ),
  31. 'three' => array (
  32. 'label' => 'Dark red',
  33. 'value' => 'works'
  34. ),
  35. 'four' => array (
  36. 'label' => 'Green',
  37. 'value' => 'portfolio'
  38. ),
  39. 'five' => array (
  40. 'label' => 'Light blue',
  41. 'value' => 'about'
  42. ),
  43. 'six' => array (
  44. 'label' => 'Dark blue',
  45. 'value' => 'contact'
  46. )
  47. )
  48. ),
  49. array(
  50. 'label'=> 'Main page display',
  51. 'desc' => 'Tick if this page will be displayed on the homepage. || Notice: Make sure there are no 2 pages with the same color displayed on the homepage.',
  52. 'id' => $prefix.'main',
  53. 'type' => 'checkbox'
  54. )
  55. );
  56.  
  57. // The Callback
  58. function vs_show_custom_meta_box() {
  59. global $custom_meta_fields, $post;
  60. // Use nonce for verification
  61. echo '<input type="hidden" name="custom_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';
  62.  
  63. // Begin the field table and loop
  64. echo '<table class="form-table">';
  65. foreach ($custom_meta_fields as $field) {
  66. // get value of this field if it exists for this post
  67. $meta = get_post_meta($post->ID, $field['id'], true);
  68. // begin a table row with
  69. echo '<tr>
  70. <th><label for="'.$field['id'].'">'.$field['label'].'</label></th>
  71. <td>';
  72. switch($field['type']) {
  73. // case items will go here
  74. // text
  75. case 'text':
  76. echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" />
  77. <br /><span class="description">'.$field['desc'].'</span>';
  78. break;
  79. // textarea
  80. case 'textarea':
  81. echo '<textarea name="'.$field['id'].'" id="'.$field['id'].'" cols="60" rows="4">'.$meta.'</textarea>
  82. <br /><span class="description">'.$field['desc'].'</span>';
  83. break;
  84. // checkbox
  85. case 'checkbox':
  86. echo '<input type="checkbox" name="'.$field['id'].'" id="'.$field['id'].'" ',$meta ? ' checked="checked"' : '','/>
  87. <label for="'.$field['id'].'">'.$field['desc'].'</label>';
  88. break;
  89. // select
  90. case 'select':
  91. echo '<select name="'.$field['id'].'" id="'.$field['id'].'">';
  92. foreach ($field['options'] as $option) {
  93. echo '<option', $meta == $option['value'] ? ' selected="selected"' : '', ' value="'.$option['value'].'">'.$option['label'].'</option>';
  94. }
  95. echo '</select><br /><span class="description">'.$field['desc'].'</span>';
  96. break;
  97. } //end switch
  98. echo '</td></tr>';
  99. } // end foreach
  100. echo '</table>'; // end table
  101. }
  102.  
  103. // Save the Data
  104. function vs_save_custom_meta($post_id) {
  105. global $custom_meta_fields;
  106.  
  107. // verify nonce
  108. if (!wp_verify_nonce($_POST['custom_meta_box_nonce'], basename(__FILE__)))
  109. return $post_id;
  110. // check autosave
  111. if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
  112. return $post_id;
  113. // check permissions
  114. if ('page' == $_POST['post_type']) {
  115. if (!current_user_can('edit_page', $post_id))
  116. return $post_id;
  117. } elseif (!current_user_can('edit_post', $post_id)) {
  118. return $post_id;
  119. }
  120.  
  121. // loop through fields and save the data
  122. foreach ($custom_meta_fields as $field) {
  123. $old = get_post_meta($post_id, $field['id'], true);
  124. $new = $_POST[$field['id']];
  125. if ($new && $new != $old) {
  126. update_post_meta($post_id, $field['id'], $new);
  127. } elseif ('' == $new && $old) {
  128. delete_post_meta($post_id, $field['id'], $old);
  129. }
  130. } // end foreach
  131. }
  132. add_action('save_post', 'vs_save_custom_meta');
  133. ?>
Advertisement
Add Comment
Please, Sign In to add comment