Advertisement
Guest User

functions.php

a guest
Aug 31st, 2012
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.11 KB | None | 0 0
  1. <?php //
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8. //Price list meta boxes
  9. add_action( 'add_meta_boxes', 'cd_meta_box_add' );
  10. function cd_meta_box_add() {add_meta_box( 'price-list-id', 'Price List', 'cd_meta_box_cb', 'post', 'normal', 'high' );}
  11.  
  12. function cd_meta_box_cb()
  13. {
  14.     // $post is already set, and contains an object: the WordPress post
  15.     global $post;
  16.     $values = get_post_custom( $post->ID );
  17.    
  18.         //
  19.         $plheading = isset( $values['price_list_heading'] ) ? esc_attr( $values['price_list_heading'][0] ) : '';
  20.        
  21.         $plcategory1 = isset( $values['price_list_category1'] ) ? esc_attr( $values['price_list_category1'][0] ) : '';
  22.         $plcategoryitems1 = isset( $values['price_list_items_category1'] ) ? esc_attr( $values['price_list_items_category1'][0] ) : '';
  23.        
  24.         $plcategory2 = isset( $values['price_list_category2'] ) ? esc_attr( $values['price_list_category2'][0] ) : '';
  25.         $plcategoryitems2 = isset( $values['price_list_items_category2'] ) ? esc_attr( $values['price_list_items_category2'][0] ) : '';
  26.        
  27.         $plcategory3 = isset( $values['price_list_category3'] ) ? esc_attr( $values['price_list_category3'][0] ) : '';
  28.         $plcategoryitems3 = isset( $values['price_list_items_category3'] ) ? esc_attr( $values['price_list_items_category3'][0] ) : '';
  29.    
  30.  
  31.     // We'll use this nonce field later on when saving.
  32.     wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
  33.     ?>
  34.  
  35.         <p>
  36.             <label for="price_list_heading">Price list heading:</label>
  37.             <input type="text" name="price_list_heading" id="price_list_heading" value="<?php echo $plheading; ?>" />
  38.             *Deleting this value disables price list in sidebar front-end
  39.         </p>
  40.        
  41.         <p>
  42.             <label for="price_list_category1">Category1 Title:</label>
  43.             <input type="text" name="price_list_category1" id="price_list_category1" value="<?php echo $plcategory1; ?>" />
  44.             <textarea style="width: 100%;" rows="3" name="price_list_items_category1" id="price_list_items_category1"><?php echo $plcategoryitems1; ?></textarea>
  45.             <span style="display: block; font-weight: bold; text-align: right;">Example: Rhine Riesling1|0,75 l|9,50 &euro;</span>
  46.         </p>
  47.        
  48.         <p>
  49.             <label for="price_list_category2">Category2 Title:</label>
  50.             <input type="text" name="price_list_category2" id="price_list_category2" value="<?php echo $plcategory2; ?>" />
  51.             <textarea style="width: 100%;" rows="3" name="price_list_items_category2" id="price_list_items_category2"><?php echo $plcategoryitems2; ?></textarea>
  52.             <span style="display: block; font-weight: bold; text-align: right;">Example: Rhine Riesling1|0,75 l|9,50 &euro;</span>
  53.         </p>
  54.        
  55.         <p>
  56.             <label for="price_list_category3">Category3 Title:</label>
  57.             <input type="text" name="price_list_category3" id="price_list_category3" value="<?php echo $plcategory3; ?>" />
  58.             <textarea style="width: 100%;" rows="3" name="price_list_items_category3" id="price_list_items_category3"><?php echo $plcategoryitems3; ?></textarea>
  59.             <span style="display: block; font-weight: bold; text-align: right;">Example: Rhine Riesling1|0,75 l|9,50 &euro;</span>
  60.         </p>
  61.  
  62.     <?php
  63. }
  64.  
  65. //Saving price list
  66. add_action( 'save_post', 'cd_meta_box_save' );  
  67. function cd_meta_box_save( $post_id )  
  68. {  
  69.         // Bail if we're doing an auto save
  70.         if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
  71.      
  72.         // if our nonce isn't there, or we can't verify it, bail
  73.         if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
  74.      
  75.         // if our current user can't edit this post, bail
  76.         if( !current_user_can( 'edit_post' ) ) return;
  77.      
  78.        
  79.         // Make sure your data is set before trying to save it
  80.         if( isset( $_POST['price_list_heading'] ) )
  81.             update_post_meta( $post_id, 'price_list_heading', esc_attr( $_POST['price_list_heading'] ) );
  82.        
  83.        
  84.         //
  85.         if( isset( $_POST['price_list_category1'] ) )
  86.             update_post_meta( $post_id, 'price_list_category1', esc_attr( $_POST['price_list_category1'] ) );
  87.            
  88.         if( isset( $_POST['price_list_items_category1'] ) )
  89.             update_post_meta( $post_id, 'price_list_items_category1', esc_attr( $_POST['price_list_items_category1'] ) );
  90.        
  91.        
  92.         //
  93.         if( isset( $_POST['price_list_category2'] ) )
  94.             update_post_meta( $post_id, 'price_list_category2', esc_attr( $_POST['price_list_category2'] ) );
  95.            
  96.         if( isset( $_POST['price_list_items_category2'] ) )
  97.             update_post_meta( $post_id, 'price_list_items_category2', esc_attr( $_POST['price_list_items_category2'] ) );
  98.        
  99.        
  100.         //
  101.         if( isset( $_POST['price_list_category3'] ) )
  102.             update_post_meta( $post_id, 'price_list_category3', esc_attr( $_POST['price_list_category3'] ) );
  103.            
  104.         if( isset( $_POST['price_list_items_category3'] ) )
  105.             update_post_meta( $post_id, 'price_list_items_category3', esc_attr( $_POST['price_list_items_category3'] ) );
  106. }
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119. //Listing details features and services meta checkboxes
  120. add_action( 'add_meta_boxes', 'cd_meta_checklist_add' );
  121. function cd_meta_checklist_add() {add_meta_box( 'checklist-id', 'Listing Icons', 'cd_meta_checklist_cb', 'post', 'normal', 'high' );}
  122.  
  123. function cd_meta_checklist_cb()
  124. {
  125.  
  126.         global $post;
  127.         $values = get_post_custom( $post->ID );
  128.        
  129.         //check of checkbox should be active or not
  130.         $pricelistline = explode(",", get_post_meta($post->ID, 'meta_features_checklist', true));
  131.        
  132.         // We'll use this nonce field later on when saving.
  133.         wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
  134.    
  135. ?>
  136.  
  137.         <p>
  138.             <label for="meta_box_check_bar">bar</label>
  139.             <input type="checkbox" id="meta_box_check_bar" name="meta_box_check_bar" value="bar" <?php if ($pricelistline[0]) {echo "checked";} ?> />
  140.         </p>
  141.         <p>
  142.             <label for="meta_box_check_parking">parking</label>
  143.             <input type="checkbox" id="meta_box_check_parking" name="meta_box_check_parking" value="parking" <?php if ($pricelistline[1]) {echo "checked";} ?> />
  144.         </p>
  145.         <p>
  146.             <label for="meta_box_check_accessible-for-disabled">accessible-for-disabled</label>
  147.             <input type="checkbox" id="meta_box_check_accessible-for-disabled" name="meta_box_check_accessible-for-disabled" value="accessible-for-disabled" <?php if ($pricelistline[2]) {echo "checked";} ?> />
  148.         </p>
  149.         <p>
  150.             <label for="meta_box_check_air-conditioning">air-conditioning</label>
  151.             <input type="checkbox" id="meta_box_check_air-conditioning" name="meta_box_check_air-conditioning" value="air-conditioning" <?php if ($pricelistline[3]) {echo "checked";} ?> />
  152.         </p>
  153.         <p>
  154.             <label for="meta_box_check_frigo-bar">frigo-bar </label>
  155.             <input type="checkbox" id="meta_box_check_frigo-bar" name="meta_box_check_frigo-bar" value="frigo-bar" <?php if ($pricelistline[4]) {echo "checked";} ?> />
  156.         </p>
  157.         <p>
  158.             <label for="meta_box_check_pets">pets</label>
  159.             <input type="checkbox" id="meta_box_check_pets" name="meta_box_check_pets" value="pets" <?php if ($pricelistline[5]) {echo "checked";} ?> />
  160.         </p>
  161.         <p>
  162.             <label for="meta_box_check_phone">phone</label>
  163.             <input type="checkbox" id="meta_box_check_phone" name="meta_box_check_phone" value="phone" <?php if ($pricelistline[6]) {echo "checked";} ?> />
  164.         </p>
  165.         <p>
  166.             <label for="meta_box_check_tv">tv</label>
  167.             <input type="checkbox" id="meta_box_check_tv" name="meta_box_check_tv" value="tv" <?php if ($pricelistline[7]) {echo "checked";} ?> />
  168.         </p>
  169.         <p>
  170.             <label for="meta_box_check_local-dishes">local-dishes</label>
  171.             <input type="checkbox" id="meta_box_check_local-dishes" name="meta_box_check_local-dishes" value="local-dishes" <?php if ($pricelistline[8]) {echo "checked";} ?> />
  172.         </p>
  173. <?php }
  174.  
  175. //Saving checkbox states
  176. add_action( 'save_post', 'cd_meta_checkbox_save' );  
  177. function cd_meta_checkbox_save( $post_id )  
  178. {  
  179.     // Bail if we're doing an auto save  
  180.     if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
  181.  
  182.     // if our nonce isn't there, or we can't verify it, bail
  183.     if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
  184.  
  185.     // if our current user can't edit this post, bail  
  186.     if( !current_user_can( 'edit_post' ) ) return;
  187.    
  188.    
  189.     //grabing all checkbox values and combining them
  190.    
  191.                                 /* make an array for all used checkbox */
  192.                                 $used_checkboxes = array();
  193.                                
  194.                                 /* make an array whit all options */
  195.                                 $avaible_checkboxes = explode(',', "bar,parking,accessible-for-disabled,air-conditioning,frigo-bar,pets,phone,tv,local-dishes");
  196.                                
  197.                                
  198.                                 /* loop troguht all avaible checkboxes */
  199.                                 foreach($avaible_checkboxes as $current_key)
  200.                                 {
  201.                                    /* check if the checkbox was sent */
  202.                                    if(isset($_POST["meta_box_check_{$current_key}"]))
  203.                                    {
  204.                                       /* if sent, add key to list */
  205.                                       $used_checkboxes[$current_key] = $current_key;
  206.                                    }
  207.                                    else
  208.                                    {
  209.                                       /* if not sent, add empty value to list */
  210.                                       $used_checkboxes[$current_key] = '';
  211.                                    }
  212.                                 }
  213.                                
  214.                                 /* convert list to csv */
  215.                                 $used_checkboxes_csv = implode(',', $used_checkboxes);
  216.  
  217.  
  218.     //saving to DB
  219.     if( isset( $used_checkboxes_csv ) )
  220.     update_post_meta( $post_id, 'meta_features_checklist', esc_attr( $used_checkboxes_csv ) );
  221.    
  222.    
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement