• Join the Best Wordpress and 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!

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) { // Declare new field type $types['my_option'] = "MyOption"; return $types; }); add_filter('elementor_pro/forms/render/item/my_option', function ($item, $item_index, $instance) { // Retrieve post id $id = get_the_id(); // Build array of values $states = [ get_post_meta( $id, 'my_choice1', true ), get_post_meta( $id, 'my_choice2', true ), get_post_meta( $id, 'my_choice3', true ), ]; // Build option list $field_options = []; foreach ($states as $val) { if ($val != '') { $field_options[] = $val; } } $item['field_options'] = implode("\n", $field_options); $item['field_type'] = "select"; // Return formatted options return $item; },99,3);

Elementor should take this into account, though.

Works fine for me. Hope this helps.

Patrick
 
M

mates2

New Member
Hello, it works fine. Thank you Samoreen. What if I would like to add 2 custom options?
For example: MySelect1, MySelect2

Please help me if you can :)
Thank you!
 

Latest Resources

Other Elementor Resources

elementor official

Still stuck? Ask the forum

Post your question with a screenshot and a link if you can, and a member will usually reply within a day. Free to join, no sales pitch.

Create a free account
Top