pustakakoran

Aneka Variasi WP Custom Meta Box di WP-Admin

Apr 1st, 2017
65
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sumber:
  2. https://code.tutsplus.com/articles/reusable-custom-meta-boxes-part-1-intro-and-basic-fields--wp-23259
  3. https://code.tutsplus.com/articles/reusable-custom-meta-boxes-part-2-advanced-fields--wp-23293
  4.  
  5.  
  6. // Add the Meta Box --->> Create a Meta Box
  7. function add_custom_meta_box() {
  8. add_meta_box(
  9. 'custom_meta_box', // $id
  10. 'Custom Meta Box', // $title
  11. 'show_custom_meta_box', // $callback
  12. 'post', // $page
  13. 'normal', // $context
  14. 'high'); // $priority
  15. }
  16. add_action('add_meta_boxes', 'add_custom_meta_box');
  17.  
  18. // Field Array -->> Create the Field Array
  19. $prefix = 'custom_';
  20. $custom_meta_fields = array(
  21. array(
  22. 'label'=> 'Text Input',
  23. 'desc' => 'A description for the field.',
  24. 'id' => $prefix.'text',
  25. 'type' => 'text'
  26. ),
  27. array(
  28. 'label'=> 'Textarea',
  29. 'desc' => 'A description for the field.',
  30. 'id' => $prefix.'textarea',
  31. 'type' => 'textarea'
  32. ),
  33. array(
  34. 'label'=> 'Checkbox Input',
  35. 'desc' => 'A description for the field.',
  36. 'id' => $prefix.'checkbox',
  37. 'type' => 'checkbox'
  38. ),
  39. array(
  40. 'label'=> 'Select Box',
  41. 'desc' => 'A description for the field.',
  42. 'id' => $prefix.'select',
  43. 'type' => 'select',
  44. 'options' => array (
  45. 'one' => array (
  46. 'label' => 'Option One',
  47. 'value' => 'one'
  48. ),
  49. 'two' => array (
  50. 'label' => 'Option Two',
  51. 'value' => 'two'
  52. ),
  53. 'three' => array (
  54. 'label' => 'Option Three',
  55. 'value' => 'three'
  56. )
  57. )
  58. )
  59. );
  60.  
  61. // The Callback -->> Outputting the Fields
  62. function show_custom_meta_box() {
  63. global $custom_meta_fields, $post;
  64. // Use nonce for verification
  65. echo '<input type="hidden" name="custom_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';
  66.  
  67. // Begin the field table and loop
  68. echo '<table class="form-table">';
  69. foreach ($custom_meta_fields as $field) {
  70. // get value of this field if it exists for this post
  71. $meta = get_post_meta($post->ID, $field['id'], true);
  72. // begin a table row with
  73. echo '<tr>
  74. <th><label for="'.$field['id'].'">'.$field['label'].'</label></th>
  75. <td>';
  76. switch($field['type']) {
  77.  
  78.  
  79.  
  80.  
  81. // TEXT
  82. case 'text':
  83. echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" />
  84. <br /><span class="description">'.$field['desc'].'</span>';
  85. break;
  86.  
  87. // TEXT AREA
  88. case 'textarea':
  89. echo '<textarea name="'.$field['id'].'" id="'.$field['id'].'" cols="60" rows="4">'.$meta.'</textarea>
  90. <br /><span class="description">'.$field['desc'].'</span>';
  91. break;
  92.  
  93. // CHECK BOX
  94. case 'checkbox':
  95. echo '<input type="checkbox" name="'.$field['id'].'" id="'.$field['id'].'" ',$meta ? ' checked="checked"' : '','/>
  96. <label for="'.$field['id'].'">'.$field['desc'].'</label>';
  97. break;
  98.  
  99. // SELECT BOX
  100. case 'select':
  101. echo '<select name="'.$field['id'].'" id="'.$field['id'].'">';
  102. foreach ($field['options'] as $option) {
  103. echo '<option', $meta == $option['value'] ? ' selected="selected"' : '', ' value="'.$option['value'].'">'.$option['label'].'</option>';
  104. }
  105. echo '</select><br /><span class="description">'.$field['desc'].'</span>';
  106. break;
  107.  
  108.  
  109.  
  110.  
  111.  
  112. } //end switch
  113. echo '</td></tr>';
  114. } // end foreach
  115. echo '</table>'; // end table
  116. }
  117.  
  118.  
  119.  
  120. // SAVE THE DATA
  121. function save_custom_meta($post_id) {
  122. global $custom_meta_fields;
  123.  
  124. // verify nonce
  125. if (!wp_verify_nonce($_POST['custom_meta_box_nonce'], basename(__FILE__)))
  126. return $post_id;
  127. // check autosave
  128. if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
  129. return $post_id;
  130. // check permissions
  131. if ('page' == $_POST['post_type']) {
  132. if (!current_user_can('edit_page', $post_id))
  133. return $post_id;
  134. } elseif (!current_user_can('edit_post', $post_id)) {
  135. return $post_id;
  136. }
  137. // loop through fields and save the data
  138. foreach ($custom_meta_fields as $field) {
  139. $old = get_post_meta($post_id, $field['id'], true);
  140. $new = $_POST[$field['id']];
  141. if ($new && $new != $old) {
  142. update_post_meta($post_id, $field['id'], $new);
  143. } elseif ('' == $new && $old) {
  144. delete_post_meta($post_id, $field['id'], $old);
  145. }
  146. } // end foreach
  147. }
  148. add_action('save_post', 'save_custom_meta');
RAW Paste Data