Home › Forums › Feature Requests › Local fields defined by PHP : auto-generate unique keys › Reply To: Local fields defined by PHP : auto-generate unique keys
The main issue I’ve run into when doing this is that the key must remain the same for each instance. Field keys can be dynamically generated, but once they are for a specific field they must then remain the same forever. What happens when you remove one, then some dynamic value changes and all the keys end up different. Or maybe you add a new one that gets generated before one that is already added and the field keys for the new one use the field keys for the old one and the old fields get new keys. When generating fields using PHP you also cannot depend on the field being defined already so you can’t even use
get_field_object($field_key) to see if it has been defined already.
Not only do field keys need to be unique and never change, but depending on where you’re adding the fields, the field names may also need to be unique and never change. The only time you can safely repeat a field name is if that field is a sub field of a repeater, flex or group field.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
© 2023 Advanced Custom Fields.