Filter
uncanny-automator
automator_ws_form_get_form_fields
Filters form fields before they are retrieved by the WS Form Lite integration.
add_filter( 'automator_ws_form_get_form_fields', $callback, 10, 2 );
Description
Fires after WS Form Lite fields are retrieved for a specific form. Developers can use this filter to modify or filter the array of form fields before they are processed, for example, to dynamically remove or add fields. The `$fields` parameter contains the form's fields, and `$id` is the form's ID.
Usage
add_filter( 'automator_ws_form_get_form_fields', 'your_function_name', 10, 2 );
Parameters
-
$fields(mixed) - This parameter contains an array of form fields that are being retrieved or filtered for a specific form ID.
-
$id(mixed) - This parameter contains an array of form fields that have already been retrieved for a given form ID, acting as a cache.
Return Value
The filtered value.
Examples
<?php
/**
* Example of using the automator_ws_form_get_form_fields filter to remove a specific field.
*
* This example demonstrates how to hook into the filter to conditionally remove a field
* from a WS Form based on its ID and the form's ID.
*
* @param array $fields The array of form fields.
* @param int $form_id The ID of the WS Form.
*
* @return array The modified array of form fields.
*/
function my_custom_ws_form_field_removal( $fields, $form_id ) {
// Check if we're dealing with a specific form and a specific field to remove.
// Let's assume we want to remove a field with the ID 'user_email' from form with ID '123'.
$target_form_id = 123;
$field_to_remove_id = 'user_email';
if ( $form_id === $target_form_id && isset( $fields[ $field_to_remove_id ] ) ) {
// Remove the specified field from the form fields.
unset( $fields[ $field_to_remove_id ] );
}
// Always return the (potentially modified) fields array.
return $fields;
}
add_filter( 'automator_ws_form_get_form_fields', 'my_custom_ws_form_field_removal', 10, 2 );
?>
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/integrations/ws-form-lite/helpers/ws-form-lite-helpers.php:134
public function get_form_fields( $id ) {
static $fields = array();
if ( isset( $fields[ $id ] ) ) {
return $fields[ $id ];
}
try {
$form = new WS_Form_Form();
$form->id = $id;
$form_object = $form->db_read_published( true );
// Check form object to prevent fatals.
if ( ! is_object( $form_object ) || ! property_exists( $form_object, 'id' ) || ! property_exists( $form_object, 'label' ) ) {
throw new Exception( 'Invalid form object' );
}
$fields[ $id ] = WS_Form_Common::get_fields_from_form( $form_object, true );
foreach ( $fields[ $id ] as $field_id => $field ) {
// Remove submit button - Review may be other fields to omit.
if ( 'submit' === $field->type ) {
unset( $fields[ $id ][ $field_id ] );
}
}
} catch ( Exception $e ) {
$fields[ $id ] = array();
}
return apply_filters( 'automator_ws_form_get_form_fields', $fields[ $id ], $id );
}