Issue
Client wants to add to the user-entered “Other” value into the dropdown, checkbox or radio button group.
Use Case
Client is a service organization and its clients are multilingual. He currently has a checkbox list of languages for his clients to identify as their spoken languages. The final choice is “Other”. If a user chooses Other and enters a value, client would like to automatically add that value to the list of options.
Solution
Use the formidable filters frm_after_create_entry and frm_after_update_entry to get the Other value entered in and add it to the Options column of the field element’s entry (wp_frm_fields).
Credits to
Tree Martschink (@Tree Martschink on the Formidable Slack channel) for having this situation.
Caveats
You open yourself to people adding unwanted values to your list, either with typos, unacceptable language or possible hacking.
Code
add_action('frm_after_create_entry', 'after_saved_check_other', 30, 2); add_action('frm_after_update_entry', 'after_saved_check_other', 10, 2); function after_saved_check_other($entry_id, $form_id) { if ($form_id == 4) { // change 4 to your form's id $fieldId = 10; // change 10 to your checkbox's field id $values = $_POST['item_meta'][$fieldId]; // NOTE: for multi-select fields the $val is an array that can look like this Array ( [0] => Option 1 [other_2] => Other ) // so, the values for Dropdowns, Radio Buttons and Checkboxes should be handled differently // since in the case of a checkbox, "Other" could be one of the choices, while in the case of a // radio button there's only one choice // For this example, I will handle the more complex case of a checkbox foreach ($values as $key => $option) { if ($option == "Other") { $otherKey = $key; $newVal = array_values(FrmEntryMeta::get_entry_meta_by_field($entry_id, $fieldId))[0]; $newVal = sanitize_text_field($newVal); global $wpdb; $sql = "SELECT options FROM {$wpdb->prefix}frm_fields WHERE id=%d"; $sql = $wpdb->prepare($sql, $fieldId); $options = $wpdb->get_var($sql); $options = unserialize($options); // convert to a format that's easier to manipulate // if the Other value is not already in the array, add it // NOTE: You may choose to do something to the new value such as camel-case it here if (!in_array($newVal, $options)) { unset($options[$otherKey]); // piece together the new array (btw, I tried array_slice and couldn't get it to work) $newOptionArr = array("label" => $newVal, "value" => $newVal); array_push($options, $newOptionArr); $options[$otherKey] = "Other"; $options = serialize($options); // convert back to format for saving $sql = "UPDATE {$wpdb->prefix}frm_fields SET options='%s' WHERE id=%d"; $sql = $wpdb->prepare($sql, $options, $fieldId); $wpdb->query($sql); // the following is necessary to clear the cache for the change to take place immediately FrmField::delete_form_transient( $form_id ); } break; // drop out because there can be only one Other choice } } } }
*FrmField::delete_form_transient( $form_id) is key, otherwise you won’t see your change until hours or days later.