Docs & Support

Learn about all the Formidable features and get support from our amazing customer success team.

frm_get_default_value

Heads up!
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.

Use the frm_get_default_value filter to set the default value of any field when your form is displayed. The default value only applies to new entries. It isn't applied when editing an entry.

Usage

add_filter( 'frm_get_default_value', 'my_custom_default_value', 10, 5 );
function my_custom_default_value( $new_value, $field, $is_default = true, $return_array = false, $args = array() )

Formidable Forms makes advanced site building simple. Launch forms, directories, dashboards, and custom WordPress apps faster than ever before.

Parameters

  • $new_value (string or array): The value to filter. Return the value you want to use.
  • $field (object): The field being shown. Use $field->id to pick a field.
  • $is_default (boolean): True when setting the default value. False in other cases, such as a placeholder or a calculation.
  • $return_array (boolean): True if an array can be handled correctly. Not always passed.
  • $args (array): Only passed when setting a calculation. In that case, $args['is_calc'] is true.

Not every call passes all 5 parameters. Give $is_default, $return_array, and $args default values in your function, as the Usage code does.

Examples

Learn where to put the code when adding a custom PHP function to your site.

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_field_default_value', 10, 3);
function my_field_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. The pass_entry parameter in the page address holds the ID or key of an entry in the same form, such as ?pass_entry=123. Without the parameter, the code doesn't change the fields.

Change 25, 26, and 27 to your field IDs. Replace 'pass_entry' (in 2 places) with the parameter name in your URL.

Since Formidable Forms 7.0, the [frm-field-value] shortcode in this code fills the fields only for users who can view entries in the admin area (administrators by default) and for the person who submitted the entry. Since 7.0.1, that includes a logged-out visitor, in the same browser, for 1 hour after they submit the entry. Everyone else, including other logged-out visitors, gets blank fields.

To change how long guests see their own entry, use the frm_guest_entries_cookie_expiration hook.

The same rule applies to a fixed entry ID, such as entry=123, in this code's shortcode. A shortcode with a fixed entry shows to every visitor only when it's typed in the field's Default Value box in the form builder. When the values are meant to be public, you can show those fields to every visitor instead.

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 referring URL

Use this code to save the URL of the page the visitor was on before opening the page with your form. Change 25 to the ID of the field.

When the visitor comes from another site, most browsers send only that site's domain, such as https://example.com/, not the full page URL. Browsers don't always send a referrer, for example after a direct visit or because of privacy settings. In that case, the field is empty.

The code also tells page caching plugins not to cache the page with your form. Without it, a cached page shows the first visitor's referring URL to everyone after them.

After you add the code, clear your page cache once. If your caching plugin still caches the page, exclude the page with your form in the plugin's settings.

add_filter( 'frm_get_default_value', 'frm_set_referring_url', 10, 3 );
function frm_set_referring_url( $new_value, $field, $is_default = true ) {
	if ( $field->id == 25 && $is_default ) { //change 25 to the ID of the field
		$referrer  = isset( $_SERVER['HTTP_REFERER'] ) ? wp_unslash( $_SERVER['HTTP_REFERER'] ) : '';
		$new_value = esc_url_raw( $referrer ); //empty when the browser doesn't send a referrer
		if ( ! defined( 'DONOTCACHEPAGE' ) ) {
			define( 'DONOTCACHEPAGE', true ); //keep page caching plugins from saving this visitor's URL for everyone
		}
	}
	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_for_user', 10, 2);
function auto_populate_address_for_user( $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. The entry ID is passed to the current form as a parameter named "pass_entry." Use this code only for addresses meant to be public, because anyone who changes the entry ID in the page address sees that entry's address.

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_by_entry', 10, 2 );
function auto_populate_address_by_entry( $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, such as 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

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 want as your random number and 999999 to 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 adds a new first option to a dropdown and selects it when the form loads. The dropdown's other options stay.

The 'value' is used only when the dropdown has Use separate values turned on. Otherwise, the option's value is its label.

add_filter( 'frm_get_default_value', 'my_dropdown_default_value', 10, 3 );
function my_dropdown_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. Change 25 to the ID of your calculated field.

add_filter( 'frm_get_default_value', 'add_a_value_to_default', 10, 5 );
function add_a_value_to_default( $new_value, $field, $is_default = true, $return_array = false, $args = array() ) {
	if ( (int) $field->id === 25 && ! empty( $args['is_calc'] ) ) { // Change 25 to the ID of your calculated field.
		$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 generates a 10-character string.

add_filter( 'frm_get_default_value', 'frm_set_random_characters', 10, 3 );
function frm_set_random_characters( $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;
}
Was this article helpful? *

This article may contain affiliate links. Once in a while, we earn commissions from those links. But we only recommend products we like, with or without commissions.

In this article

    We have a small, but amazing team of dedicated people who are committed to helping you achieve your goals and project requirements.


    Copyright © 2026 Strategy11, LLC. Formidable Forms® is a registered trademark Strategy11, LLC.

    Complete your purchase
    Don't forget your purchase!
    Complete Purchase
    Join 400,000+ using Formidable Forms to create form-focused solutions fast. Get Formidable Forms