Array to string conversion warning in revisions.php when using Enhanced Repeater
-
Hi,
I encountered a warning when viewing revision history for posts that contain the Enhanced Repeater field:
Warning: Array to string conversion in …/advanced-custom-fields/includes/revisions.php on line 323
This happens because ACF’s revision display tries to implode() the repeater’s nested array, which fails for multidimensional arrays.
I fixed this by hooking into _wp_post_revision_fields (priority 20) to detect enhanced_repeater fields, then adding a _wp_post_revision_field_{$name} filter (priority 9, before ACF’s 10) that converts the nested array into a readable string before ACF processes it.
Here is the fix to add in class-agrf-field-enhanced-repeater.php:
In initialize():
add_filter(‘_wp_post_revision_fields’, array($this, ‘register_revision_field_filters’), 20, 2);
Add these two methods to the class:public function register_revision_field_filters($fields, $post) { $post_id = is_object($post) ? $post->ID : (is_array($post) ? $post['ID'] : 0); if (!$post_id) return $fields; $meta = get_post_meta($post_id); foreach ($meta as $name => $value) { if (strpos($name, '_') === 0) continue; $key = isset($meta['_' . $name]) ? $meta['_' . $name][0] : null; if (!$key) continue; $field = acf_get_field($key); if (!$field || $field['type'] !== 'enhanced_repeater') continue; add_filter("_wp_post_revision_field_{$name}", array($this, 'format_repeater_revision_value'), 9, 4); } return $fields; } public function format_repeater_revision_value($value, $field_name, $post, $direction) { $raw = maybe_unserialize($value); if (!is_array($raw)) return $value; $label_map = array(); $meta_key = get_post_meta($post->ID, '_' . $field_name, true); if ($meta_key) { $field = acf_get_field($meta_key); if ($field && !empty($field['sub_fields'])) { foreach ($field['sub_fields'] as $sub) { $label_map[$sub['name']] = $sub['label']; $label_map[$sub['key']] = $sub['label']; } } } $lines = array(); foreach ($raw as $i => $row) { if (!is_array($row)) continue; $cells = array(); foreach ($row as $key => $val) { if (is_array($val)) { $val = implode(', ', array_map('strval', $val)); } $label = isset($label_map[$key]) ? $label_map[$key] : $key; $cells[] = $label . ': ' . $val; } $lines[] = 'Row ' . ($i + 1) . ' [' . implode(' | ', $cells) . ']'; } return implode("\n", $lines); }Hope this helps. Thanks for the great plugin!
You must be logged in to reply to this topic.