<?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');