This article contains PHP code and is intended for developers. We offer this code as a courtesy, but don't provide support for code customizations or 3rd party development.
Set the default value of any field when your form is displayed. The default value will ONLY apply to new entries and will not be applied when editing an entry.
Usage
add_filter('frm_get_default_value', 'my_custom_default_value', 10, 2); function my_custom_default_value($new_value, $field)
Parameters
- $new_value (string or array)
- $field (object)
- $is_default (boolean) - will be false if setting a placeholder
- $return_array (boolean) - true if an array can be handled correctly
Examples
Set the default value of a field
The following code can be used to set the default value of a field.
add_filter('frm_get_default_value', 'my_custom_default_value', 10, 3);
function my_custom_default_value( $new_value, $field, $is_default ) {
if ( $field->id == 25 && $is_default ) { //change 25 to the ID of the field
$new_value = 'default'; //change 'default' to your default value
}
return $new_value;
}
Conditionally switch default value
The following code can be used to conditionally change the default value in a field, depending on a parameter in the URL. Change 25, 26, and 27 to your field IDs. Replace 'pass_entry' (in two places) with the parameter name in your URL.
add_filter('frm_get_default_value', 'switch_my_default_value', 10, 2);
function switch_my_default_value( $new_value, $field ){
if ( in_array( $field->id, array( 25, 26, 27 ) ) ) {
if ( isset( $_GET['pass_entry'] ) ) {
$new_value = do_shortcode( '[frm-field-value field_id=' . $field->id . ' entry=pass_entry]' );
}
}
return $new_value;
}
Populate a field with the original referring url
If you would like to save the url of the site that the user came from, you can use this code. This will only work if you also go to the Formidable -> global settings page and turn on user tracking.
add_filter('frm_get_default_value', 'my_custom_default_value', 10, 2);
function my_custom_default_value($new_value, $field){
if($field->id == 25){ //change 25 to the ID of the field
$new_value = reset($_SESSION['frm_http_referer']); //stores the value of the referring URL
}
return $new_value;
}
Select current user's option in Dynamic field
Use this code to dynamically select the option created by the current user in a Dynamic field.
add_filter('frm_get_default_value', 'auto_populate_dynamic_field', 10, 2);
function auto_populate_dynamic_field($new_value, $field){
if ( $field->id == 25 ) { //change 25 to the ID of the Dynamic field
global $wpdb;
$user = wp_get_current_user();
$entry_id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM " . $wpdb->prefix . "frm_items WHERE form_id=%d AND user_id=%d ORDER BY created_at DESC", 216, $user->ID ) );// Change 216 to the ID of the linked form
$new_value = $entry_id;
}
return $new_value;
}
Auto-populate address field for current user
Use the code below to automatically populate an address field with the current user's most recent entry in another address field. Replace 250 with the ID of the field you want to autopopulate. Replace 256 with the ID of the Address field that you want to get the value from.
add_filter('frm_get_default_value', 'auto_populate_address_field', 10, 2);
function auto_populate_address_field( $new_value, $field ) {
if ( $field->id == 250 ) { //change 250 to the ID of the field you want to autopopulate
global $wpdb;
$user = wp_get_current_user();
$get_field_id = '256';
$get_field = FrmField::getOne( $get_field_id );
if ( $user && $get_field ) {
$query = "SELECT id FROM " . $wpdb->prefix . "frm_items WHERE user_id=" . $user->ID . " AND form_id=" . $get_field->form_id . " ORDER BY id DESC";
$entry_id = $wpdb->get_var( $query );
if ( $entry_id ) {
$entry = FrmEntry::getOne( $entry_id );
$new_value = FrmProEntryMetaHelper::get_post_or_meta_value( $entry, $get_field );
}
}
}
return $new_value;
}
Auto-populate address field by passed entry ID
Use the code below to automatically populate an address field with the address field of a specified entry where the entry ID has been passed to the current form as a parameter named "pass_entry." Replace 135 with the ID of the field you want to autopopulate. Replace 120 with the ID of the Address field from which you want to get the value.
add_filter( 'frm_get_default_value', 'auto_populate_address_field', 10, 2 );
function auto_populate_address_field( $new_value, $field ) {
if ( $field->id == 135 ) { //change 135 to the ID of the field you want to autopopulate
if ( isset( $_GET['pass_entry'] ) && $_GET['pass_entry'] !== '' ) {
$get_field = FrmField::getOne( 120 );//change 120 to the ID of the first address field
$entry = FrmEntry::getOne( $_GET['pass_entry'] );
$new_value = FrmProEntryMetaHelper::get_post_or_meta_value( $entry, $get_field );
}
}
return $new_value;
}
Set a date relative to the current date
You can set a default value in a Date or text field that's a set number of days, weeks, months, or years before or after today.
Replace 4615 with the id of the Date or text field whose default value you want to set. Replace '+15 days' with your desired value.
If you like, you can change the reference date from 'today' to any other date, e.g. the 'first day of next month.'
add_filter( 'frm_get_default_value', 'frm_set_relative_date', 10, 2 );
function frm_set_relative_date( $new_value, $field ) {
if ( $field ->id == 4615 ) { //change 4615 to the ID of the field
$start = strtotime( 'today' );//change 'today' to any set or relative date
$final = strtotime( '+15 days', $start );//change +15 days to your desired value
$new_value = date( 'Y-m-d', $final );
}
return $new_value;
}
'
Set random number as default value
You can use this code example to generate a random number and set it as the default value of a field.
Change 11610 to the id of the field whose default value you want to set. Change 100000 to the lowest number you'd want as your random number and 999999 as the highest.
add_filter( 'frm_get_default_value', 'frm_set_random_number', 10, 3 );
function frm_set_random_number( $new_value, $field, $is_default ) {
if ( $field->id == 11610 && $is_default ) { //change 11610 to the ID of the field
$new_value = rand( 100000, 999999 ); //change 100000 to the lowest number you'd want as your random number and 999999 to the highest
}
return $new_value;
}
Set the default value of a dropdown
This example will set the default value of a dropdown
add_filter( 'frm_get_default_value', 'my_custom_default_value', 10, 3 );
function my_custom_default_value( $new_value, $field, $is_default ) {
if ( $field->type === 'select' && $field->id == '25' && $is_default ) { //change 25 to the ID of the field
$default_value = array(
'label' => 'Custom label', //change 'Custom label' to your custom label
'value' => 'Default value', //change 'Default value' to your default value
'image' => '0'
);
array_unshift( $field->options, $default_value );
}
return $new_value;
}
Set the default value of a calculated field
The following code can be used to set the default value of a calculated field. It returns an integer value instead of a string.
add_filter( 'frm_get_default_value', 'add_a_value_to_default', 10, 5 );
function add_a_value_to_default( $new_value, $field, $dynamic_default, $allow_array, $args = array() ) {
if ( ! empty( $args['is_calc'] ) ) {
$new_value = 500;
}
return $new_value;
}
Generate random characters
Use this code example to generate random characters with a combination of letters and numbers and set it as a field's default value. In this instance, setting the random_bytes value to 5 will generate a ten-character long string.
add_filter( 'frm_get_default_value', 'frm_set_random_number', 10, 3 );
function frm_set_random_number( $new_value, $field, $is_default ) {
if ( $field->id == 17990 && $is_default ) { //change 17990 to the ID of the field
$new_value = bin2hex( random_bytes(5)); //Change 5 to a different number to change the size of the result.
}
return $new_value;
}