Populate the Default Value of a Field

You can use WS Form functions to dynamically set the default value of fields.

Example Code

Here is example code for setting the default value of the field with ID 321 in form ID 123. 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 select field ID: 321
    $field = wsf_form_get_field($form, 321);

    // Change the default value of the field
    $field->meta->default_value = 'Default value here';

    // 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 the field settings.

Changing the Default Value

To change the default value of a field, you simply change the value of the default value in the meta property.

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 the select field (ID: 321)
    $field = wsf_form_get_field($form, 321);

    // Change the field default value
    $field->meta->default_value = 'Default value here';

    // Return the form
    return $form;
  }