Hi @sinned1
I hope you are doing well.
You can use something like this:
<?php
function handle_my_field_option_update()
{
$value = 'test';
$label = 'test';
$new_options = array(
'label' => $label,
'value' => $value,
'limit' => '',
'key' => forminator_unique_key(),
);
$form_id = 26197;
$elem_id = 'checkbox-1';
$to_array = true;
$current_field = Forminator_API::get_form_field($form_id, $elem_id, $to_array);
$current_field['options'][] = $new_options;
Forminator_API::update_form_field($form_id, $elem_id, $current_field);
}
// register ajax
add_action('wp_ajax_handle_my_field_option_update', 'handle_my_field_option_update');
add_action('wp_ajax_nopriv_handle_my_field_option_update', 'handle_my_field_option_update');
add_filter('wp_footer', function () {
?>
<script>
jQuery(document).ready(function($) {
$.ajax({
url: '<?php echo admin_url('admin-ajax.php'); ?>',
type: 'POST',
data: {
action: 'handle_my_field_option_update',
},
success: function(response) {
console.log(response);
}
});
});
</script>
<?php
}, 9999);
This is a simple test that updates the option by adding “test” for label and value.
But some consideration here.
– This means updating the Forminator database, for example you have two people submitting the form at the same time you run into issues.
– This will print the custom entry for the next user unless you reset the default values after submission, but let’s say your user leaves the page then it will cause the same problem. A possible solution for it is to reset the options on form load and for that, you can use this hook https://gist.github.com/wpmudev-sls/6f1c8989a170b8f02026354ffe5ad24f#file-forminator-render-select-from-csv-php-L18
A better option is to detach the “other” option from your field, you can use the repeater https://wpmudev.com/docs/wpmu-dev-plugins/forminator/#field-group-repeater and create the “Add your option” as a group which allow you to create the values on the fly and will be handled by Forminator by default.
Best Regards
Patrick Freitas