I have input fields with names like: child1, child[2], child[3] and so on. A user can add as many fields as he wants, its handled with jquery and dynamically ads an input field with [n] in the end of the name.
Now I need to store this data to database in WordPress plugin I work on. And later retrieve this data to display it on the website, and of cause add those field out fields in admin page, where user edits data.
I know how to store one field for example child to the database in WordPress, it would be something like that:
<?php $child = get_post_meta( $post->ID, 'child', true ); ?>
<?php
function save_child_data( $post_id ) {
// Check if exists
if(! isset($_POST['child'])){return;}
// Sanitize
$child_data = sanitize_text_field( $_POST['child'] );
// Store data
update_post_meta( $post_id, 'child', $child_data);
}
add_action( 'save_post', 'save_child_data' );
?>
<!-- Field in admin -->
<input type="text" name="child" id="child" value="<?php echo esc_attr($child); ?>">
<!-- Front office -->
<div>Child: <?php echo get_post_meta( $post->ID, 'child', true ); ?></div>
So my question is how do I handle this array like child[n]?
Or another question maybe it is better to just use child1, child2, child3 and handle it as regular input fields?
So you understand what I mean by dynamically added block here is a jsfiddle link: http://jsfiddle.net/alexchizhov/LC2K6/