- First, dd this code [] at the end of your form field name. So your field's name will be "test[]".
First, dd this code [] at the end of your form field name. So your field's name will be "test[]".
Example:
<input type="text" name="test[]" value="" size="60" />
Use this way to save the value:
$old = get_post_meta($post_id, 'your_meta_key', true); $newtest = array(); $test = $_POST['test']; $count = count( $test ); for ( $i = 0; $i < $count; $i++ ) { if ( $test[$i] != '' ) { $newtest[$i]['test'] = stripslashes( strip_tags( $test[$i] ) ); } } if ( !empty( $newtest ) && $newtest != $old ) { update_post_meta( $post_id, 'repeatable_fields', $newtest ); } elseif ( empty($newtest) && $old ) { delete_post_meta( $post_id, 'repeatable_fields', $old ); }
Showing your field with the saved values:
$show_the_value = get_post_meta($post->ID, 'your_meta_key', true); if ($show_the_value) { foreach ($show_the_value as $value) { ?> <input type="text" name="test[]" value="<?php if($value['test'] != '') echo $value['test']; ?>" size="60" /> <?php } }
Example:
<input type="text" name="test[]" value="" size="60" />
Use this way to save the value:
$old = get_post_meta($post_id, 'your_meta_key', true); $newtest = array(); $test = $_POST['test']; $count = count( $test ); for ( $i = 0; $i < $count; $i++ ) { if ( $test[$i] != '' ) { $newtest[$i]['test'] = stripslashes( strip_tags( $test[$i] ) ); } } if ( !empty( $newtest ) && $newtest != $old ) { update_post_meta( $post_id, 'repeatable_fields', $newtest ); } elseif ( empty($newtest) && $old ) { delete_post_meta( $post_id, 'repeatable_fields', $old ); }
Showing your field with the saved values:
$show_the_value = get_post_meta($post->ID, 'your_meta_key', true); if ($show_the_value) { foreach ($show_the_value as $value) { ?> <input type="text" name="test[]" value="<?php if($value['test'] != '') echo $value['test']; ?>" size="60" /> <?php } }