Last active
November 27, 2023 13:25
-
-
Save AnadarProSvcs/e3b1f1f3296184baaf3b6509d4b662af to your computer and use it in GitHub Desktop.
Save the values of checkboxes from a Formidable Form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//You can use this to load the values from checkboxes in a Formidable form. | |
//In Formidable Forms for WordPress, when you want to retrieve the values of checkboxes using the frm_after_create_entry hook, | |
//you need to access the form entry data that's passed to your hook function. | |
//This hook is triggered after an entry is created, and it provides you with all the submitted data. | |
add_action('frm_after_create_entry', 'get_checkbox_values', 30, 2); | |
function get_checkbox_values($entry_id, $form_id){ | |
// Check if it's the specific form you want (replace 123 with your form ID) | |
if ($form_id == 123) { | |
// Get the entry object | |
$entry = FrmEntry::getOne($entry_id, true); | |
// Replace 'your_field_key' with the key of your checkbox field | |
$checkbox_values = $entry->metas['your_field_key']; | |
// Do something with the checkbox values | |
// $checkbox_values is an array if multiple boxes can be checked | |
foreach ($checkbox_values as $value) { | |
// Handle each value | |
} | |
} | |
} | |
//You can populate the values from an array | |
//To pre-populate checkboxes in Formidable Forms using the frm_get_default_value hook, | |
//this assumes an array of values that you want to set as the default for the checkbox field. | |
//This hook allows you to set a default value for a field when the form is first loaded which can also be used to load | |
//values from a database or other source. | |
add_filter('frm_get_default_value', 'set_default_checkbox_values', 10, 2); | |
function set_default_checkbox_values($value, $field){ | |
// Check if it's the specific checkbox field (replace 'checkbox_field_key' with your field's key) | |
if ($field->id == 'checkbox_field_key') { | |
// Define the default values as an array | |
// These should match the values assigned to the checkboxes | |
$default_values = array('Value1', 'Value3'); | |
// Set the default values for the checkboxes | |
$value = $default_values; | |
} | |
return $value; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment