pusatdata

Custom Meta Box Pustakaalwadi.com

Mar 14th, 2018 (edited)
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.62 KB | None | 0 0
  1. function.php
  2. ==================
  3.  
  4. add_action('meta_box','new_meta_boxes');
  5. //META BOX
  6. /*
  7. ---------------------
  8. Custom Field
  9. ---------------------
  10. */
  11.  
  12. $new_meta_boxes =
  13. array(
  14. "kotak-isian-01" => array(
  15. "name" => "pentahqiq",
  16. "title" => "Pentahqiq",
  17. "description" => ""),
  18. "kotak-isian-02" => array(
  19. "name" => "sampul",
  20. "title" => "Sampul",
  21. "description" => ""),
  22. "kotak-isian-03" => array(
  23. "name" => "kertas",
  24. "title" => "Kertas",
  25. "description" => ""),
  26. "kotak-isian-04" => array(
  27. "name" => "jilid",
  28. "title" => "Jilid",
  29. "description" => ""),
  30. "kotak-isian-05" => array(
  31. "name" => "pembahasan",
  32. "title" => "Pembahasan",
  33. "description" => ""),
  34. );
  35. //end of array
  36.  
  37.  
  38. //now we begin a function to create the html that comprise the new meta boxes
  39. //below you can edit/add inline css if you want to change how the label and description appear on the admin page
  40. //worth testing, but not important to manipulate this portion of the code unless you have a super specific objective in mind.
  41. function new_meta_boxes() {
  42. global $post, $new_meta_boxes;
  43.  
  44. foreach($new_meta_boxes as $meta_box) {
  45. $meta_box_value = get_post_meta($post->ID, $meta_box['name'], true);
  46.  
  47. if($meta_box_value == "")
  48. $meta_box_value = $meta_box['std'];
  49.  
  50. echo'<input type="hidden" name="'.$meta_box['name'].'_noncename" id="'.$meta_box['name'].'_noncename"
  51. value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';
  52.  
  53. echo'<label style="font-weight: bold; display: block; padding: 5px 0 2px 2px"
  54. for="'.$meta_box['name'].'">'.$meta_box['title'].'</label>';
  55.  
  56. echo'<input type="text" name="'.$meta_box['name'].'" value="'.$meta_box_value.'" size="55" /><br />';
  57.  
  58. echo'<p><label for="'.$meta_box['name'].'">'.$meta_box['description'].'</label></p>';
  59. }
  60. }//end of new_meta_boxes() function
  61.  
  62.  
  63. //another function begins here. This is what actually creates the meta box. The one you see on the admin page.
  64. //you should edit the title for your meta box below. don't worry about anything else
  65. function create_meta_box() {
  66. global $theme_name;
  67. if ( function_exists('add_meta_box') ) {
  68. add_meta_box( 'new-meta-boxes', 'DETAIL KITAB', 'new_meta_boxes', 'post', 'normal', 'high' );
  69. }
  70. }//end of create_meta_box() function
  71.  
  72. //the third and most important function. this is what effectively saves your meta data into your database.
  73. //DO NOT EDIT unless you have expert php skills or advice.
  74. function save_postdata( $post_id ) {
  75. global $post, $new_meta_boxes;
  76.  
  77. foreach($new_meta_boxes as $meta_box) {
  78. // Verify
  79. if ( !wp_verify_nonce( $_POST[$meta_box['name'].'_noncename'], plugin_basename(__FILE__) )) {
  80. return $post_id;
  81. }
  82.  
  83. if ( 'page' == $_POST['post_type'] ) {
  84. if ( !current_user_can( 'edit_page', $post_id ))
  85. return $post_id;
  86. } else {
  87. if ( !current_user_can( 'edit_post', $post_id ))
  88. return $post_id;
  89. }
  90.  
  91. $data = $_POST[$meta_box['name']];
  92.  
  93. if(get_post_meta($post_id, $meta_box['name']) == "")
  94. add_post_meta($post_id, $meta_box['name'], $data, true);
  95. elseif($data != get_post_meta($post_id, $meta_box['name'], true))
  96. update_post_meta($post_id, $meta_box['name'], $data);
  97. elseif($data == "")
  98. delete_post_meta($post_id, $meta_box['name'], get_post_meta($post_id, $meta_box['name'], true));
  99. }
  100. }//end of save_postdata () function
  101.  
  102. //these are action hooks that place your special functions defined above onto the admin page.
  103. add_action('admin_menu', 'create_meta_box');
  104. add_action('save_post', 'save_postdata');
  105.  
  106.  
  107.  
  108. single.php
  109. ================
  110. <h4 style="margin:0 8px 6px 0px; padding-left:20px;">Data Buku</h4>
  111. <table>
  112. <?php
  113. $my_title = get_the_title();
  114. $my_date = get_post_meta( get_the_ID(), 'date', true);
  115. $my_pentahqiq = get_post_meta( get_the_ID(), 'pentahqiq', true);
  116. $my_sampul = get_post_meta( get_the_ID(), 'sampul', true);
  117. $my_kertas = get_post_meta( get_the_ID(), 'kertas', true);
  118. $my_jilid = get_post_meta( get_the_ID(), 'jilid', true);
  119. $my_pembahasan = get_post_meta( get_the_ID(), 'pembahasan', true);
  120. $my_publisher = get_the_category();
  121. $my_penerbit = get_the_tags();
  122.  
  123. // Here we save the "Genre" terms in a variable.
  124. $penulis_terms = get_the_term_list( $post->ID, 'penulis', '', ', ', ' ' );
  125.  
  126. if( ! empty( $my_title ) ) {
  127. echo '<tr><td align="right" class="style" width="210"><b>Judul</td><td align="center" class="style">:</td></b><td class="style"> ' . $my_title . '</td></tr>';
  128. }
  129.  
  130. // Here "Penulis" are displayed if not empty.
  131. if( ! empty( $penulis_terms ) ) {
  132. echo '<tr><td align="right" class="style"><b>Penulis</td><td align="center" class="style">:</td></b><td class="style"> ' . $penulis_terms . ' </td></tr>';
  133. }
  134.  
  135. if( ! empty( $my_pentahqiq ) ) {
  136. echo '<tr><td align="right" class="style"><b>Pentahqiq</td><td align="center" class="style">:</td></b><td class="style"> ' . $my_pentahqiq . '</td></tr>';
  137. }
  138.  
  139. if( ! empty( $my_sampul ) ) {
  140. echo '<tr><td align="right" class="style"><b>Sampul</td><td align="center" class="style">:</td></b><td class="style"> ' . $my_sampul . '</td></tr>';
  141. }
  142.  
  143. if( ! empty( $my_kertas ) ) {
  144. echo '<tr><td align="right" class="style"><b>Kertas</td><td align="center" class="style">:</td></b><td class="style"> ' . $my_kertas . '</td></tr>';
  145. }
  146.  
  147. if( ! empty( $my_penerbit ) ) {
  148. $tag_links = array();
  149. foreach($my_penerbit as $tag) {
  150. $tag_links[] = '<a href="'.get_tag_link($tag).'">'.$tag->name.'</a>';
  151. }
  152.  
  153. if( ! empty( $my_jilid ) ) {
  154. echo '<tr><td align="right" class="style"><b>Jilid</td><td align="center" class="style">:</td></b><td class="style"> ' . $my_jilid . '</td></tr>';
  155. }
  156.  
  157. if( ! empty( $my_pembahasan ) ) {
  158. echo '<tr><td align="right" class="style"><b>Pembahasan</td><td align="center" class="style">:</td></b><td class="style"> ' . $my_pembahasan . '</td></tr>';
  159. }
  160.  
  161. ?>
  162. </table>
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171. ======================
  172.  
  173.  
  174.  
  175.  
  176. if( ! empty( $my_date ) ) {
  177. echo '<tr><td align="right" class="style"><b>Release Date</td><td align="center" class="style">:</td></b><td class="style"> ' . $my_date . '</td></tr>';
  178. }
  179. if( ! empty( $my_isbn ) ) {
  180. echo '<tr><td align="right" class="style"><b>ISBN</td><td align="center" class="style">:</td></b><td class="style"> ' . $my_isbn . '</td></tr>';
  181. }
  182. if( ! empty( $my_publisher[0] ) ) {
  183. echo '<tr><td align="right" class="style"><b>Publisher</td><td align="center" class="style">:</td></b><td class="style"> <a href="'.get_category_link($my_publisher[0]->term_id ).'">'.$my_publisher[0]->cat_name.'</a></td></tr>';
  184. }
  185. if( ! empty( $my_author ) ) {
  186. $tag_links = array();
  187. foreach($my_author as $tag) {
  188. $tag_links[] = '<a href="'.get_tag_link($tag).'">'.$tag->name.'</a>';
  189. }
  190. echo '<tr><td align="right" class="style"><b>Author</td><td align="center" class="style">:</td></b><td class="style"> ' . implode(', ', $tag_links) . ' </td></tr>';
  191. }
  192.  
  193. ====================
  194.  
  195. <table>
  196. <tr>
  197. <td align="right" class="databuku kiri" style="vertical-align: middle;">Judul</td>
  198. <td align="center" class="databuku titikdua">:</td>
  199. <td class="databuku kanan"><?php the_title(); ?></td>
  200. </tr>
  201. <tr>
  202. <td align="right" class="databuku kiri" style="vertical-align: middle;">Penulis</td>
  203. <td align="center" class="databuku titikdua">:</td>
  204. <td class="databuku kanan"><?php echo get_post_meta($post->ID, "penulis", true);?></td>
  205. </tr>
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215. <tr>
  216. <td style="text-align: center;vertical-align: middle;" width="248" colspan="4">
  217.  
  218.  
  219. <img src="<?php echo get_post_meta($post->ID, "label", true);?>" style="width:150px;align:center;" onError="this.src='/belum-terverifikasi.png';"/>
  220. <h2 class="title"><?php the_title(); ?></h2>
  221. </td>
  222. </tr>
  223.  
  224. <tr>
  225. <td style="text-align: center;vertical-align: middle; width:150px" rowspan="6"><img src="/img/cover/<?php echo get_the_ID(); ?>.jpg" width="150px" class="bw" onError="this.src='/no-cover.png';"/></td>
  226.  
  227. <td align="right" class="databuku kiri" style="vertical-align: middle;" >Penulis</td>
  228. <td align="center" class="databuku titikdua">:</td>
  229. <td class="databuku kanan"><?php echo get_post_meta($post->ID, "penulis", true);?></td>
  230. </tr>
  231. <tr>
  232. <td align="right" class="databuku kiri" style="vertical-align: middle;">Penerbit</td>
  233. <td align="center" class="databuku titikdua">:</td>
  234. <td class="databuku kanan"><?php the_category(', '); ?></td>
  235. </tr>
  236. <tr>
  237. <td align="right" class="databuku kiri" style="vertical-align: middle;">Terbit</td>
  238. <td align="center" class="databuku titikdua">:</td>
  239. <td class="databuku kanan"><?php the_time('Y'); ?></td>
  240. </tr>
  241. <tr>
  242. <td align="right" class="databuku kiri" style="vertical-align: middle;">Cetakan</td>
  243. <td align="center" class="databuku titikdua">:</td>
  244. <td class="databuku kanan"><?php echo get_post_meta($post->ID, "cetakan", true);?></td>
  245. </tr>
  246. <tr>
  247. <td align="right" class="databuku kiri" style="vertical-align: middle;">Status MUBI</td>
  248. <td align="center" class="databuku titikdua">:</td>
  249. <td class="databuku kanan"><?php
  250. $id_verifikasi = get_post_meta( get_the_ID(), 'verifikasi', true);
  251. if( ! empty( $id_verifikasi ) ) {
  252. echo '' . $id_verifikasi . '';
  253. }
  254. ?></td>
  255. </tr>
  256. </table>
Add Comment
Please, Sign In to add comment