By using Elementorforum.com’s services you agree to our Cookies Use and Data Transfer outside the EU.
We and our partners operate globally and use cookies, including for analytics, personalisation, ads and Newsletters.

  • Join the Best Unofficial Elementor Support forum

    Provide or get advice on everything Elementor and Wordpress, ask questions, gain confirmation or just become apart of a friendly, like minded community who love Wordpress and Elementor


    Join us!
elementor official

How to dynamically set options in radio/select/checkbox form fields from ACF custom field ?

Samoreen

Samoreen

New Member
Hi,

I'm setting up a form in an Elementor template and I'd like to populate the "choices" setting with values retrieved from an ACF custom field. I have tried to use the "dynamic" button and pointed the value to a "text area" custom field containing all the options. This doesn't work.

So, I don't know how to proceed. Which type of ACF custom field should I use ? How should I retrieve its contents to automatically populate the options field of my radio form field ?

Thanks very much in advance.

Patrick
 
Samoreen

Samoreen

New Member
Hi,

I eventually found a solution. Let's assume that you want to dynamically retrieve the option values for a generic option named MyOption. The option values can be found in custom fields named my_choice1, my_choice2 and my_choice3. Here is the code for a replacement of the "Select" type.

add_filter('elementor_pro/forms/field_types', function ($types){$types['my_option'] = "MyOption"; return $types; }); add_filter('elementor_pro/forms/render/item/my_option', function ($item, $item_index, $instance){$id = get_the_id(); $states = [ get_post_meta( $id, 'my_choice1', true ),get_post_meta( $id, 'my_choice2', true ),get_post_meta( $id, 'my_choice3', true ),]; $field_options = []; foreach ($states as $val){if ($val != ''){$field_options[] = $val; } } $item['field_options'] = implode("\n", $field_options); $item['field_type'] = "select"; return $item; },99,3);

Elementor should take this into account, though.

Works fine for me. Hope this helps.

Patrick
 

Latest posts

Latest Resources

Other Elementor Resources

elementor official
Top