Advertisement
Guest User

Custop post type + Meta box + Save fields

a guest
Apr 30th, 2013
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.55 KB | None | 0 0
  1. <?php
  2.  
  3. //Custom post type: restaurant
  4.  
  5. add_action( 'init', 'create_post_type' );
  6. function create_post_type() {
  7.     register_post_type( 'restaurant',
  8.         array(
  9.             'labels' => array(
  10.                 'name' => __( 'Restaurants' ),
  11.                 'singular_name' => __( 'Product' ),
  12.                 'menu_name' => __( 'Restaurants' )
  13.             ),
  14.         'public' => true,
  15.         'has_archive' => true,
  16.         'taxonomies' => array('post_tag'),
  17.         'capability_type' => 'page'
  18.         )
  19.     );
  20. }
  21.  
  22. //Custom post type restaurant: categories
  23. function restaurant_post_type_categories()
  24. {
  25.     register_taxonomy_for_object_type('category','restaurant');
  26. }
  27. add_action('init', 'restaurant_post_type_categories');
  28.  
  29. //Custom post type restaurant: metabox
  30. function add_restaurant_meta_box() {  
  31.     add_meta_box(  
  32.         'custom_meta_box', // $id  
  33.         'Restaurant data', // $title  
  34.         'restaurant_data_form', // $callback  
  35.         'restaurant', // $page  
  36.         'normal', // $context  
  37.         'high'); // $priority  
  38. }  
  39. add_action('add_meta_boxes', 'add_restaurant_meta_box');
  40.  
  41. //Custom post type restaurant: metabox data fields
  42. function restaurant_data_form()
  43. {
  44.     $prefix = 'rest_';  
  45. $rest_custom_meta_fields = array(  
  46.     array(  
  47.         'label'=> 'Address',  
  48.         'desc'  => 'Plugin use it to get map',  
  49.         'id'    => $prefix.'text_address',  
  50.         'type'  => 'text'  
  51.     ),  
  52.     //Use commented textarea to add a textarea field
  53.     /*array(  
  54.         'label'=> 'Textarea',  
  55.         'desc'  => 'A description for the field.',  
  56.         'id'    => $prefix.'textarea',  
  57.         'type'  => 'textarea'  
  58.     ),  */
  59.     array(  
  60.         'label'=> 'Postal code',  
  61.         'desc'  => 'Plugin use it to calculate distance between customer and restaurant. Can be used also to get map.',  
  62.         'id'    => $prefix.'text_postal_code',  
  63.         'type'  => 'text'  
  64.     ),  
  65.     array(  
  66.         'label'=> 'State',  
  67.         'desc'  => 'May be used to get map.',  
  68.         'id'    => $prefix.'text_state',  
  69.         'type'  => 'text'  
  70.     ),  
  71.     array(  
  72.         'label'=> 'Country',  
  73.         'desc'  => 'May be used to get map.',  
  74.         'id'    => $prefix.'text_country',  
  75.         'type'  => 'text'  
  76.     ),  
  77.     array(  
  78.         'label'=> 'City',  
  79.         'desc'  => 'May be used to get map.',  
  80.         'id'    => $prefix.'text_city',  
  81.         'type'  => 'text'  
  82.     ),  
  83.     array(  
  84.         'label'=> 'Book a table',  
  85.         'desc'  => ' This restaurants accept table booking',  
  86.         'id'    => $prefix.'checkbox_book_a_table',  
  87.         'type'  => 'checkbox'  
  88.     ),  
  89.     array(  
  90.         'label'=> 'Take away',  
  91.         'desc'  => ' This restaurants do take away',  
  92.         'id'    => $prefix.'checkbox',  
  93.         'type'  => 'checkbox'  
  94.     ),
  95.     array(  
  96.         'label'=> 'Delivery',  
  97.         'desc'  => ' This restaurants do home delivery',  
  98.         'id'    => $prefix.'checkbox',  
  99.         'type'  => 'checkbox'  
  100.     ),
  101.     array(  
  102.         'label'=> 'Table',  
  103.         'desc'  => 'Insert place-setting number.',  
  104.         'id'    => $prefix.'text_table',  
  105.         'type'  => 'text'  
  106.     ),
  107.     array(  
  108.         'label'=> 'Child area',  
  109.         'desc'  => ' Yes, there is child area',  
  110.         'id'    => $prefix.'checkbox',  
  111.         'type'  => 'checkbox'  
  112.     ),
  113.     array(  
  114.         'label'=> 'Parking',  
  115.         'desc'  => ' Yes, there is a car park',  
  116.         'id'    => $prefix.'checkbox',  
  117.         'type'  => 'checkbox'  
  118.     ),
  119. );  
  120.  
  121. //Show form fields
  122. global $post;  
  123. // Use nonce for verification  
  124. echo '<input type="hidden" name="rest_custom_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';  
  125.      
  126.     // Begin the field table and loop  
  127.     echo '<table class="form-table">';  
  128.     foreach ($rest_custom_meta_fields as $field) {  
  129.         // get value of this field if it exists for this post  
  130.         $meta = get_post_meta($post->ID, $field['id'], true);  
  131.         // begin a table row with  
  132.         echo '<tr>
  133.                <th><label for="'.$field['id'].'">'.$field['label'].'</label></th>
  134.                <td>';  
  135.                 switch($field['type'])
  136.                 {  
  137.                     case 'text':  
  138.                                 echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" />
  139.        <br /><span class="description">'.$field['desc'].'</span>';  
  140.                                 break;  
  141.                     case 'textarea':  
  142.                                 echo '<textarea name="'.$field['id'].'" id="'.$field['id'].'" cols="60" rows="4">'.$meta.'</textarea>
  143.        <br /><span class="description">'.$field['desc'].'</span>';  
  144.                                 break;
  145.                     case 'checkbox':  
  146.                                 echo '<input type="checkbox" name="'.$field['id'].'" id="'.$field['id'].'" ',$meta ? ' checked="checked"' : '','/><label for="'.$field['id'].'">'.$field['desc'].'</label>';  
  147.                                 break;  
  148.                     case 'select':  
  149.                                 echo '<select name="'.$field['id'].'" id="'.$field['id'].'">';  
  150.                                 foreach ($field['options'] as $option)
  151.                                 {  
  152.                                     echo '<option', $meta == $option['value'] ? ' selected="selected"' : '', ' value="'.$option['value'].'">'.$option['label'].'</option>';  
  153.                                 }  
  154.                                 echo '</select><br /><span class="description">'.$field['desc'].'</span>';  
  155.                                 break;  
  156.                 }
  157.         echo '</td></tr>';  
  158.     } // end foreach  
  159.     echo '</table>'; // end table  
  160. }
  161.  
  162.  
  163. //Custom post type restaurant: save metabox data
  164. function save_restaurant_custom_meta($post_id)
  165. {  
  166.     global $rest_custom_meta_fields;  
  167.    
  168.     // verify nonce  
  169.     if (isset($_POST['custom_meta_box_nonce']) && (!wp_verify_nonce($_POST['rest_custom_meta_box_nonce'], basename(__FILE__))))  
  170.         return $post_id;  
  171.     // check autosave  
  172.     if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)  
  173.         return $post_id;  
  174.     // check permissions  
  175.     if ('page' == $_POST['post_type'])
  176.     {  
  177.         if (!current_user_can('edit_page', $post_id))  
  178.         {
  179.              return $post_id;
  180.         }
  181.     }
  182.     elseif (!current_user_can('edit_post', $post_id))
  183.     {  
  184.             return $post_id;
  185.     }  
  186.      
  187.     // loop through fields and save the data  
  188.     foreach ($rest_custom_meta_fields as $field) {  
  189.         $old = get_post_meta($post_id, $field['id'], true);  
  190.         $new = $_POST[$field['id']];  
  191.         if ($new && $new != $old) {  
  192.             update_post_meta($post_id, $field['id'], $new);  
  193.         } elseif ('' == $new && $old) {  
  194.             delete_post_meta($post_id, $field['id'], $old);  
  195.         }  
  196.     } // end foreach  
  197. }  
  198. echo add_action('save_post', 'save_restaurant_custom_meta');
  199. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement