Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Shelob9/9e1312d3b37d4f78438cc9c8bc9ea9a5 to your computer and use it in GitHub Desktop.
Save Shelob9/9e1312d3b37d4f78438cc9c8bc9ea9a5 to your computer and use it in GitHub Desktop.
Examples of using the Caldera Forms filter caldera_forms_render_field_structure to modify field display when rednering forms, see: https://calderaforms.com/doc/caldera_forms_render_field_structure/
<?php
/**
* Change the default for a dropdown to "red" if a specific user meta key is set for current user
*/
add_filter( 'caldera_forms_render_field_structure', function( $field_structure, $form ){
//make sure to change field ID to your field's ID here!
//not that $field_structure is not the same as field config, but that is stored in $field_structure[ 'field' ]
if( 0 != get_current_user_id() && 'fld_1234' == $field_structure[ 'field' ][ 'ID' ] ) {
if( 'red' == get_user_meta( get_current_user_id(), 'color', true ) ){
$field_structure[ 'default' ] = 'red';
}
}
return $field_structure;
}, 10, 2 );
<?php
/**
* Change the value of a specific field based on a time comparison
*/
add_filter( 'caldera_forms_render_field_structure', function( $field_structure, $form ){
//make sure to change field ID to your field's ID here!
//not that $field_structure is not the same as field config, but that is stored in $field_structure[ 'field' ]
if( 'fld_1234' == $field_structure[ 'field' ][ 'ID' ] ) {
//IMPORTANT - for American-style dates use a "/" IE - m/d/y, for European style dates, use a "-" IE d-m-y for euro style dates, because PHP
if( time() > strtotime( '02/11/2016' ) ){
//For hidden fields use "value", for other types, use "default"
$field_structure[ 'field_value' ] = 50;
}
}
return $field_structure;
}, 10, 2 );
<?php
/**
Move field description (caption) above input, below label
**/
add_filter( 'caldera_forms_render_field_structure', function( $field_structure, $form){
//place description after label
$field_structure[ 'label_after' ] .= $field_structure[ 'field_caption' ];
//empty description so it doesn't show twice
$field_structure[ 'field_caption' ] = '';
return $field_structure;
}, 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment