php – Grab data Metabox to display it into another Metabox

Question

In the admin dashboard, I have two Metaboxes with repeatable fields inside the same page.

The first Metabox is a list of names of activities for teachers. Each activities is a row with only one input field $field[tag_filter] that can be repeated. The id of the Metabox is filter_repeatable_fields.

The second Metabox is a list of teachers. Each row is a teacher with one input and one select field, that can also be repeated. The id of the Metabox is teachers_repeatable_fields.

What I’m trying to do is grabbing the data from the first Metabox to display it into the second Metabox, with the values inside a select dropdown.

I have 10 activities listed in the first Metabox. The issue is, the options/values inside the select tag are numbers (0 to 9) in state of the names of activities.

The code of the second Metabox:

add_action('add_meta_boxes', 'teachers_add_meta_boxes', 1);
function teachers_add_meta_boxes() {
  global $post;
  if(!empty($post)) {
    $pageTemplate = get_post_meta($post->ID, '_wp_page_template', true);
    if($pageTemplate == 'page-teachers.php') {
      add_meta_box( 'teachers-repeatable-fields', 'Teachers', 'teachers_repeatable_meta_box_display', 'page', 'normal', 'low');
    }
  }
}

function teachers_repeatable_meta_box_display() {
 global $post;

 $teachers_repeatable_fields = get_post_meta($post->ID, 'teachers_repeatable_fields', true);

 // Grab the the data from the first metabox
 $options = get_post_meta( $post->ID, 'filter_repeatable_fields', true );

 wp_nonce_field( 'teachers_repeatable_meta_box_nonce', 'teachers_repeatable_meta_box_nonce' );
?>
 <script type="text/javascript">
jQuery(document).ready(function($) {
 $('#add-row').on('click', function() {
   var row = $('.empty-row.screen-reader-text').clone(true);
   row.removeClass('empty-row screen-reader-text');
   row.insertBefore('#teachers-repeatable-fieldset-one tbody>tr:last');
   return false;
 });
 $('.remove-row').on('click', function() {
   $(this).parents('tr').remove();
   return false;
 });

 $('#teachers-repeatable-fieldset-one tbody').sortable({
   opacity: 0.6,
   revert: true,
   cursor: 'move',
   handle: '.sort'
 });
});
 </script>

 <table id="teachers-repeatable-fieldset-one" width="100%">
 <thead>
   <tr>
     <th width="2%"></th>
     <th width="33%">Name</th>
     <th width="33%">Select</th>
     <th width="2%"></th>
   </tr>
 </thead>
 <tbody>
 <?php

 if ( $teachers_repeatable_fields ) :
   foreach ( $teachers_repeatable_fields as $field ) {
 ?>
 <tr>
   <td><a class="button remove-row" href="#">-</a></td>
   <td><input type="text" class="widefat" name="name[]" value="<?php if($field['name'] != '') echo esc_attr( $field['name'] ); ?>" /></td>
   <td>

     <select name="select[]">
     <?php foreach ( $options as $label => $value ) : ?>
     <option value="<?php echo $value; ?>"<?php selected( $field['select'], $value ); ?>><?php echo $label; ?></option>
     <?php endforeach; ?>
     </select>
   </td>
   
   <td><a class="sort">|||</a></td>
 </tr>
 <?php
   }
 else :
   // show a blank one
 ?>
 <tr>
   <td><a class="button remove-row" href="#">-</a></td>
   <td><input type="text" class="widefat" name="name[]" /></td>
   <td>
   
     <select name="select[]">
     <?php foreach ( $options as $label => $value ) : ?>
     <option value="<?php echo $value; ?>"><?php echo $label; ?></option>
     <?php endforeach; ?>
     </select>
     
   </td>
   <td><a class="sort">|||</a></td>
 </tr>
 <?php endif; ?>

 <!-- empty hidden one for jQuery -->
 <tr class="empty-row screen-reader-text">
   <td><a class="button remove-row" href="#">-</a></td>
   <td><input type="text" class="widefat" name="name[]" /></td>
   <td>
   
     <select name="select[]">
     <?php foreach ( $options as $label => $value ) : ?>
     <option value="<?php echo $value; ?>"><?php echo $label; ?></option>
     <?php endforeach; ?>
     </select>
     
   </td>
   <td><a class="sort">|||</a></td>
 </tr>
 </tbody>
 </table>

 <p><a id="add-row" class="button" href="#">Afegeix nou</a></p>

 <?php
}

add_action('save_post', 'teachers_repeatable_meta_box_save');
function teachers_repeatable_meta_box_save($post_id) {
 if ( ! isset( $_POST['teachers_repeatable_meta_box_nonce'] ) ||
   ! wp_verify_nonce( $_POST['teachers_repeatable_meta_box_nonce'], 'teachers_repeatable_meta_box_nonce' ) )
   return;

 if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
   return;

 if (!current_user_can('edit_post', $post_id))
   return;

 $old = get_post_meta($post_id, 'teachers_repeatable_fields', true);
 $new = array();

 $names = $_POST['name'];
 $selects = $_POST['select'];

 $count = count( $names );

 for ( $i = 0; $i < $count; $i++ ) {
   if ( $names[$i] != '' ) :
     $new[$i]['name'] = stripslashes( strip_tags( $names[$i] ) );
   endif;

   if ( in_array( $selects[$i], $options ) ) :
     $new[$i]['select'] = $selects[$i];
   else :
     $new[$i]['select'] = '';
   endif;
 }

 if ( !empty( $new ) && $new != $old )
   update_post_meta( $post_id, 'teachers_repeatable_fields', $new );
 elseif ( empty($new) && $old )
   delete_post_meta( $post_id, 'teachers_repeatable_fields', $old );

}

I’m struggling a lot to make it work. Any help would be much appreciated, thanks!

0
Mathieu Preaud 5 months 2021-07-10T10:34:18-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse