Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //Custom post type: restaurant
- add_action( 'init', 'create_post_type' );
- function create_post_type() {
- register_post_type( 'restaurant',
- array(
- 'labels' => array(
- 'name' => __( 'Restaurants' ),
- 'singular_name' => __( 'Product' ),
- 'menu_name' => __( 'Restaurants' )
- ),
- 'public' => true,
- 'has_archive' => true,
- 'taxonomies' => array('post_tag'),
- 'capability_type' => 'page'
- )
- );
- }
- //Custom post type restaurant: categories
- function restaurant_post_type_categories()
- {
- register_taxonomy_for_object_type('category','restaurant');
- }
- add_action('init', 'restaurant_post_type_categories');
- //Custom post type restaurant: metabox
- function add_restaurant_meta_box() {
- add_meta_box(
- 'custom_meta_box', // $id
- 'Restaurant data', // $title
- 'restaurant_data_form', // $callback
- 'restaurant', // $page
- 'normal', // $context
- 'high'); // $priority
- }
- add_action('add_meta_boxes', 'add_restaurant_meta_box');
- //Custom post type restaurant: metabox data fields
- function restaurant_data_form()
- {
- $prefix = 'rest_';
- $rest_custom_meta_fields = array(
- array(
- 'label'=> 'Address',
- 'desc' => 'Plugin use it to get map',
- 'id' => $prefix.'text_address',
- 'type' => 'text'
- ),
- //Use commented textarea to add a textarea field
- /*array(
- 'label'=> 'Textarea',
- 'desc' => 'A description for the field.',
- 'id' => $prefix.'textarea',
- 'type' => 'textarea'
- ), */
- array(
- 'label'=> 'Postal code',
- 'desc' => 'Plugin use it to calculate distance between customer and restaurant. Can be used also to get map.',
- 'id' => $prefix.'text_postal_code',
- 'type' => 'text'
- ),
- array(
- 'label'=> 'State',
- 'desc' => 'May be used to get map.',
- 'id' => $prefix.'text_state',
- 'type' => 'text'
- ),
- array(
- 'label'=> 'Country',
- 'desc' => 'May be used to get map.',
- 'id' => $prefix.'text_country',
- 'type' => 'text'
- ),
- array(
- 'label'=> 'City',
- 'desc' => 'May be used to get map.',
- 'id' => $prefix.'text_city',
- 'type' => 'text'
- ),
- array(
- 'label'=> 'Book a table',
- 'desc' => ' This restaurants accept table booking',
- 'id' => $prefix.'checkbox_book_a_table',
- 'type' => 'checkbox'
- ),
- array(
- 'label'=> 'Take away',
- 'desc' => ' This restaurants do take away',
- 'id' => $prefix.'checkbox',
- 'type' => 'checkbox'
- ),
- array(
- 'label'=> 'Delivery',
- 'desc' => ' This restaurants do home delivery',
- 'id' => $prefix.'checkbox',
- 'type' => 'checkbox'
- ),
- array(
- 'label'=> 'Table',
- 'desc' => 'Insert place-setting number.',
- 'id' => $prefix.'text_table',
- 'type' => 'text'
- ),
- array(
- 'label'=> 'Child area',
- 'desc' => ' Yes, there is child area',
- 'id' => $prefix.'checkbox',
- 'type' => 'checkbox'
- ),
- array(
- 'label'=> 'Parking',
- 'desc' => ' Yes, there is a car park',
- 'id' => $prefix.'checkbox',
- 'type' => 'checkbox'
- ),
- );
- //Show form fields
- global $post;
- // Use nonce for verification
- echo '<input type="hidden" name="rest_custom_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';
- // Begin the field table and loop
- echo '<table class="form-table">';
- foreach ($rest_custom_meta_fields as $field) {
- // get value of this field if it exists for this post
- $meta = get_post_meta($post->ID, $field['id'], true);
- // begin a table row with
- echo '<tr>
- <th><label for="'.$field['id'].'">'.$field['label'].'</label></th>
- <td>';
- switch($field['type'])
- {
- case 'text':
- echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" />
- <br /><span class="description">'.$field['desc'].'</span>';
- break;
- case 'textarea':
- echo '<textarea name="'.$field['id'].'" id="'.$field['id'].'" cols="60" rows="4">'.$meta.'</textarea>
- <br /><span class="description">'.$field['desc'].'</span>';
- break;
- case 'checkbox':
- echo '<input type="checkbox" name="'.$field['id'].'" id="'.$field['id'].'" ',$meta ? ' checked="checked"' : '','/><label for="'.$field['id'].'">'.$field['desc'].'</label>';
- break;
- case 'select':
- echo '<select name="'.$field['id'].'" id="'.$field['id'].'">';
- foreach ($field['options'] as $option)
- {
- echo '<option', $meta == $option['value'] ? ' selected="selected"' : '', ' value="'.$option['value'].'">'.$option['label'].'</option>';
- }
- echo '</select><br /><span class="description">'.$field['desc'].'</span>';
- break;
- }
- echo '</td></tr>';
- } // end foreach
- echo '</table>'; // end table
- }
- //Custom post type restaurant: save metabox data
- function save_restaurant_custom_meta($post_id)
- {
- global $rest_custom_meta_fields;
- // verify nonce
- if (isset($_POST['custom_meta_box_nonce']) && (!wp_verify_nonce($_POST['rest_custom_meta_box_nonce'], basename(__FILE__))))
- return $post_id;
- // check autosave
- if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
- return $post_id;
- // check permissions
- if ('page' == $_POST['post_type'])
- {
- if (!current_user_can('edit_page', $post_id))
- {
- return $post_id;
- }
- }
- elseif (!current_user_can('edit_post', $post_id))
- {
- return $post_id;
- }
- // loop through fields and save the data
- foreach ($rest_custom_meta_fields as $field) {
- $old = get_post_meta($post_id, $field['id'], true);
- $new = $_POST[$field['id']];
- if ($new && $new != $old) {
- update_post_meta($post_id, $field['id'], $new);
- } elseif ('' == $new && $old) {
- delete_post_meta($post_id, $field['id'], $old);
- }
- } // end foreach
- }
- echo add_action('save_post', 'save_restaurant_custom_meta');
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement