I have written a code for repeating metabox for a custom type post.
But the data is not saved when i publish the post.
here is the full code for repeatable metabox
I also want to submit the metabox data from frontend please give me the idea, how can i do it from front end.

add_action('admin_init', 'hhs_add_meta_boxes', 1);
function hhs_add_meta_boxes() {
    add_meta_box( 'repeatable-fields', 'Repeatable Fields', 'hhs_repeatable_meta_box_display', 'initiative', 'normal', 'default');
}
function hhs_repeatable_meta_box_display() {
global $post;
$repeatable_fields = get_post_meta($post->ID, 'repeatable_fields', true);
/*$options = hhs_get_sample_options();*/
wp_nonce_field( 'hhs_repeatable_meta_box_nonce', 'hhs_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( '#repeatable-fieldset-one > tbody>tr:last' );
        return false;
    });

    $( '.remove-row' ).on('click', function() {
        $(this).parents('tr').remove();
        return false;
    });
});
</script>

<table id="repeatable-fieldset-one" width="100%">
<tbody>
<?php

if ( $repeatable_fields ) :

foreach ( $repeatable_fields as $field ) {
?>
<tr>
    <td>
        <table>
            <tr>
                <td>Meeting Date <br/><input type="text" class="widefat" name="meetingdate[]" value="<?php if($field['meetingdate'] != '') echo esc_attr( $field['meetingdate'] ); ?>" /></td>
                <td>Cordinator <br/><input type="text" class="widefat" name="cordinator[]" value="<?php if($field['cordinator'] != '') echo esc_attr( $field['cordinator'] ); ?>" /></td>
                <td>Soil <br/><input type="text" class="widefat" name="soil[]" value="<?php if($field['soil'] != '') echo esc_attr( $field['soil'] ); ?>" /></td>
                <td>Water<br/><input type="text" class="widefat" name="water[]" value="<?php if($field['water'] != '') echo esc_attr( $field['water'] ); ?>" /></td>
                <td>Sowing<br/><input type="text" class="widefat" name="sowing[]" value="<?php if($field['sowing'] != '') echo esc_attr( $field['sowing'] ); ?>" /></td>
            </tr>
            <tr>
                <td>Maintenance <br/><input type="text" class="widefat" name="maintenance[]" value="<?php if($field['maintenance'] != '') echo esc_attr( $field['maintenance'] ); ?>" /></td>
                <td>Pesticide<br/><input type="text" class="widefat" name="pesticide[]" value="<?php if($field['pesticide'] != '') echo esc_attr( $field['pesticide'] ); ?>" /></td>
                <td>Fertilizers<br/><input type="text" class="widefat" name="fertilizer[]" value="<?php if($field['fertilizer'] != '') echo esc_attr( $field['fertilizer'] ); ?>" /></td>
                <td>Others<br/><input type="text" class="widefat" name="others[]" value="<?php if($field['others'] != '') echo esc_attr( $field['others'] ); ?>" /></td>
                <td>Next Meeting<br/><input type="text" class="widefat" name="nextmeeting[]" value="<?php if($field['nextmeeting'] != '') echo esc_attr( $field['nextmeeting'] ); ?>" /></td>

            </tr>
            <tr>
                <td>Next Action<br/><input type="text" class="widefat" name="nextaction[]" value="<?php if($field['nextaction'] != '') echo esc_attr( $field['nextaction'] ); ?>" /></td>
                <td>&nbsp;<br/><a class="button remove-row" href="#">Remove</a></td>
            </tr>
        </table>

        </td>


</tr>
<?php
}
else :
// show a blank one
?>
<tr>
    <td>
        <table>
            <tr>
                <td>Meeting Date <br/><input type="text" class="widefat" name="meetingdate[]"  /></td>
                <td>Cordinator <br/><input type="text" class="widefat" name="cordinator[]"  /></td>
                <td>Soil <br/><input type="text" class="widefat" name="soil[]"  /></td>
                <td>Water<br/><input type="text" class="widefat" name="water[]"  /></td>
                <td>Sowing<br/><input type="text" class="widefat" name="sowing[]"  /></td>
            </tr>
            <tr>
                <td>Maintenance <br/><input type="text" class="widefat" name="maintenance[]"  /></td>
                <td>Pesticide<br/><input type="text" class="widefat" name="pesticide[]"  /></td>
                <td>Fertilizers<br/><input type="text" class="widefat" name="fertilizer[]"  /></td>
                <td>Others<br/><input type="text" class="widefat" name="others[]"  /></td>
                <td>Next Meeting<br/><input type="text" class="widefat" name="nextmeeting[]"  /></td>

            </tr>
            <tr>
                <td>Next Action<br/><input type="text" class="widefat" name="nextaction[]"  /></td>
                <td>&nbsp;<br/><a class="button remove-row" href="#">Remove</a></td>
            </tr>
        </table>

        </td>


</tr>
<?php endif; ?>

<!-- empty hidden one for jQuery -->
<tr class="empty-row screen-reader-text">
    <td>
        <table>
            <tr>
                <td>Meeting Date <br/><input type="text" class="widefat" name="meetingdate[]"  /></td>
                <td>Cordinator <br/><input type="text" class="widefat" name="cordinator[]"  /></td>
                <td>Soil <br/><input type="text" class="widefat" name="soil[]"  /></td>
                <td>Water<br/><input type="text" class="widefat" name="water[]"  /></td>
                <td>Sowing<br/><input type="text" class="widefat" name="sowing[]"  /></td>
            </tr>
            <tr>
                <td>Maintenance <br/><input type="text" class="widefat" name="maintenance[]"  /></td>
                <td>Pesticide<br/><input type="text" class="widefat" name="pesticide[]"  /></td>
                <td>Fertilizers<br/><input type="text" class="widefat" name="fertilizer[]"  /></td>
                <td>Others<br/><input type="text" class="widefat" name="others[]"  /></td>
                <td>Next Meeting<br/><input type="text" class="widefat" name="nextmeeting[]"  /></td>

            </tr>
            <tr>
                <td>Next Action<br/><input type="text" class="widefat" name="nextaction[]"  /></td>
                <td>&nbsp;<br/><a class="button remove-row" href="#">Remove</a></td>
            </tr>
        </table>

        </td>


    </tr>
    </tbody>
</table>

<p><a id="add-row" class="button" href="#">Add another</a></p>
    <?php
}
add_action('save_post', 'hhs_repeatable_meta_box_save');
function hhs_repeatable_meta_box_save($post_id) {
if ( ! isset( $_POST['hhs_repeatable_meta_box_nonce'] ) ||
! wp_verify_nonce( $_POST['hhs_repeatable_meta_box_nonce'], 'hhs_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, 'repeatable_fields', true);
$new = array();
/*$options = hhs_get_sample_options();*/

/*$names = $_POST['name'];
$selects = $_POST['select'];
$urls = $_POST['url'];*/
$meetingdates = $_POST['meetingdate'];
$cordinators = $_POST['cordinator'];
$soils = $_POST['soil'];
$waters = $_POST['water'];
$sowings = $_POST['sowing'];
$maintenances = $_POST['maintenance'];
$pesticides = $_POST['pesticide'];
$fertilizers = $_POST['fertilizer'];
$other = $_POST['others'];
$nextmeetings = $_POST['nextmeeting'];
$nextactions = $_POST['nextaction'];


$count = count( $meetingdates);

for ( $i = 0; $i < $count; $i++ ) {
    if ( $meetingdates[$i] != '' ) :
        $new[$i]['meetingdate'] = stripslashes( strip_tags( $meetingdates[$i] ) );
        $new[$i]['cordinator'] = stripslashes( strip_tags( $cordinators[$i] ) );
        $new[$i]['soil'] = stripslashes( strip_tags( $soils[$i] ) );
        $new[$i]['water'] = stripslashes( strip_tags( $waters[$i] ) );
        $new[$i]['sowing'] = stripslashes( strip_tags( $sowings[$i] ) );
        $new[$i]['pesticide'] = stripslashes( strip_tags( $pesticides[$i] ) );
        $new[$i]['maintenance'] = stripslashes( strip_tags( $maintenances[$i] ) );
        $new[$i]['fertilizer'] = stripslashes( strip_tags( $fertilizers[$i] ) );
        $new[$i]['others'] = stripslashes( strip_tags( $other[$i] ) );
        $new[$i]['nextmeeting'] = stripslashes( strip_tags( $nextmeetings[$i] ) );
        $new[$i]['nextaction'] = stripslashes( strip_tags( $nextactions[$i] ) );

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

Read more here: Data not saved when the post is published


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: