Home › Forums › General Issues › Variable for select choices?
Hi there,
I currently have a select field inside a repeater where the choices are setup like this
/wp-content/uploads/2023/02/imagered.png : Red
/wp-content/uploads/2023/02/imagewhite.png : White
/wp-content/uploads/2023/02/imageblack.png : Black
It’s working perfectly however I’ve now been asked to make a custom option. I’m curious if there’s a way for me to have the value of an image field populate the choices. So for example it would look like this:
/wp-content/uploads/2023/02/imagered.png : Red
/wp-content/uploads/2023/02/imagewhite.png : White
/wp-content/uploads/2023/02/imageblack.png : Black
Custom
If custom is chosen then it would show an image field that is conditional on Custom being selected and in doing so would also populate the value that is set so the end result is
/wp-content/uploads/2023/02/imagered.png : Red
/wp-content/uploads/2023/02/imagewhite.png : White
/wp-content/uploads/2023/02/imageblack.png : Black
{value of conditional image field fire url} : Custom
Short answer: not really, no.
Long answer:
Option 1:
You would probably also need to have them input a label for the custom choice. You could create both an acf/load_value and an acf/prepare_field filter. In the acf/load_value filter you would set the field value of custom to the value of the image field. In the acf/prepare_field filter you would include the new value as one of the choices. Both would be required to ensure that the value is set and remains selected. You would also need to keep the “custom” value so that they could change it.
Option 2:
Create an acf/load_value field for the select field. In this filter check the value and then return the image url from the image field if set to custom.
Option 3:
use the function acf_update_field() which is not documented, to permanently change the selections of the field. This will only work if you are not using acf-json. Basically you create an acf/save_post filter. In this filter you use get_field_object() to get the field and you update the choices then call acf_update_field() with the changed field object. It is also possible at this point it would probably also be possible to force the re-saving of a json file but at the moment I’m writing this I don’t know what function would be used or how to call it.
The only reason I would do one of these is if you cannot do this by simply editing the PHP template.
You must be logged in to reply to this topic.
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.