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;
  }