I’m interested in the possibility of defining a set of custom fields in php and then adding them to various different groups as needed. For example, I need an address field in a lot of different groups and it has lots of sub-fields and layouts. This would also help maintain consistency of formatting and naming when using those fields on the frontend.
The method for generating theme fields I have seen so far in the forums is based on exporting to php, then including that code.
However, that produces (depending on the complexity) a somewhat massive multi-dimensional array that is very hard to read or edit.
The only problem with my idea is that I don’t know anything about the unique field keys that are generated. I couldn’t find the function that generates them, and I don’t know if the unique character string has any relationship to the rest of the field definition (is it a hash of some of the array elements, for example?)
Can anyone tell me if they’ve done this, and if so how and what issues should I be aware of? Or what is the best method you can suggest for defining reusable fields?
1. The field_key can be ANYTHING you want but MUST start with field_
2. To generate a key in the same way that ACF does, use this:
$key = 'field_' . uniqid();
3. You can create function which returns an array (1 single field), and then call this function when you want the field. This is a way to include 1 field in multiple field groups. It will use the same field key, and that’s fine because the field_key is only used to load the field object and use it’s settings to modify the interface and value.
Thanks for the reply. I found it helpful and I am creating a series of classes now to build fields and field groups.
I have run into an issue though:
I have a base
acfField class from which I extend the different types. I was using the
uniqid() function in the constructor to generate the key IDs as you suggested, but they weren’t ‘sticking’. Every time I refresh the page, the key for any
new acfField() object regenerates. This actually prevents the field from getting written to the database when saving a post with the field(s).
Any suggestions on how to get around this? In reality I only need it in situations where 2 fields exist with the same key (the key is based on the name at the moment). So far my solution has been to manually add a incremented number to the key for situations where there is a clash, but tracking that manually could become really unwieldy, depending on the complexity of the setup. It also seems risky, in that problems could arise that are not immediately obvious.
The key for a field cannot change. If it does, the reference value will not connect the value to the field.
Think of the field’s key as a tables ID column.
Unfortunately, this cant be generated and you will need to somehow save the key to the field and reuse the same kjey on each page load.
If you are trying to create a field without defining a key for it, this will prove very difficult.
Hope that helps.
Thanks @elliot, I figured as much! Basically I need permanent storage for my field definitions, such as in the database.
In a way it seems overkill to use the database for this purpose, but from the moment I started using ACF I’ve been meaning to add a feature suggestion of saved field definitions that you could choose from when setting up an actual field, so I’ll do that now!
Thanks for the info everyone, good stuff here.
As @elliot mentioned, the key can be any (reasonable) string starting with
So while you could manually generate IDs with
uniqid(), it might make more sense to use something that has meaning and you know is unique. Like maybe
field_user_message_body where the first part might relate to the object type this is attached to.
@elliot do you see any possible issue with this? Presuming it isn’t too difficult to make those key names unique.
Just a thought 🙂
The topic ‘Creating theme fields – generating a key?’ is closed to new replies.
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!
© 2022 Advanced Custom Fields.