caldera_forms_render_get_field

Caldera Forms logo and name

This filter can be used to modify the configuration of a field before it is rendered. It is also used during form submission. Keep in mind there are two dynamically named filters for modifying fields as well.

One use for this filter is to change the default value of a field:

<?php
add_filter( 'caldera_forms_render_get_field', function( $field ) {
if( 'fld123456' == $field[ 'ID' ] ){
$field[ 'config' ][ 'default' ] = esc_url_raw( caldera_forms_get_current_url() );
}
return $field;
});

One excellent use for it is to change the options of a select field. In this example, a fields options are set using posts in a specific custom post type, with a specific value of a meta field:

<?php
add_filter( 'caldera_forms_render_get_field', function( $field ) {
if ( 'stores_in_pa' == $field[ 'slug' ] ) {
$stores = get_posts( array( 'post_type' => 'stores', 'meta_key' => 'state', 'meta_value' => 'PA' ) );
if ( ! empty( $stores ) ) {
foreach( $stores as $store ) {
$field[ 'config' ][ 'option' ][ $store->ID ] = array(
'value' => $store->ID,
'label' => $store->post_title
);
}
}
}
return $field;
});

Here is an example of how to reset the default value for a select field based on a custom field for the current user:

<?php
add_filter( 'caldera_forms_render_get_field', function( $field, $form ){
if( 'dropdown' == $field[ 'type'] && ! empty( $field[ 'config' ][ 'option' ] ) ){
//@TODO Change field name to the right user custom field
$meta = get_user_meta( get_current_user_id(), 'field_name', true );
if ( ! empty( $meta ) ) {
foreach ( $field[ 'config' ][ 'option' ] as $option => $args ) {
if ( $meta == $args[ 'value' ] ) {
$field[ 'config' ][ 'default' ] = $option;
}
}
}
}
return $field;
}, 10, 2);

Use Pods Fields To Set Caldera Forms Field Defaults

<?php
/**
* Preset a Caldera Forms field with the value of a Pods field
*/
add_filter( 'caldera_forms_render_get_field', function( $field ) {
//Change your field ID here
if( 'fld123456' == $field[ 'ID' ] ){
//change pods() arguments, see http://pods.io/docs/code/pods/
$value = pods( 'pod-name', 42 );
$field[ 'config' ][ 'default' ] = $pods->value( 'field_name' );
}
return $field;
});

How To Set Caldera Forms Field Default From ACF Field

This is the right hook to use if you wish to set the default value of a field using a valuy saved by Advanced Custom Fields (ACF)

<?php
/**
* Set Caldera Forms Field default from ACF field
*/
add_filter( 'caldera_forms_render_get_field', function( $field ) {
//Change your field ID here
if( 'fld123456' == $field[ 'ID' ] ){
$field[ 'config' ][ 'default' ] = get_field( 'field_name' );
}
return $field;
});

Make A Field Not Required

You can use the “required” index of the array exposed by this filter to control if a field is required or not. This is a good way to avoid situations where a processor makes a field required.

<?php
/**
* Set a specific Caldera Forms field to NOT be required
*/
add_filter( 'caldera_forms_render_get_field', function( $field ){
//IMPORTANT: Change this to your field's ID
if( 'fld1234' === $field[ 'ID' ] ){
$field[ 'required' ] = 0;
}
return $field;
});

Multiple Defaults For Checkbox Fields

Caldera Forms checkbox fields can have more than one default value, in Caldera Forms 1.6.0 or later.

<?php
/**
* Set a Caldera Forms checkbox field to have mutltiple defaults selected
*
* Requires: Caldera Forms 1.6 or later
*
* Important: This only works with checkox fields
*/
add_filter( 'caldera_forms_render_get_field', function( $field, $form ){
if( 'fld_9641225' === $field[ 'ID' ] ){
$field[ 'config' ][ 'default' ] = array(
'Yes', 'No'
);
}
return $field;
}, 10, 2 );

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.

Learn More

f