Reusable-Custom-WordPress-Meta-Boxes
Reusable-Custom-WordPress-Meta-Boxes copied to clipboard
Repeatable/Sortable fields not saving sort order
I am having trouble with the Repeatable/Sortable field type. The information is saved correctly and I can create/delete repeatable fields, but after resorting them, the new position is not being saved. Is there something that I am missing?
You will find the field setup that I am using for this field below.
Thank you in advance for the help!
$location_offer_fields = array(
array(
'label' => '',
'desc' => 'Enter your location offers above',
'id' => $prefix.'location_offers',
'type' => 'repeatable',
'sanitizer' => array(
'title' => 'sanitize_text_field',
'desc' => 'wp_kses_data'
),
'sanitizer' => array(
'title' => 'sanitize_text_field',
'desc' => 'wp_kses_data'
),
'repeatable_fields' => array(
'title' => array(
'label' => 'Title',
'id' => 'title',
'type' => 'text'
),
'desc' => array(
'label' => 'Description',
'id' => 'desc',
'type' => 'textarea'
)
)
)
);
Bump - I'm having the same issues here. I looked at your code - perhaps it's the two sanitizer's? Any help on this would really be appreciated
I think I got it - or at least this worked for me. At the bottom of the meta_box.php you had to add one line of code.
Hope this helps!
if( in_array( $field['type'], array( 'tax_select', 'tax_checkboxes' ) ) ) {
// save taxonomies
if ( isset( $_POST[$field['id']] ) ) {
$term = $_POST[$field['id']];
wp_set_object_terms( $post_id, $term, $field['id'] );
}
}
else {
// save the rest
$new = false;
$old = get_post_meta( $post_id, $field['id'], true );
if ( isset( $_POST[$field['id']] ) )
$new = $_POST[$field['id']];
if($field['type'] == 'repeatable')
$new = array_values($new);
if ( isset( $new ) && '' == $new && $old ) {
delete_post_meta( $post_id, $field['id'], $old );
} elseif ( isset( $new ) && $new != $old ) {
$sanitizer = isset( $field['sanitizer'] ) ? $field['sanitizer'] : 'sanitize_text_field';
if ( is_array( $new ) )
$new = meta_box_array_map_r( 'meta_box_sanitize', $new, $sanitizer );
else
$new = meta_box_sanitize( $new, $sanitizer );
if( $field['type'] == 'date') {
$new = strtotime($new);
}
update_post_meta( $post_id, $field['id'], $new );
}
}