Advertisement
pusatdata

Create Custom MetaBox DASAR

Jan 23rd, 2017
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. KODE MEMBUAT METABOX di function.php:
  2.  
  3. function custom_post_metaboxes() {
  4. add_meta_box('ob-post', 'Custom Post', 'custom_post_func', 'post', 'normal', 'high');
  5. }
  6. add_action('admin_menu', 'custom_post_metaboxes');
  7. function custom_post_func(){
  8. global $post;
  9.  
  10. echo '<input type="hidden" name="custom_post_custom_nonce" id="ob_post_custom_nonce" value="' .
  11. wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
  12.  
  13. $custom_post_value = get_post_meta($post->ID, 'custom_post_value', true);
  14. ?>
  15. <table>
  16. <tbody>
  17. <tr>
  18. <td>Value :</td>
  19. <td><input type="text" name="custom_post_value" value="<?php echo $custom_post_value; ?>"/></td>
  20. </tr>
  21. </tbody>
  22. </table>
  23. <?php
  24. }
  25. function custom_post_func_save($post_id, $post){
  26. if ( !wp_verify_nonce( $_POST['custom_post_custom_nonce'], plugin_basename(__FILE__) )) {
  27. return $post->ID;
  28. }
  29.  
  30. if ( !current_user_can( 'edit_post', $post->ID ))
  31. return $post->ID;
  32.  
  33. $custom_post_value['custom_post_value'] = $_POST['custom_post_value'];
  34.  
  35. foreach ($custom_post_value as $key => $value) {
  36. if( $post->post_type == 'revision' ) return;
  37. if(get_post_meta($post->ID, $key, FALSE)) {
  38. update_post_meta($post->ID, $key, $value);
  39. } else {
  40. add_post_meta($post->ID, $key, $value);
  41. }
  42. if(!$value) delete_post_meta($post->ID, $key);
  43. }
  44. }
  45. add_action('save_post', 'custom_post_func_save', 1, 2);
  46.  
  47. ==========================================
  48.  
  49. KODE MENAMPILKAN METABOX di single.php
  50. $custom_post_box = get_post_meta(get_the_ID(), "custom_post_value", true);
  51. echo $custom_post_box;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement