Custom Post Type for a Menu

I created this simple plugin for a restaurant to post their menu online.

Here’s the plugin file (put in plugins directory).

It includes categories (for menu sections), and several parameters for each item.

<?php
/*
Plugin Name: Grille Menu
Description: Custom Post Type for Menu Items
Author: Diana Nichols
Version: 1.0
Author URI: http://www.lavenderthreads.com
*/

add_action( 'init', 'register_grille_menu' );

function register_grille_menu() {

    $labels = array( 
        'name' => _x( 'Grille Menu', 'menu' ),
        'singular_name' => _x( 'Grille Menu Item', 'menu' ),
        'add_new' => _x( 'Add New', 'menu' ),
        'add_new_item' => _x( 'Add New Menu Item', 'menu' ),
        'edit_item' => _x( 'Edit Item', 'menu' ),
        'new_item' => _x( 'New Item', 'menu' ),
        'view_item' => _x( 'View Item', 'menu' ),
        'search_items' => _x( 'Search Items', 'menu' ),
        'not_found' => _x( 'No items found', 'menu' ),
        'not_found_in_trash' => _x( 'No items found in Trash', 'menu' ),
        'parent_item_colon' => _x( 'Parent Item:', 'menu' ),
        'menu_name' => _x( 'Grille Menu', 'menu' ),
    );

    $args = array( 
        'labels' => $labels,
        'hierarchical' => false,        
        'supports' => array( 'title', 'editor', 'page-atributes' ),
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,   
        'show_in_nav_menus' => true,
        'publicly_queryable' => true,
        'exclude_from_search' => false,
        'has_archive' => true,
        'query_var' => true,
        'can_export' => true,
        'rewrite' => true,
        'capability_type' => 'post'
    );

    register_post_type( 'grille_menu', $args );
}

add_action( 'init', 'register_grille_taxonomy_categories' );

function register_grille_taxonomy_categories() {

    $labels = array( 
        'name' => _x( 'Grille Categories', 'categories' ),
        'singular_name' => _x( 'Category', 'categories' ),
        'search_items' => _x( 'Search Categories', 'categories' ),
        'popular_items' => _x( 'Popular Categories', 'categories' ),
        'all_items' => _x( 'All Categories', 'categories' ),
        'parent_item' => _x( 'Parent Category', 'categories' ),
        'parent_item_colon' => _x( 'Parent Category:', 'categories' ),
        'edit_item' => _x( 'Edit Category', 'categories' ),
        'update_item' => _x( 'Update Category', 'categories' ),
        'add_new_item' => _x( 'Add New Category', 'categories' ),
        'new_item_name' => _x( 'New Category', 'categories' ),
        'separate_items_with_commas' => _x( 'Separate categories with commas', 'categories' ),
        'add_or_remove_items' => _x( 'Add or remove categories', 'categories' ),
        'choose_from_most_used' => _x( 'Choose from the most used categories', 'categories' ),
        'menu_name' => _x( 'Categories', 'categories' ),
    );

    $args = array( 
        'labels' => $labels,
        'public' => true,
        'show_in_nav_menus' => true,
        'show_ui' => true,
        'show_tagcloud' => true,
        'show_admin_column' => true,
        'hierarchical' => true,
        'rewrite' => true,
        'query_var' => true
    );

    register_taxonomy( 'grille_categories', array('grille_menu'), $args );
}

// the custom fields
add_action('admin_menu', 'lt_grille_add_box'); 

$prefix = 'grille_menu_';

$grille_meta_box['grille_menu'] = array(
    'id' => 'grille-menu-meta-details',
    'title' => 'Menu Item Information',
    'context' => 'side',
    'priority' => 'default',
    'fields' => array(
		array(
		'name' => 'Price 1',
		'id' => $prefix . 'price1',
		'type' => 'text',
		'desc' => '',
		'default' => '',
		'class' => '',
		),
		array(
		'name' => 'Price Label 1',
		'id' => $prefix . 'pricelabel1',
		'type' => 'text',
		'desc' => '',
		'default' => '',
		'class' => '',
		),
		array(
		'name' => 'Price 2',
		'id' => $prefix . 'price2',
		'type' => 'text',
		'desc' => '',
		'default' => '',
		'class' => '',
		),
		array(
		'name' => 'Price Label 2',
		'id' => $prefix . 'pricelabel2',
		'type' => 'text',
		'desc' => '',
		'default' => '',
		'class' => '',
		),
		array(
		'name' => 'Signature Item',
		'id' => $prefix . 'signature',
		'type' => 'checkbox',
		'desc' => '',
		'default' => '',
		'class' => '',
		),
		array(
		'name' => 'Menu Order',
		'id' => $prefix . 'order',
		'type' => 'text',
		'desc' => '',
		'default' => '1',
		'class' => '',
		),
    )
);

//Add meta boxes to post types
function lt_grille_add_box() {
    global $grille_meta_box;
    
    foreach($grille_meta_box as $post_type => $value) {
        add_meta_box($value['id'], $value['title'], 'lt_grille_format_box', $post_type, $value['context'], $value['priority']);
    }
}

//Format meta boxes
function lt_grille_format_box() {
  global $grille_meta_box, $post;

  // Use nonce for verification
  echo '<input type="hidden" name="lt_grille_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';

  echo '<table class="form-table">';

  foreach ($grille_meta_box[$post->post_type]['fields'] as $field) {
      // get current post meta data
      $meta = get_post_meta($post->ID, $field['id'], true);

      echo '<tr>'.
              '<th style="width:20%"><label for="'. $field['id'] .'">'. $field['name']. '</label></th>'.
              '<td>';
      switch ($field['type']) {
          case 'text':
              echo '<input type="text" name="'. $field['id']. '" id="'. $field['id'] .'" value="'. ($meta ? $meta : $field['default']) . '" size="30" style="width:97%" class= "' .$field['class']. '" />'. '<br />'. $field['desc'];
              break;
          case 'textarea':
              echo '<textarea name="'. $field['id']. '" id="'. $field['id']. '" cols="60" rows="4" style="width:97%">'. ($meta ? $meta : $field['default']) . '</textarea>'. '<br />'. $field['desc'];
              break;
          case 'select':
              echo '<select name="'. $field['id'] . '" id="'. $field['id'] . '">';
              foreach ($field['options'] as $option) {
                  echo '<option '. ( $meta == $option ? ' selected="selected"' : '' ) . '>'. $option . '</option>';
              }
              echo '</select>';
              break;
          case 'radio':
              foreach ($field['options'] as $option) {
                  echo '<input type="radio" name="' . $field['id'] . '" value="' . $option['value'] . '"' . ( $meta == $option['value'] ? ' checked="checked"' : '' ) . ' />' . $option['name'];
              }
              break;
          case 'checkbox':
              echo '<input type="checkbox" name="' . $field['id'] . '" id="' . $field['id'] . '"' . ( $meta ? ' checked="checked"' : '' ) . ' />';
              break;
      }
      echo     '<td>'.'</tr>';
  }

  echo '</table>';

}

// Save data from meta box
function lt_grille_save_data($post_id) {
    global $grille_meta_box,  $post;
    
    //Verify nonce
    if (!wp_verify_nonce($_POST['lt_grille_meta_box_nonce'], basename(__FILE__))) {
        return $post_id;
    }

    //Check autosave
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return $post_id;
    }

    //Don't update on Quick Edit save
    if (defined('DOING_AJAX') ) {
	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;
    }
    
    foreach ($grille_meta_box[$post->post_type]['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);
        }
    }
}

add_action('save_post', 'lt_grille_save_data');

Now here’s the menu category display page. Create a page named “archive-grille_menu.php” and upload to your theme directory. Replace the loop with this code:

(I listed the categories in two columns.)

	$args2 = array(	'orderby' => 'name',	'order' => 'ASC',	'style' => 'none',	'taxonomy' => 'grille_categories',	'echo' => 0,'title_li'  =>'' );
	$cats = explode("<br />",wp_list_categories($args2));
	$cat_n = count($cats) - 1;
	$cat_left='';
	$cat_right= '';
	for ($i=0;$i<$cat_n;$i++):
		if ($i<$cat_n/2):
			$cat_left = $cat_left.'<li>'.$cats[$i].'</li>';
		elseif ($i>=$cat_n/2):
			$cat_right = $cat_right.'<li>'.$cats[$i].'</li>';
		endif;
	endfor;
	?>

 Next is the display page for each category:

Remember to change the divs and styling to match your page layout.

 $meta['grille_menu_price2'][0];
			$label2= $meta['grille_menu_pricelabel2'][0];
			$sig=$meta['grille_menu_signature'][0];
			?>
			<div class="menuitem<?php if($count %2 == 0) echo ' odd' ?>">
				<div class="itemname">
					<h3><?php if ($sig) { ?><img src="<?php bloginfo('template_directory') ?>/images/sig-icon.png" style="display:inline-block"> <?php } the_title(); ?></h3>
					<div class="itemdesc"><?php the_content(); ?></div>
				</div>
				<div class="prices">
				<div class="price1">
					<?php if (!empty($label1)) {
						echo  '<p>'.$label1. '</p>';
						}
						echo '<strong>'.$price1.'</strong>'; ?>
				</div>
				<div class="price2">
					<?php if( !empty($price2)) {
						if (!empty($label2)) {
							echo '<p>'.$label2. '</p>';
						}
						echo '<strong>'.$price2.'</strong>';
					}?>
				</div>
				</div>
			</div>
		<?php endwhile; endif; 
		wp_reset_query() ?>
		</div>
		</div>

	</div>

 You can see this plugin in action at: http://paradisegrille.net.

 

 

Comments are closed.

Skip to toolbar