Posted By

hienluvshien on 07/29/11


Tagged


Versions (?)

Custom Fields


 / Published in: PHP
 

  1. <?php
  2. add_action( 'init', 'create_product' );
  3. function create_product() {
  4. $labels = array(
  5. 'name' => _x('Products', 'post type general name'),
  6. 'singular_name' => _x('Product', 'post type singular name'),
  7. 'add_new' => _x('Add New', 'Product'),
  8. 'add_new_item' => __('Add New Product'),
  9. 'edit_item' => __('Edit Product'),
  10. 'new_item' => __('New Product'),
  11. 'view_item' => __('View Product'),
  12. 'search_items' => __('Search Products'),
  13. 'not_found' => __('No Products found'),
  14. 'not_found_in_trash' => __('No Products found in Trash'),
  15. 'parent_item_colon' => ''
  16. );
  17.  
  18. $supports = array('title', 'revisions', 'thumbnail', 'editor');
  19.  
  20. register_post_type( 'product',
  21. 'labels' => $labels,
  22. 'public' => true,
  23. 'supports' => $supports
  24. )
  25. );
  26. }
  27. ?>
  28. <?php
  29.  
  30. $meta_box = array(
  31. 'id' => 'my-meta-box',
  32. 'title' => 'Product Options',
  33. 'page' => 'product',
  34. 'context' => 'normal',
  35. 'priority' => 'high',
  36. 'fields' => array(
  37. 'name' => 'Price',
  38. 'desc' => 'Enter the price of the product starting with $.',
  39. 'id' => 'price',
  40. 'type' => 'text',
  41. 'std' => ''
  42. ),
  43. 'name' => 'Dimensions',
  44. 'desc' => 'Enter the dimensions of the product.',
  45. 'id' => 'dimensions',
  46. 'type' => 'text',
  47. 'std' => ''
  48. ),
  49. 'name' => 'Colors',
  50. 'desc' => 'Enter to colors seperating each value.',
  51. 'id' => 'colors',
  52. 'type' => 'text',
  53. 'std' => ''
  54. ),
  55. 'name' => 'Description',
  56. 'desc' => 'Enter the description of the product here, to enter the details, write it in the large text box underneath the title.',
  57. 'id' => 'description',
  58. 'type' => 'textarea',
  59. 'std' => ''
  60. )
  61. )
  62. );
  63. add_action('admin_menu', 'mytheme_add_box');
  64.  
  65. // Add meta box
  66. function mytheme_add_box() {
  67. global $meta_box;
  68.  
  69. add_meta_box($meta_box['id'], $meta_box['title'], 'mytheme_show_box', $meta_box['page'], $meta_box['context'], $meta_box['priority']);
  70. }
  71. // Callback function to show fields in meta box
  72. function mytheme_show_box() {
  73. global $meta_box, $post;
  74.  
  75. // Use nonce for verification
  76. echo '<input type="hidden" name="mytheme_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
  77.  
  78. echo '<table class="form-table">';
  79.  
  80. foreach ($meta_box['fields'] as $field) {
  81. // get current post meta data
  82. $meta = get_post_meta($post->ID, $field['id'], true);
  83.  
  84. echo '<tr>',
  85. '<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>',
  86. '<td>';
  87. switch ($field['type']) {
  88. case 'price':
  89. echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />', '<br />', $field['desc'];
  90. break;
  91. case 'dimensions':
  92. echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />', '<br />', $field['desc'];
  93. break;
  94. case 'colors':
  95. echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />', '<br />', $field['desc'];
  96. break;
  97. case 'description':
  98. echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>', '<br />', $field['desc'];
  99. break;
  100. }
  101. echo '<td>',
  102. '</tr>';
  103. }
  104.  
  105. echo '</table>';
  106. }
  107. add_action('save_post', 'mytheme_save_data');
  108.  
  109. // Save data from meta box
  110. function mytheme_save_data($post_id) {
  111. global $meta_box;
  112.  
  113. // verify nonce
  114. if (!wp_verify_nonce($_POST['mytheme_meta_box_nonce'], basename(__FILE__))) {
  115. return $post_id;
  116. }
  117.  
  118. // check autosave
  119. if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
  120. return $post_id;
  121. }
  122.  
  123. // check permissions
  124. if ('page' == $_POST['post_type']) {
  125. if (!current_user_can('edit_page', $post_id)) {
  126. return $post_id;
  127. }
  128. } elseif (!current_user_can('edit_post', $post_id)) {
  129. return $post_id;
  130. }
  131.  
  132. foreach ($meta_box['fields'] as $field) {
  133. $old = get_post_meta($post_id, $field['id'], true);
  134. $new = $_POST[$field['id']];
  135.  
  136. if ($new && $new != $old) {
  137. update_post_meta($post_id, $field['id'], $new);
  138. } elseif ('' == $new && $old) {
  139. delete_post_meta($post_id, $field['id'], $old);
  140. }
  141. }
  142. }
  143. ?>

Report this snippet  

You need to login to post a comment.