Home › Forums › Backend Issues (wp-admin) › How to retrieve field type keys and default values?
I am looking for a way to retrieve the default field keys and values. I am not looking to retrieve a field that has already been created/saved, instead I just want the default keys and values that are used when creating that field. Is there a way?
For instance the tab field, has these settings (see below)… I got this from the export tool, but is there a way to get them with a func/filter?
I was manually duplicating this in my code, but I figured it would be best to retrieve from the plugin, in case the plugin updates the field keys.
array (
'key' => '',
'label' => '',
'name' => '',
'type' => 'tab',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'placement' => 'top',
'endpoint' => 0,
),
get_field_object()
will do that https://www.advancedcustomfields.com/resources/get_field_object/
get_field_object()
expects the field to be saved. I am trying to retrieve the default values found in the field classes.
But I think I wont need that anymore, since the defaults are passed during field creation.
If you want to get information about a field that’s not created yet for a particular post, then you need to use get_field_object() using the field key.
The load_field filter can be used to get the information when a field is loaded https://www.advancedcustomfields.com/resources/acfload_field/
ACF will never rename a field key once it’s create. Unless Elliot decides to completely rework everything about the way ACF works.
I think we are talking about 2 different things, and it’s probably my fault. I’m working on a options page. All fields in the options page are created on the fly, so I wanted to get the default field keys per field type as defined in the plugin and then merge with the key/values I am using. I was try to access the class directly… but I failed to realize that the acf_add_local_field already does that for me.
You may have confused me even more.
Are you generating all the field keys on the fly with each page load? Meaning that the field keys are constantly changing?
Or just that you want to make sure you’re not using something that’s already used?
Sorry. I am pretty terrible at describing things… the key fields are generated on the fly but based on a bunch of other information, so they will be unique but the same every time.
Anyway, no worries, because I was trying to merge my arrays with the default acf array, but that’s is already done by ACF when adding a field.
K, all makes sens to me know. I do the same thing. The only requirements for the field keys is that they are unique, don’t change and start with “field_” Generally I create field like “field_prefix000001”, “field_prefix000002”, etc. so I know they will never be the same as a key that ACF will ever create. Then like you said, you’re good to just use the ACF function to add field groups and fields.
Thanks. You bring up a interesting point.. .do the field keys need to start with field_
? I didn’t see that as a requirement. And what about the field group key? Does it need to start with group_
?
Totally missed that php comment. Thanks. That will save some headache later on.
I find that this method works only after the post has been saved. If the post has not been saved yet, bool(false) is returned. Is there a way to get the default value without needing to save the post first?
If you use the field key get when calling any of the acf function the default value will be returned.
get_field('field_XXXXX');
The topic ‘How to retrieve field type keys and default values?’ 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!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.