Advertisement
Guest User

Untitled

a guest
Jun 28th, 2013
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. <?php
  2. // Add the Meta Box
  3. function vs_add_excerpt_meta_box() {
  4. add_meta_box(
  5. 'excerpt_meta_box', // $id
  6. 'Excerpt Meta Box', // $title
  7. 'vs_show_excerpt_meta_box', // $callback
  8. 'post', // $page
  9. 'normal', // $context
  10. 'high'); // $priority
  11. }
  12. add_action('add_meta_boxes', 'vs_add_excerpt_meta_box');
  13.  
  14. // Field Array
  15. $prefix = 'custom_';
  16. $custom_meta_fields = array(
  17. array(
  18. 'label' => 'Custom post teaser',
  19. 'desc' => 'Replace the default excerpt the text of your choice',
  20. 'id' => $prefix.'excerpt',
  21. 'type' => 'textarea'
  22. )
  23. );
  24.  
  25. // The Callback
  26. function vs_show_excerpt_meta_box() {
  27. global $custom_meta_fields, $post;
  28. // Use nonce for verification
  29. echo '<input type="hidden" name="excerpt_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';
  30.  
  31. // Begin the field table and loop
  32. echo '<table class="form-table">';
  33. foreach ($custom_meta_fields as $field) {
  34. // get value of this field if it exists for this post
  35. $meta = get_post_meta($post->ID, $field['id'], true);
  36. // begin a table row with
  37. echo '<tr>
  38. <th><label for="'.$field['id'].'">'.$field['label'].'</label></th>
  39. <td>';
  40. switch($field['type']) {
  41. // case items will go here
  42. // text
  43. case 'text':
  44. echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" />
  45. <br /><span class="description">'.$field['desc'].'</span>';
  46. break;
  47. // textarea
  48. case 'textarea':
  49. echo '<textarea name="'.$field['id'].'" id="'.$field['id'].'" cols="60" rows="4">'.$meta.'</textarea>
  50. <br /><span class="description">'.$field['desc'].'</span>';
  51. break;
  52. // checkbox
  53. case 'checkbox':
  54. echo '<input type="checkbox" name="'.$field['id'].'" id="'.$field['id'].'" ',$meta ? ' checked="checked"' : '','/>
  55. <label for="'.$field['id'].'">'.$field['desc'].'</label>';
  56. break;
  57. // select
  58. case 'select':
  59. echo '<select name="'.$field['id'].'" id="'.$field['id'].'">';
  60. foreach ($field['options'] as $option) {
  61. echo '<option', $meta == $option['value'] ? ' selected="selected"' : '', ' value="'.$option['value'].'">'.$option['label'].'</option>';
  62. }
  63. echo '</select><br /><span class="description">'.$field['desc'].'</span>';
  64. break;
  65. } //end switch
  66. echo '</td></tr>';
  67. } // end foreach
  68. echo '</table>'; // end table
  69. }
  70.  
  71. // Save the Data
  72. function vs_save_excerpt_meta($post_id) {
  73. global $custom_meta_fields;
  74.  
  75. // verify nonce
  76. if (!wp_verify_nonce($_POST['excerpt_meta_box_nonce'], basename(__FILE__)))
  77. return $post_id;
  78. // check autosave
  79. if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
  80. return $post_id;
  81. // check permissions
  82. if ('page' == $_POST['post_type']) {
  83. if (!current_user_can('edit_page', $post_id))
  84. return $post_id;
  85. } elseif (!current_user_can('edit_post', $post_id)) {
  86. return $post_id;
  87. }
  88.  
  89. // loop through fields and save the data
  90. foreach ($custom_meta_fields as $field) {
  91. $old = get_post_meta($post_id, $field['id'], true);
  92. $new = $_POST[$field['id']];
  93. if ($new && $new != $old) {
  94. update_post_meta($post_id, $field['id'], $new);
  95. } elseif ('' == $new && $old) {
  96. delete_post_meta($post_id, $field['id'], $old);
  97. }
  98. } // end foreach
  99. }
  100. add_action('save_post', 'vs_save_custom_meta');
  101. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement