Dynamically Populating Select, Checkbox and Radio FieldsPRO

You can use WS Form functions to dynamically populate select, checkbox or radio fields.

Example Code

Here is example code for changing the dropdown options of a select field with ID 321 in form ID 123. This example also changes the label of the select field. You would put this code in your themes functions.php file.

<?php

  // Add filter to change form ID 123 prior to rendering
  add_filter('wsf_pre_render_123', 'my_pre_render_123');

  // My function for filter wsf_pre_render_123
  function my_pre_render_123($form) {

    // Get reference (&) to select field ID: 321
    $field = &wsf_form_get_field($form, 321);

    // Change the field label
    $field['label'] = 'My new select name';

    // Clear the rows in the select field
    wsf_field_rows_clear($field);

    // Create a new select row
    $row = array(

      // Set row default parameter (Sets this row as selected)
      'default' => true,

      // Add one column to the row data
      'data' => array('Option 1')
    );

    // Add the row to the select field
    wsf_field_row_add($field, $row);

    // Return the form
    return $form;
  }

Code Breakdown

WordPress Filter

Prior to WS Form rendering a form, a WordPress filter runs called wsf_pre_render_{form_id}, where {form_id} is the ID of your form. WS Form passes a form array to this filter function that can be used for editing all elements of the form.

An example of using this filter is shown below:

<?php

  // Add filter to change form ID 123 prior to rendering
  add_filter('wsf_pre_render_123', 'my_pre_render_123');

  // My function for filter wsf_pre_render_123
  function my_pre_render_123($form) {

    // Your code here
  }

Getting a Field

To change a field in your form, you can use &wsf_form_get_field($form, $field_id) where $form is the form passed to your filter function from WS Form.  $field_id is the ID of the field you want to retrieve from form. You can find the field ID in the layout editor. This function returns a reference to the field that contains an array of the field settings.

Changing a Field Label

To change the label of a field, you simply change the value of the label attribute in the field array.

For example:

<?php

  // Add filter to change form 123 prior to rendering
  add_filter('wsf_pre_render_123', 'my_pre_render_123');

  // My function for filter wsf_pre_render_123
  function my_pre_render_123($form) {

    // Get reference (&) to select field (ID: 321)
    $field = &wsf_form_get_field($form, 321);

    // Change the field label
    $field['label'] = 'My new select name';

    // Return the form
    return $form;
  }

Clearing a Select Field

To clear the options in a select dropdown, you use the wsf_field_rows_clear($field) function, where $field is the field returned from the &wsf_form_get_field($form, $field_id) function (See above).

An example of clearing a select field is given below:

<?php

  // Add filter to change form 123 prior to rendering
  add_filter('wsf_pre_render_123', 'my_pre_render_123');

  // My function for filter wsf_pre_render_123
  function my_pre_render_123($form) {

    // Get reference (&) to select field (ID: 321)
    $field = &wsf_form_get_field($form, 321);

    // Clear the rows in the select field
    wsf_field_rows_clear($field);

    // Return the form
    return $form;
  }

Adding a Option to a Select Field

To add an option to a select dropdown, you use the wsf_field_row_add($field, $row) function, where $field is the field returned from the &wsf_form_get_field($form, $field_id) function (See above).

An example of adding a select dropdown option is given below:

<?php

  // Add filter to change form 123 prior to rendering
  add_filter('wsf_pre_render_123', 'my_pre_render_123');

  // My function for filter wsf_pre_render_123
  function my_pre_render_123($form) {

    // Get reference (&) to select field (ID: 321)
    $field = &wsf_form_get_field($form, 321);

    // Create a new select row
    $row = array(

      // Set row default parameter (Sets this row as selected)
      'default' => true, 

      // Add one column to the row data
      'data' => array('Option 1')
    );

    // Add the row to the select field
    wsf_field_row_add($field, $row);

    // Return the form
    return $form;
  }

Populating a Select Field with Custom Post Type ID’s and Titles

The example pre-fills a select field with custom post type ID’s as the option values and the post title as the option labels.

In order to make this work, you need to:

  1. Add a ‘Select’ field to your form and click the gear icon to edit the field settings.
  2. Click on the ‘Options’ tab.
  3. In the data grid, add a second column by clicking the  icon (top right of the data grid).
  4. Name the first column Value. Name the second column Label. To rename the columns, you double click the column headings.
  5. Below the data grid, set the Label and Value dropdown settings to the appropriate columns.

See the screenshot below for how this looks in WS Form PRO.

Populating A Select Field Dynamically

In the example PHP code below you would change:

  • 123 to the form ID
  • 321 to the Select field ID
  • 'page' to the custom post type ID you want to read data from

This would typically go in your theme functions.php script.

<?php

  // Add filter to change form 123 prior to rendering
  add_filter('wsf_pre_render_123', 'my_pre_render_123');

  function my_pre_render_123($form) {

    // Get reference (&) to select field (ID: 321)
    $field = &wsf_form_get_field($form, 321);

    // Clear the rows in the select field
    wsf_field_rows_clear($field); 

    // Get posts (Change post_type to your custom post type, this example uses page)
    $args = array('post_type' => 'page', 'posts_per_page' => -1);
    $custom_posts = get_posts($args);

    foreach($custom_posts as $custom_post) {

      // Create a new select row
      $row = array(

        // Add one column to the row data
        'data' => array($custom_post->ID, $custom_post->post_title)
      );

      // Add the row to the select field
      wsf_field_row_add($field, $row);
    }

    // Return the form
    return $form;
  }