Advertisement
iamdangavin

Duplicate Items

Jul 11th, 2011
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.33 KB | None | 0 0
  1. <?php
  2.  
  3. function menu_item_save_details(){
  4. global $post;
  5.    
  6. $menu_items = array();
  7. for ( $i = 0, $j = min(
  8.     count( $_POST['menu_item_name']),
  9.     count( $_POST['menu_item_price']),
  10.     count( $_POST['menu_item_description']),
  11.     count( $_POST['menu_item_notes'])
  12.    
  13.     ); $i < $j; $i++ ) {
  14.    
  15.     if ( empty( $_POST['menu_item_name'] ) )
  16.         continue;
  17.     $menu_items[] = array(
  18.         $_POST['menu_item_name'][ $i ],
  19.         $_POST['menu_item_price'][ $i ],
  20.         $_POST['menu_item_description'][ $i ],
  21.         $_POST['menu_item_notes'][ $i ]
  22.     );
  23. }
  24. update_post_meta( $post->ID, '_menu_items', $menu_items );
  25.  
  26. }
  27.  
  28. function menu_items_init(){
  29.   add_meta_box("menu_items", "Menu Items", "menu_items_meta", "gavin_landd_menu", "normal", "high");
  30.   add_action( 'admin_footer-post.php', 'menu_items_scripts' );
  31.   add_action( 'admin_footer-post-new.php', 'menu_items_scripts' );
  32. }
  33.  
  34. function menu_items_meta() {
  35.     global $post;
  36.     $menu_items = get_post_meta( $post->ID, '_menu_items', true );
  37.     echo '<div id="menu_item">';
  38.     echo '<table class="form-table">';
  39.     foreach ( $menu_items as $menu_item ) {
  40.         echo '
  41.             <tr>
  42.                 <th style="width: 20%;">
  43.                     <label>Menu Item Name</label>
  44.                 </th>
  45.                 <td>
  46.                     <input name="menu_item_name[]" type="text" size="30" style="width:300px;" value="' . esc_attr( $menu_item[ 0 ] ) . '" />
  47.                 </td>
  48.             </tr>
  49.             <tr>
  50.                 <th style="width: 20%;">
  51.                     <label>Menu Item Price</label>
  52.                 </th>
  53.                 <td>
  54.                     <input name="menu_item_price[]" type="text"  class="code" size="6" style="width: 100px;" value="' . esc_attr( $menu_item[ 1 ] ) . '" /><br/>
  55.                     <span class="description">With or without the $</span>
  56.                 </td>
  57.             </tr>
  58.             <tr>
  59.                 <th style="width: 20%;">
  60.                     <label>Menu Item Description</label>
  61.                 </th>
  62.                 <td>
  63.                     <textarea name="menu_item_description[]" type="textarea" cols="80" rows="5">' . esc_attr( $menu_item[ 2 ] ). '</textarea>
  64.                 </td>
  65.             </tr>
  66.             <tr>
  67.                 <th style="width: 20%;">
  68.                     <label>Menu Item Notes</label>
  69.                 </th>
  70.                 <td>
  71.                     <input name="menu_item_notes[]" type="text" size="30" style="width:300px;" value="' . esc_attr( $menu_item[ 3 ] ) .'" /><br/>
  72.                     <span class="description">Do you reccommend anything for this item?</span>
  73.                 </td>
  74.             </tr>
  75.             <tr>
  76.                 <th style="width:20%;"><a href="#" class="remove button">Remove Menu Item</a></th>
  77.             </tr>
  78.             <tr>
  79.                 <td colspan="100%"><hr /></th>
  80.             </tr>';
  81.  
  82.     }
  83.     echo '</table>';
  84.     echo '<table class="form-table"><tbody><tr><th style="width: 100%;"><a href="#" class="button-primary" id="new_menu_item">New Menu Item</a></th></tr></tbody></table>';
  85.     echo '</div>';
  86. }
  87.  
  88. function menu_item_scripts() {
  89.     if ( $GLOBALS['post_type'] != 'gavin_landd_menu' )
  90.         return;
  91. $js = <<<JS
  92. <script type="text/javascript">
  93. jQuery(document).ready( function($) {
  94.     $('#new_menu_item').click( function() {
  95.    
  96.         $('<table class="form-table"><tr><th style="width: 20%;"><label>Menu Item Name</label></th><td><input name="menu_item_name[]" type="text"  style="width: 300px;"/></td></tr><tr><th><label>Menu Item Price</label></th><td><input name="menu_item_price[]" type="text"  class="code" length="12" style="width: 100px;"/><br/><span class="description">With or without the $</span></td></tr><tr><th style="width: 20%;"><label>Menu Item Description</label></th><td><textarea name="menu_item_description[]" cols="80" rows="5"></textarea></td></tr><tr><th><label style="width: 20%;">Menu Item Notes</label></th><td><input name="menu_item_notes[]" type="text"  style="width: 300px;"/><br/><span class="description">Do you reccommend anything for this item?</span></td></tr><tr><th style="width:20%;"><a href="#" class="remove button">Remove Menu Item</a></th></tr><tr><td colspan="100%"><hr/></td></tr></table>').hide().appendTo('#menu_item').fadeIn(600)
  97.         return false;
  98.     });
  99.     $('#menu_item .remove').live('click', function() {
  100.        
  101.         $(this).parent().find('#menu_item').fadeOut(600);
  102.         return false;
  103.     });
  104. });
  105. </script>
  106. JS;
  107. echo $js;
  108. }
  109.  
  110. add_action( 'admin_init', 'add_menu_items_meta_box' );
  111. function add_menu_items_meta_box() {
  112.     add_meta_box( 'menu_items', 'Menu Items', 'menu_items_meta', 'gavin_landd_menu', 'normal', 'high' );
  113.     add_action( 'admin_footer-post.php', 'menu_item_scripts' );
  114.     add_action( 'admin_footer-post-new.php', 'menu_item_scripts' );
  115. }
  116.  
  117. add_action('admin_init', 'menu_items_init');
  118. add_action('save_post', 'menu_item_save_details');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement