I’ve got a Flexible Content Field containing multiple layouts, and each layout has a number of fields added via wp-admin. When I use acf_add_local_field()
to programmatically add a field to one of these layouts, the new field is added but all other fields in the Flexible Content Field (not just that one layout, but all layouts) disappear.
I can’t find anything on this in the docs, and perhaps that’s my search skills being sub-par, in which case feel free to point me in the right direction.
Is this the intended behaviour? Is there a way to get around it and keep the fields defined in admin as well as the programmatically defined field?
Here’s a sample of the code I’m using:
add_action('acf/init', 'add_some_field');
function add_some_field(){
acf_add_local_field(array(
'key' => 'field_test_123',
'label' => 'TEST FIELD',
'name' => 'test_field',
'type' => 'text',
'parent_layout' => '57a871c336e7b', // The layout's key
'parent' => 'field_5784aa9f079d5', // The Flexible Content Field's key
));
}
Hi @erika_thefarm
I’m afraid you can’t add additional fields via PHP code to a flexible content that is created via wp-admin (backend). Kindly create the whole flexible content via PHP only or via wp-admin only.
I hope this makes sense 🙂
Thanks, that explains why it didn’t work! 🙂
(Out of curiosity, does the same hold for adding fields to fields/groups other than Flexible Content? Or is Flexible Content special in this regard?)
Hi @erika_thefarm
I believe you can add fields via PHP to a field group, but only if it’s an empty group. If not, it will remove all of the existing fields.
Hope this helps 🙂