custom post types – Update metapost dynamically in multidimensional array with grouped values
Question
Access multidimensional array
$gallery = get_post_meta( $post->ID, 'ds_product_gallery', true );
Here is the markup
<div class="field_left">
<div class="form_field">
<label>Image URL</label>
<input type="text"
name="meta[ds_product_gallery][picture][<?php echo $i; ?>]"
value="<?php esc_html_e( $gallery['picture'][$i] ); ?>"
type="url" pattern="https://.*"
size="30"
minlength="8"
placeholder="No picture chosen yet!" readonly
/>
</div>
<div class="form_field">
<label>Description</label>
<input type="text"
name="meta[ds_product_gallery][figcaption][<?php echo $i; ?>]"
value="<?php esc_html_e( $gallery['figcaption'][$i] ); ?>"
placeholder="Enter the caption here"
/>
</div>
to add fields dynamically, i use this
<div class="field_left">
<div class="form_field">
<label>Image URL</label>
<input class="meta_image_url"
name="meta[ds_product_gallery][picture][]"
value=""
type="url"
size="30"
minlength="8"
placeholder="No picture chosen yet!" readonly />
</div>
<div class="form_field">
<label>Description</label>
<input class="meta_image_desc"
value=""
type="text"
name="meta[ds_product_gallery][figcaption][]"
placeholder="Enter the caption here"/>
</div>
</div>
to send data to the database i use this
if ( isset( $_POST['meta'] ) ) {
if ( $_POST['meta']['ds_product_gallery'] )
{
$gallery_data = array();
for ($i = 0; $i < count( $_POST['meta']['ds_product_gallery']['picture'] ); $i++ )
{
if ( '' != $_POST['meta']['ds_product_gallery']['picture'][ $i ] )
{
$gallery_data['picture'][] = $_POST['meta']['ds_product_gallery']['picture'][ $i ];
$gallery_data['figcaption'][] = $_POST['meta']['ds_product_gallery']['figcaption'][ $i ];
}
}
if ( $gallery_data ) {
$_POST['meta']['ds_product_gallery'] = $gallery_data;
}else{
unset($_POST['meta']['ds_product_gallery']);
}
}else{
unset($_POST['meta']['ds_product_gallery']);
}
foreach( $_POST['meta'] as $key => $value ){
update_post_meta( $post_id, $key, $value );
}
}
everything works great except from when i delete an element that was saved in the DB using javascript.
i am trying to figure out where my mistake since the weekend.
0
2 years
2022-03-07T03:27:43-05:00
2022-03-07T03:27:43-05:00 0 Answers
0 views
0
Leave an answer