How save input fields value created dynamically in WordPress save_post hook action?

I’m developing a plugin that create a custom post type.
I’ve created a custom meta box with a custom meta input field.
With an AJAX function, I duplicate the input field. They are the same name and the same class.
It’s all ok, but how can I save these data with save_post hook action?

If I use the classic function, with update_post_meta, it’s save only last value.
I need to create an array and pass it to the save function.
How?

Read More

If I use admin-ajax.php how can I pass the post_id for update_post_meta in callback function?
I don’t wanna use wpalchemy.

Related posts

Leave a Reply

2 comments

  1. The input filed name has to be in array format: field_name[].

    In the <form>:

    <td><input type="text" class="widefat" name="name[]" /></td>
    <td><input type="text" class="widefat" name="url[]" value="http://" /></td>
    

    Then in save_post action:

    $names = $_POST['name'];
    $urls = $_POST['url'];
    $count = count( $names );
    
    for ( $i = 0; $i < $count; $i++ ) 
    {
        if ( $names[$i] != '' ) 
        {
            $new[$i]['name'] = stripslashes( strip_tags( $names[$i] ) ); // Sanitization
    
            if ( $urls[$i] == 'http://' )
                $new[$i]['url'] = '';
            else
                $new[$i]['url'] = stripslashes( $urls[$i] );
        }
    }
    
    if ( !empty( $new ) && $new != $old )
        update_post_meta( $post_id, 'repeatable_fields', $new );
    

    Example adapted from here.

  2. Thanks ! It’s perfect !
    I use

    $new[$i]['name'] = stripslashes( strip_tags( htmlentities($names[$i]) ) );
    

    for special chars like ‘à,è,ì,ò,ù’ and it works perectly !

    Thanks !