Advertisement
VOST

Repeatable fields with drop-down lists of taxonomies' terms

Apr 2nd, 2014
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.57 KB | None | 0 0
  1. //////////////////////////////////
  2. //  SETUP THE REPEATABLE FIELDS //
  3. // TO BE PUT IN A FUNCTION FILE //
  4. //////////////////////////////////
  5.  
  6. <?php
  7. //DROP DOWN LISTS USING TERMS FROM CUSTOM TAXONOMIES
  8. function print_screening_field($cnt, $p = null) {
  9. if ($p === null) {
  10.     $a = $b = $c = '';
  11. } else {
  12.     $a = $p['screeningName']; // Change variable name
  13.     $b = $p['screeningMonth']; // Change variable name
  14.     $c = $p['screeningYear']; // Change variable name  
  15. }
  16.     $festivalTerms = get_terms('festival', 'hide_empty=0'); // Change variable name and taxonomy name
  17.     $festivalNames = wp_get_object_terms($post->ID, 'festival'); // Change variable name and taxonomy name
  18.     $monthTerms = get_terms('month', 'hide_empty=0'); // Change variable name and taxonomy name
  19.     $monthNames = wp_get_object_terms($post->ID, 'month'); // Change variable name and taxonomy name
  20.     $yearTerms = get_terms('year', 'hide_empty=0'); // Change variable name and taxonomy name
  21.     $yearNames = wp_get_object_terms($post->ID, 'year'); // Change variable name and taxonomy name
  22.  
  23.     $html .= '<tr id="'. $cnt .'" class="repeatableField form-field">'; // The class name here is important regarding the jquery's part of the code
  24.         $html .= '<td><label>#'.$cnt.'</label></td>'; // Here the $cnt variable is used just to visually see how many fields we have created
  25.            
  26.         $html .= '<td>';
  27.             $html .= '<select name="screeningDetails[' . $cnt . '][screeningName]" id="' . $cnt . '">'; // to name the select makes it work, adding also the $cnt variable to it
  28.                 $html .= '<option value="';
  29.                     if (!count( $festivalNames )) {
  30.                         $html .= 'selected">';
  31.                     }
  32.                 $html .= 'Select a festival</option>';
  33.                 foreach ($festivalTerms as $festivalTerm) {
  34.                 $html .= '<option value="' . $festivalTerm->name . '" '.($a==$festivalTerm->name?'selected':'').'>' . $festivalTerm->name . '</option>';   
  35.                 }
  36.             $html .= '</select>';
  37.         $html .= '</td>';
  38.            
  39.         $html .= '<td>';
  40.             $html .= '<select name="screeningDetails[' . $cnt . '][screeningMonth]" id="' . $cnt . '">'; // to name the select makes it work, adding also the $cnt variable to it
  41.                 $html .= '<option value="';
  42.                     if (!count( $monthNames )) {
  43.                         $html .= 'selected">';
  44.                     }
  45.                 $html .= 'Select a month</option>';
  46.                 foreach ($monthTerms as $monthTerm) {
  47.                 $html .= '<option value="' . $monthTerm->name . '" '.($b==$monthTerm->name?'selected':'').' id="' . $cnt . '">' . $monthTerm->name . '</option>';  
  48.                 }
  49.             $html .= '</select>';
  50.         $html .= '</td>';
  51.            
  52.         $html .= '<td>';
  53.             $html .= '<select name="screeningDetails[' . $cnt . '][screeningYear]" id="' . $cnt . '">'; // to name the select makes it work, adding also the $cnt variable to it
  54.                 $html .= '<option value="';
  55.                     if (!count( $yearNames )) {
  56.                         $html .= 'selected">';
  57.                     }
  58.                 $html .= 'Select a year</option>';
  59.                 foreach ($yearTerms as $yearTerm) {
  60.                     $html .= '<option value="' . $yearTerm->name . '" '.($c==$yearTerm->name?'selected':'').'>' . $yearTerm->name . '</option>';   
  61.                 }
  62.             $html .= '</select>';
  63.         $html .= '</td>';  
  64.            
  65.         $html .= '<td>';
  66.             $html .= '<button class="remove">Remove #' . $cnt . '</button>';   
  67.         $html .= '</td>';
  68.            
  69.     $html .= '</tr>';
  70.    
  71. return $html;  
  72. }
  73.  
  74. //HOW TO DISPLAY THE DROP DOWN LISTS ON THE ADMIN SIDE
  75. function screening_details_meta_box(){
  76.     global $post;
  77.     $custom = get_post_custom($post->ID);
  78.     $screeningDetailsData = get_post_meta($post->ID,"screeningDetails",true);
  79. ?>
  80.     <h4 style=" text-transform: uppercase;">Other screenings</h4>
  81.     <table id="data_items" class="form-table" border-collapse="collapse">
  82.         <caption style="display: none;">List of screenings other than the premiere screenings</caption>
  83.    
  84.         <thead>
  85.             <tr class="form-field">
  86.                 <th>Number</th>
  87.                 <th>Name</th>
  88.                 <th>Month</th>
  89.                 <th>Year</th>
  90.                 <th></th>
  91.             </tr>
  92.         </thead>
  93.    
  94.         <tbody>
  95.         <?php
  96.         $c = 0;
  97.             if (count($screeningDetailsData) > 0){
  98.                 foreach((array)$screeningDetailsData as $p ){
  99.                     if ( isset($p['screeningName']) || isset($p['screeningMonth']) || isset($p['screeningYear']) ){
  100.                         echo print_screening_field($c,$p); // uses the fonction name
  101.                         $c = $c +1;
  102.                     }
  103.                 }
  104.             }
  105.         ?>
  106.         </tbody>
  107.     </table>
  108.     <span id="here"></span>
  109.     <button class="add"><?php echo __('Add a screening'); ?></button>
  110.     <script>
  111.         var $ =jQuery.noConflict();
  112.         $(document).ready(function() {
  113.         var count = <?php echo $c; ?>;
  114.         $(".add").click(function() {
  115.             count = count + 1;
  116.                 $('#data_items').append('<? echo implode('',explode("\n",print_screening_field('count'))); ?>'.replace(/count/g, count));
  117.                     return false;
  118.                 });
  119.                 $(".remove").live('click', function() {
  120.                     //$(this).parent().remove(); // When the button is outside the repeatable element
  121.                     $(this).parents('[class*="repeatableField"]').remove(); // When the button is inside the repeatable element
  122.                 });
  123.             });
  124.     </script>
  125.     <style>
  126.         table#data_items { margin-bottom: 20px; }
  127.         tr[class*="repeatableField"]:nth-child(odd) { background-color: #EEEEEE; }
  128.     </style>
  129.     <?php
  130. }
  131.  
  132. //SAVE THE DATA FROM THE REPEATABLE FIELDS
  133. function save_meta_box($post_id){
  134.     global $post;
  135.  
  136.     if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
  137.     return $post_id;
  138.     if (isset($_POST['screeningDetails'])){ $screeningDetailsData = $_POST['screeningDetails']; update_post_meta($post_id,'screeningDetails',$screeningDetailsData); } else { delete_post_meta($post_id,'screeningDetails'); }
  139. }
  140. add_action('save_post', 'save_meta_box');
  141. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement