Dynamic Select/Radio/Checkbox options

It’s possible to set the available options of a Select Menu, Radio Buttons, Checkboxes or Multi Select element using a bit of PHP code.

Note: you should go to the element settings, on Data tab, and turn off the Validate submitted value option otherwise you will not be able to submit a dynamically added option.

Use WordPress pages as options

Add the following code to the theme functions.php file (or create a plugin for it).

123
4
5
6
7
8
9
10
11
12
13
14
add_action('quform_pre_display_1', function (Quform_Form $form) {    $element = $form->getElement('quform_1_3'); 
    if ($element instanceof Quform_Element_Multi) {
        $options = array();
        $posts = get_posts(array('numberposts' => -1, 'post_type' => 'page'));
 
        foreach ($posts as $post) {
            $options[] = array('label' => $post->post_title, 'value' => $post->post_title, 'id' => $post->ID);
        }
 
        $element->setOptions($options);
    }
});
add_action('quform_pre_display_1', function (Quform_Form $form) {
    $element = $form->getElement('quform_1_3');

    if ($element instanceof Quform_Element_Multi) {
        $options = array();
        $posts = get_posts(array('numberposts' => -1, 'post_type' => 'page'));

        foreach ($posts as $post) {
            $options[] = array('label' => $post->post_title, 'value' => $post->post_title, 'id' => $post->ID);
        }

        $element->setOptions($options);
    }
});
  • On line 1, replace the number 1 with the form ID
  • On line 2, replace 1_3 with the element unique ID
1
2
34
5
6
7
8
9
10
11
12
13
14
15
16
function my_set_form_options(Quform_Form $form)
{
    $element = $form->getElement('quform_1_3'); 
    if ($element instanceof Quform_Element_Multi) {
        $options = array();
        $posts = get_posts(array('numberposts' => -1, 'post_type' => 'page'));
 
        foreach ($posts as $post) {
            $options[] = array('label' => $post->post_title, 'value' => $post->post_title, 'id' => $post->ID);
        }
 
        $element->setOptions($options);
    }
}
add_action('quform_pre_display_1', 'my_set_form_options');
function my_set_form_options(Quform_Form $form)
{
    $element = $form->getElement('quform_1_3');

    if ($element instanceof Quform_Element_Multi) {
        $options = array();
        $posts = get_posts(array('numberposts' => -1, 'post_type' => 'page'));

        foreach ($posts as $post) {
            $options[] = array('label' => $post->post_title, 'value' => $post->post_title, 'id' => $post->ID);
        }

        $element->setOptions($options);
    }
}
add_action('quform_pre_display_1', 'my_set_form_options');
  • On line 3, replace 1_3 with the element unique ID
  • On line 16, replace the number 1 with the form ID
Be inspired. © 2024 ThemeCatcher Ltd. 20-22 Wenlock Road, London, England, N1 7GU | Company No. 08120384 | Built with React | Privacy Policy