Skip to content

Instantly share code, notes, and snippets.

@pauloiankoski
Last active October 9, 2019 23:28
Show Gist options
  • Save pauloiankoski/ac86f6cd467cbb2351d3e61641a40c62 to your computer and use it in GitHub Desktop.
Save pauloiankoski/ac86f6cd467cbb2351d3e61641a40c62 to your computer and use it in GitHub Desktop.
jQuery(document).ready(function($)) {
var preventSubmit = true
$('.acf-field[data-name="repeater_value"]').hide()
$('.wp-admin.post-type-page form#post').on('submit', function(e) {
if ( ! preventSubmit ) {
return
}
e.preventDefault()
var table = []
$('.acf-field[data-name="repeater_name"] .acf-row:not(.acf-clone)').each(function(row){
table[row] = []
$(this).find('input').each(function(column){
table[row][column] = $(this).val()
$(this).attr('disabled', true)
})
})
$('.acf-field[data-name="repeater_value"] textarea').val(JSON.stringify(table))
preventSubmit = false
$(this).submit()
preventSubmit = true
$('.acf-field[data-name="repeater_name"] .acf-row input').removeAttr('disabled')
})
})
add_action( 'acf/load_value/name=repeater_name', function( $value, $post_id, $field ) {
$table = json_decode( get_field('repeater_value', $post_id) );
if ( ! $table ) {
return $value;
}
$keys = wp_list_pluck( $field['sub_fields'], 'key' );
$new_value = array();
foreach ( $table as $row => $columns ) {
foreach ( $columns as $column => $value ) {
$new_value[ $row ][ $keys[ $column ] ] = $value;
}
}
return $new_value;
}, 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment