Create Repeater Field From Front End and Save Data

Question

I’m trying to create a form that users can post from the front end.

I’m having trouble saving data of new repeater fields though.

<?php
get_header();
if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == 'post' ) {

    $post_type      = 'liste';
    $user_id        = $current_user->ID;
    $post_title     = $_POST['post_title'];

    $post_id = wp_insert_post( array(
        'post_author'   => $user_id,
        'post_title'    => $post_title,
        'post_type'     => $post_type,
        'post_status'   => 'pending'
    ));

    $old = get_post_meta($post_id, 'data_rows', true);
    $new = array();
    $ah_upl = $_POST['al_upl'];
    $ah_mbd = $_POST['al_mbd'];
    $ah_ttl = $_POST['al_ttl'];
    $count = count( $ah_upl );
    for ( $i = 0; $i < $count; $i++ ) {
        if ( $ah_upl[$i] != '' ) :
            $new[$i]['al_upl'] = stripslashes( strip_tags( $ah_upl[$i] ) );
            $new[$i]['al_mbd'] = stripslashes( $ah_mbd[$i] );
            $new[$i]['al_ttl'] = stripslashes( $ah_ttl[$i] );
        endif;
    }

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

}
?>

jQuery

<script type="text/javascript">
jQuery(document).ready(function($){
    $('#add-image').on('click', function() {
        var row = $('.empty-img').clone(true);
        row.removeClass('empty-img').addClass('clone');
        row.insertBefore('#repeater>.entries:last');
        return false;
    });

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

HTML

I can’t save data of the new repeater added

<form name="new_post" method="post" action="<?php the_permalink(); ?>" enctype="multipart/form-data">
    <div id="repeater">
        //first repeater to show
        <div class="entries first">
            <input type="text" name="al_ttl[]"/>
            <input type="text" name="al_mbd[]"/>
            <input type="text" name="al_upl[]"/>
        </div>
        //adding new entries with jQuery
        <div class="entries empty-img">
            <input type="text" name="al_ttl[]"/>
            <input type="text" name="al_mbd[]"/>
            <input type="text" name="al_upl[]"/>
        </div>

    </div>

    <div id="add-image" class="url-img-eko"><a class="up-img-eko">images</a></div>

    <input type="hidden" name="action" value="post" />
    <?php wp_nonce_field( 'new-post' ) ?>
</form>
0
, , , , mems 2 years 2020-01-02T08:38:35-05:00 0 Answers 91 views 0

Leave an answer

Browse
Browse