This filter allows you to modify the field’s config before it’s rendered, based on the fields slug. This is the perfect place to dynamically set options of select fields like checkbox, radio, dropdown etc.
Using the slug allows you to define a prepopulate rule based on the slug. So to use it, you’ll simply give the field the appropriate slug.
You can also hook in based on type. See caldera_forms_render_get_field_type-{field_type}
This filter is passed 2 parameters:
- $field – the field config array
- $form – the form config structure array
Here’s an example on populating a dropdown field from a remote json source.
Where Does This Code Go?
When using WordPress hooks to customize Caldera Forms or other plugins you should not modify the plugin files, or you will lose your changes when you update the plugin. Instead you should create a small plugin to hold the custom code. It's easy, learn how here.
Technically you can add the custom code to your theme's functions.php, but then you will not be able to change your theme and keep these customizations.
Hi, WordPress newbie here; I can’t get this to work 🙁
Where do you put this code, and where do you see it in action (in the layout builder or in the resulting form)? Thanks a lot for your answer
This is what is referred to a Filter. They are by developers to extend functionality and to customize the things for specific projects. The code is generally used in a custom plugin or the themes functions.php file.
Thanks a lot David, works very well! Cheers
Hi David,
I have a new question for you.
I’m trying to shuffle the order of the entries in a checkbox group.
I’m using the code below in a checkbox field named random.
It all works well.
Then I uncomment the shuffle($options); statement.
Checkboxes are shuffled all right, but so are the results in the database.
Entries saved in the db are very different from those actually checked in the form, with no apparent logic.
Any idea what’s going on and how to get the proper results?
Thanks for your time,
PHE
—————————
add_filter(‘caldera_forms_render_get_field_slug-random’, ‘cf_get_data_random’);
function cf_get_data_random($field){
$field[‘config’][‘option’] = array();
$options = array();
$options[] = ‘AAAAAA’;
$options[] = ‘BBBBBB’;
$options[] = ‘CCCCCC’;
$options[] = ‘DDDDDD’;
$options[] = ‘EEEEEE’;
// shuffle($options);
for ($x = 0; $x $options[$x],
‘label’ => $options[$x] ); }
return $field;
}
Solved…
The function was re-entered upon form submission, hence the reshuffling.
Got around by using a repeatable random generator found here: http://www.sitepoint.com/php-random-number-generator/
Cheers,
PHE
Well done. I would like to see a user case when you’re done.