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.
This hook can be used to change field values, options, and other field settings when creating an entry.
Note: If you are using this hook to change the options in a dropdown field, these options will only be available when creating a new entry. The options added with this hook will not be available when updating a past entry. A dropdown field using this hook to populate its values will appear blank when editing the entry.
Usage
add_filter('frm_setup_new_fields_vars', 'my_custom_function', 20, 3); function my_custom_function($values, $field, $args )
Parameters
- $values (array)
- $field (object)
- $args (array) - can include the following:
- parent_form_id => the ID of the parent form
- parent_field_id => the ID of the parent field
- key_pointer => the row ID if in a repeating section or embedded form
Examples
Dynamically populate checkbox field
Use this code to get parameters from the URL to populate a checkbox field or multi-select dropdown.
add_filter('frm_setup_new_fields_vars', 'frm_set_checked', 20, 2);
function frm_set_checked($values, $field){
if($field->id == 125){//Replace with the ID of your field
$values['value'] = array($_GET['color1'], $_GET['color2']); //Replace color1 and color2 with the names of your parameters in the URL
}
return $values;
}
Replace field options
This code can be used to change the options in a drop-down, checkbox, or radio button field.
add_filter('frm_setup_new_fields_vars', 'frm_set_checked', 20, 2);
function frm_set_checked($values, $field){
if($field->id == 125){//Replace 125 with the ID of your field
$values['options'] = array('Option 1', 'Option 2'); //Replace Option1 and Option2 with the options you want in the field
}
return $values;
}
Populate a field with WordPress Posts
Use this code to populate a dropdown, checkbox field, or radio button field with the titles of all the posts on your site. This code will not work correctly with a dynamic field.
Enable separate values in your field settings if you want to save both the post ID and post title in the entry.
add_filter('frm_setup_new_fields_vars', 'frm_populate_posts', 20, 2);
add_filter('frm_setup_edit_fields_vars', 'frm_populate_posts', 20, 2); //use this function on edit too
function frm_populate_posts($values, $field){
if($field->id == 125){ //replace 125 with the ID of the field to populate
$posts = get_posts( array('post_type' => 'post', 'post_status' => array('publish', 'private'), 'numberposts' => 999, 'orderby' => 'title', 'order' => 'ASC'));
unset($values['options']);
$values['options'] = array(''); //remove this line if you are using a checkbox or radio button field
$values['options'][''] = ''; //remove this line if you are using a checkbox or radio button field
foreach($posts as $p){
$values['options'][$p->ID] = $p->post_title;
}
$values['use_key'] = true; //this will set the field to save the post ID instead of post title
unset($values['options'][0]);
}
return $values;
}
Remove used options
Use this code to remove all used options from a dropdown, radio, checkbox, or Dynamic field. Change 100,101, and 102 to the field IDs where the used options will be removed. There is no need to specify a form ID.
add_filter('frm_setup_new_fields_vars', 'frm_remove_selected', 20, 2);
function frm_remove_selected($values, $field){
if ( in_array( $field->id, array(100,101,102) ) ) {
$used = FrmEntryMeta::get_entry_metas_for_field( $field->id );
if ( $used ) {
$used_vals = array();
foreach ( $used as $u ) {
if ( is_array( $u ) ) {
foreach ( $u as $item ) {
$used_vals[] = $item;
unset($item);
}
} else {
$used_vals[] = $u;
}
unset($u);
}
if ( $field->type == 'data' ) {
foreach ( $used_vals as $u ) {
if ( isset($values['options'][$u]) ) {
unset($values['options'][$u]);
}
unset($u);
}
} else {
$values['options'] = array_diff( $values['options'], $used_vals );
}
}
}
return $values;
}
Remove option used variable times
This is designed to remove options from a dynamic field. The number of times the option can be used is set in the form the dynamic field pulls from. Add a quantity field in the linked form, and insert your limits there. Or if you would like all options to have the same limit, use the example above.
add_filter('frm_setup_new_fields_vars', 'frm_remove_selected', 20, 2);
function frm_remove_selected($values, $field) {
$quantity_field = 'frm-inquantity'; // change to the field key for the quantity field in the other form
$signup_field = 'frm-signup-item'; // change to the field key of the dynamic field that should be limited
if ( $field->field_key == $signup_field ) {
$used = FrmEntryMeta::get_entry_metas_for_field( $field->id );
if ( $used ) {
$used_vals = $used_count = array();
foreach ( $used as $u ) {
if ( is_array( $u ) ) {
foreach ( $u as $item ) {
$used_vals[] = $item;
frm_add_used_count( $item, $used_count );
unset($item);
}
} else {
$used_vals[] = $u;
frm_add_used_count( $u, $used_count );
}
unset($u);
}
if ( $field->type == 'data' ) {
foreach ( $used_vals as $u ) {
if ( isset( $values['options'][ $u ] ) ) {
$allowed_count = FrmProEntriesController::get_field_value_shortcode( array(
'field_id' => $quantity_field, 'entry' => $u,
) );
if ( $used_count[ $u ] >= $allowed_count ) {
unset( $values['options'][ $u ] );
}
}
unset($u);
}
} else {
$values['options'] = array_diff( $values['options'], $used_vals );
}
}
}
return $values;
}
function frm_add_used_count( $item, &$used_count ) {
if ( ! isset( $used_count[ $item ] ) ) {
$used_count[ $item ] = 0;
}
$used_count[ $item ]++;
}
Remove option after n submissions
Use the code below to remove an option from a dropdown or radio field after it has been submitted x number of times. Change 426, 427, and 428 to the field IDs that you want to limit. Change 5 to the limit that you want to set. Please note: This is not set up to work with Dynamic fields or checkbox fields at this time.
You can also hide checkbox/radio options after n submissions using frm-condition with frm-stats.
add_filter('frm_setup_new_fields_vars', 'frm_remove_full_option', 20, 2);
function frm_remove_full_option($values, $field){
if ( in_array( $field->id, array( 426, 427, 428 ) ) ) {
$submitted_values = FrmEntryMeta::get_entry_metas_for_field( $field->id );
if ( $submitted_values ) {
// Break out multi-dimensional array for checkbox field values
if ( $field->type == 'checkbox' ) {
$new_values = array();
foreach ( $submitted_values as $array_val ) {
if ( is_array( $array_val ) ) {
foreach ( $array_val as $val ) {
$new_values[] = $val;
}
} else {
$new_values[] = $array_val;
}
}
$submitted_values = $new_values;
}
$count_values = array_count_values( $submitted_values );
$remove_values = array();
foreach ( $count_values as $used_val => $count ) {
if ( $count >= 5 ) {
$remove_values[] = $used_val;
}
}
foreach ( $values['options'] as $key => $opt ) {
if ( is_array( $opt ) ) {
if ( in_array( $opt['value'], $remove_values ) ) {
unset( $values['options'][ $key ] );
}
} else {
if ( in_array( $opt, $remove_values ) ) {
unset( $values['options'][ $key ] );
}
}
}
}
}
return $values;
}
Remove an option
This example is specifically to remove a time from a time field, but can remove an option from any field.
add_filter('frm_setup_new_fields_vars', 'remove_field_option', 30, 2);
add_filter('frm_setup_edit_fields_vars', 'remove_field_option', 30, 2);
function remove_field_option( $values, $field ) {
if ( $field->id == 25 ) { // change 25 to your field id
$options_to_remove = array( '12:00 PM', '12:30 PM' );
foreach ( $options_to_remove as $remove ) {
$option_key = array_search( $remove, $values['options'] );
if ( $option_key !== false ) {
unset( $values['options'][ $option_key ] );
}
}
}
return $values;
}
Display User ID dropdown on front-end for admin
Use this code to make all user ID fields appear as dropdowns on the front-end for admin only. The user ID field will still automatically insert the currently logged-in user's ID unless admin is filling out the form.
add_filter('frm_setup_new_fields_vars', 'show_user_dropdown', 15, 2);
add_filter('frm_setup_edit_fields_vars', 'show_user_dropdown', 15, 3);
function show_user_dropdown($values, $field, $entry_id=false){
$action = FrmAppHelper::get_param( 'action', '', 'post', 'sanitize_text_field' );
$creating_field = FrmAppHelper::doing_ajax() && ( $action === 'frm_insert_field' || $action === 'frm_duplicate_field' );
if ( $values['type'] == 'user_id' && ! FrmAppHelper::is_admin() && current_user_can( 'administrator' ) && ! $creating_field ) {
$values['type'] = 'select';
$values['options'] = (new FrmProFieldUserId)->get_options( array() );
$values['use_key'] = true;
$values['custom_html'] = FrmFieldsHelper::get_default_html('select');
}
return $values;
}
Filter Dynamic Field
Use this code to filter the options in a Dynamic field. This example will make it so the option will only appear if there is a field that equals "Yes" in the linked form.
add_filter('frm_setup_new_fields_vars', 'filter_dfe_options', 25, 2);
function filter_dfe_options($values, $field){
if ( $field->id == 125 && !empty( $values['options'] ) ) {//Replace 125 with the ID of your Dynamic field
$temp = $values;
$temp['form_select'] = 30;//change 30 to the id of the field (in linked form) that you want to filter by
$field2_opts = FrmProDynamicFieldsController::get_independent_options( $temp, $field );
foreach ( $values['options'] as $id => $v ) {
if ( isset( $field2_opts[$id] ) && ( $v == '' || $field2_opts[$id] == 'Yes' ) ) {//Only include values where filtering field equals Yes
continue;
}
unset( $values['options'][$id] );
}
}
return $values;
}
Show admin all options in a filtered dynamic field
If you are filtering a dynamic field based on user id or user role, this code will display all field options for administrators.
add_filter('frm_setup_new_fields_vars', 'do_not_restrict_admin', 20, 2);
function do_not_restrict_admin( $values, $field ) {
if ( $values['restrict'] && current_user_can('administrator') ) {
if ( $values['type'] == 'data' && in_array( $values['data_type'], array( 'select', 'radio', 'checkbox' ) ) && is_numeric( $values['form_select'] ) ) {
$values['restrict'] = 0;
$values['options'] = FrmProDynamicFieldsController::get_independent_options($values, $field);
}
}
return $values;
}
Display two fields in a dynamic field
Use this code to display more than one field in a dynamic field. You must select the first field you would like to display in your dynamic field options. The second field is added with this custom code. Please note, the second field must come from the same form as the first field.
add_filter('frm_setup_new_fields_vars', 'customize_dfe', 25, 2);
function customize_dfe( $values, $field ) {
if ( $field->id == 125 && !empty( $values['options'] ) ){//Replace 125 with the ID of your dynamic field
$temp_values = $values;
$temp_values['form_select'] = 30; //change 30 to the id of the second field you want to show in your dynamic field
$field2_opts = FrmProDynamicFieldsController::get_independent_options( $temp_values, $field );
foreach ( $values['options'] as $id => $v ) {
$values['options'][ $id ] .= ' '. $field2_opts[ $id ];
}
}
return $values;
}
Change the blank option label in Dynamic field
By default, there is a blank option in a Dynamic field dropdown. You can change the label on that option with the code below.
add_filter('frm_setup_new_fields_vars', 'change_dynamic_blank_option', 25, 2);
function change_dynamic_blank_option($values, $field){
if ( in_array( $field->id, array( 100,101,102 ) ) ) {//Replace 100, 101, and 102 with the IDs of your Dynamic fields
if ( empty( $values['options'] ) ) {
return $values;
}
$values['options'][''] = 'Select an option';
}
return $values;
}
Order field options
This code can be used to change the order the options appear in a drop-down, checkbox, or radio button field.
add_filter('frm_setup_new_fields_vars', 'frm_reorder_options', 30, 2);
function frm_reorder_options($values, $field){
if ( $field->id == 125 ) {//Replace 125 with the ID of your field
asort($values['options']); // sort the values here
}
return $values;
}
Add user dropdown
Use the code below to populate a dropdown, radio button, or checkbox field with all users of a specific role. This field will store the user's ID number, but it will display the user's display name in the field options. Make sure you select 'Use separate values' in the field options before adding this code. Change 'subscriber' to any user role and change 485 to the ID of the field you want to populate.
add_filter('frm_setup_new_fields_vars', 'frm_populate_user_dropdown', 20, 2);
add_filter('frm_setup_edit_fields_vars', 'frm_populate_user_dropdown', 20, 2);
function frm_populate_user_dropdown( $values, $field ){
if ( $field->id == 485 ) {
$role = 'subscriber';
$users = get_users( array( 'role' => $role ) );
$values['options'] = array( );
foreach ( $users as $user ) {
$values['options'][] = array(
'label' => $user->display_name,
'value' => $user->ID
);
}
}
return $values;
}
If you would like to display something other than the display name, you can change "display_name" in the code above to any user meta. If you would like to display two fields like first name and last name, change that line to the following:
'label' => $user->first_name .' '. $user->last_name,
Make field read-only for non-administrators
Change 554, 555, and 556 to the IDs of the fields that you want to make conditionally read-only.
add_filter('frm_setup_new_fields_vars', 'frm_set_read_only_on_create', 9, 2);
add_filter('frm_setup_edit_fields_vars', 'frm_set_read_only_on_create', 9, 2);
function frm_set_read_only_on_create( $values, $field ){
// If on the back-end, keep fields editable
if ( FrmAppHelper::is_admin() || current_user_can( 'administrator' ) ) {
return $values;
}
// If on front-end, make specific fields read-only
if ( in_array( $field->id, array( 554,555,556 ) ) ) {
$values['read_only'] = 1;
}
return $values;
}
Populate a field with categories
Use this code to populate a dropdown, checkbox, or radio field with post categories.
add_filter('frm_setup_new_fields_vars', 'frm_populate_categories', 20, 2);
add_filter('frm_setup_edit_fields_vars', 'frm_populate_categories', 20, 2);
function frm_populate_categories( $values, $field ) {
if ( $field->id == 125 ) { //replace 125 with the ID of the field to populate
// Adjust your category aruments as needed
$category_args = array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => false,
'exclude' => array(),
'type' => 'post',
'taxonomy' => 'category',
);
$categories = get_categories( $category_args );
unset( $values['options'] );
$values['options'] = array( '' ); //remove this line if you are using a checkbox or radio field
foreach( $categories as $category ){
$values['options'][ $category->term_id ] = $category->name;
}
$values['use_key'] = true; //this will set the field to save the category ID
}
return $values;
}
Auto populate multiple rows in a repeater
Automatically populate multiple rows in a repeating section. Please note that this will only apply to brand new entries. Replace 24094 with the ID of your repeating section. Replace 1647 with the ID of the child form linked to the repeating section. You can find the ID of this form by looking in your frm_forms database table and searching for forms with a parent ID that matches your form. Replace 123 and 124 with field IDs inside of your repeating section.
add_filter('frm_setup_new_fields_vars', 'frm_auto_populate_repeating_fields', 20, 2);
function frm_auto_populate_repeating_fields( $values, $field ) {
if ( $field->id == 24094 ) {//Replace with the ID of your field
if ( ! isset( $_POST['item_meta'][ $field->id ] ) ) {
$first_field = 123;
$second_field = 124;
$values[ 'value' ] = array(
'form' => '1647',
'row_ids' => array( 0, 1, 2 ),
0 => array( 0 => '', $first_field => 'Row 1 value', $second_field => '1' ),
1 => array( 0 => '', $first_field => 'Row 2 value', $second_field => '2' ),
2 => array( 0 => '', $first_field => 'Row 3 value', $second_field => '3' ),
);
}
}
return $values;
}
Remove options based on filters
Use this code example in conjunction with a View to remove used options that are filtered as you'd like. This gives you the ability to create custom and even complex filters without code.
Set up the View to return a comma-separated list of the options you want to remove. In the Content box of the View, if your field has separate values, put:
[if 577][577 show=value],[/if 577]
If your field doesn't have separate values, put:
[if 577][577],[/if 577]
where 577 is the id or key of the field whose options you want to filter. Add filters to the View as you'd like. You can filter based on creation date, field values, or whatever you like.
Clear out the No entries message box so it's blank.
In the code example, swap out 577 for the id of the field whose options you want to filter and 159 for the id of the View you created for this. Note: this code example hasn't been set up to work with Dynamic fields.
For example, you could remove options that have been selected:
- by the current user
- by anyone in the last week
- in an entry where the status field is set to "approved"
add_filter( 'frm_setup_new_fields_vars', 'frm_remove_filtered', 20, 2 );
function frm_remove_filtered( $values, $field ) {
if ( $field->id !== "577" ) {
return $values;
}
$used_string = FrmViewsDisplaysController::get_shortcode( array( 'id' => 159, 'filter' => 0 ) );
if ( ! $used_string ) {
return $values;
}
$used_string = preg_replace( '/,s+/', ',', $used_string );
$used = explode( ',', $used_string );
$used = array_filter( $used, 'strlen' );
$used = array_unique( $used );
if ( is_array( $values['options'][0] ) ) {
$values['options'] = array_filter( $values['options'], function ( $option ) use ( $used ) {
if ( in_array( $option['value'], $used ) ) {
return false;
}
return true;
} );
} else {
$values['options'] = array_diff( $values['options'], $used );
}
return $values;
}
Populate a field with WordPress Tags
Use this code to populate a dropdown, checkbox, or radio field with post tags.
add_filter('frm_setup_new_fields_vars', 'frm_populate_tags', 20, 2);
add_filter('frm_setup_edit_fields_vars', 'frm_populate_tags', 20, 2);
function frm_populate_tags( $values, $field ) {
if ( $field->id == 125 ) { //replace 125 with the ID of the field to populate
// Adjust your tag aruments as needed
$tags_args = array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => false,
);
$tags = get_tags( $tags_args );
unset( $values['options'] );
$values['options'] = array( '' ); //remove this line if you are using a checkbox or radio field
foreach( $tags as $tag ){
$values['options'][ $tag->term_id ] = $tag->name;
}
$values['use_key'] = true; //this will set the field to save the category ID
}
return $values;
}
Randomize order of field options
This code can be used to randomize the order that options are shown in a dropdown, radio button, or checkbox field.
add_filter('frm_setup_new_fields_vars', 'frm_reorder_options', 30, 2);
function frm_reorder_options($values, $field){
if ( $field->id == 13676 ) {//Replace 13676 with the ID of your field
shuffle($values['options']); // sort the values here
}
return $values;
}
Remove a field option based on current date
If you want to remove a value from a checkbox or radio button field after a certain date, use the example below.
add_filter('frm_setup_new_fields_vars', 'remove_field_option_by_date', 30, 2);
add_filter('frm_setup_edit_fields_vars', 'remove_field_option_by_date', 30, 2);
function remove_field_option_by_date( $values, $field ) {
$today = time();
$close_date = strtotime ( "2020-06-20" ); // change 2020-06-20 to the date after which the option should be removed
if ($field->id == 13677 && $today > $close_date ) { // change 13677 to your field id
$options_to_remove = array( 'Option 1' ); // change Option 1 to the value to remove
foreach ( $options_to_remove as $remove ) {
$option_key = array_search( $remove, $values['options'] );
if ( $option_key !== false ) {
unset( $values['options'][ $option_key ] );
}
}
}
return $values;
}
Filter Dynamic field by user meta
Use this code to filter the options in a Dynamic field by the value of a user meta field of the current user. You can use this to show the user only people from his/her department, special interest group, or state, for example.
If the user isn't logged in or the user doesn't have a value for the user meta you're using for the filter, the Dynamic field won't display any options.
This example will make it so an option will only be included if the field in the linked form has the same value as the current user's hobby.
add_filter( 'frm_setup_new_fields_vars', 'filter_dfe_options_by_user_meta', 25, 2 );
add_filter( 'frm_setup_edit_fields_vars', 'filter_dfe_options_by_user_meta', 25, 2 );
function filter_dfe_options_by_user_meta( $values, $field ) {
if ( $field->id == 11483 && ! empty( $values['options'] ) ) {//Replace 11483 with the ID of your Dynamic field
$temp = $values;
$temp['form_select'] = 11480;//change 11480 to the id of the field (in linked form) that you want to filter by
$field2_opts = FrmProDynamicFieldsController::get_independent_options( $temp, $field );
$user_id = get_current_user_id();
if ( ! $user_id ) {
$values['options'] = array();
return $values;
}
$user_meta = get_user_meta( $user_id, 'hobby', true );//replace 'hobby' with the key of the user meta you want to filter by
if ( ! $user_meta ) {
$values['options'] = array();
return $values;
}
foreach ( $values['options'] as $id => $v ) {
if ( isset( $field2_opts[ $id ] ) && ( $v == '' || $field2_opts[ $id ] == $user_meta ) ) {//Only include values where filtering field equals Yes
continue;
}
unset( $values['options'][ $id ] );
}
}
return $values;
}
Change:
- 11483 to the id of the Dynamic field
- 11480 to the id of the field in the data form that you want to use for filtering
- hobby to the key of the user meta you want to use for filtering
Translate country dropdowns with Polylang
Use this code to convert the English country names to region locales using an active Polylang language. This example requires that you have Separate Values enabled for your Country list dropdown field. It will change the Option Labels based on the current label value. Saved values will be as is and used for saving.
add_filter( 'frm_setup_new_fields_vars', 'translate_countries', 20, 2 );
add_filter( 'frm_setup_edit_fields_vars', 'translate_countries', 20, 2 );
function translate_countries( $values, $field ) {
$field_id = 4865; // change 4865 with the ID of your Country list dropdown field.
if ( $field_id !== (int) $field->id ) {
return $values;
}
$language = pll_current_language();
array_walk(
$values['options'],
function( &$option ) use ( $language ) {
if ( ! is_array( $option ) ) {
return;
}
$code = get_country_code_from_country_name( $option['label'] );
if ( false === $code ) {
return;
}
$option['label'] = Locale::getDisplayRegion( $code, $language );
}
);
return $values;
}
function get_country_code_from_country_name( $country_name ) {
$map = array(
'Albania' => 'sq_AL',
'Algeria' => 'ar_DZ',
'Argentina' => 'es_AR',
'Australia' => 'en_AU',
'Austria' => 'de_AT',
'Bahrain' => 'ar_BH',
'Bangladesh' => 'bn_BD',
'Belarus' => 'be_BY',
'Belgium' => 'nl_BE',
'Bolivia' => 'es_BO',
'Bosnia and Herzegovina' => 'sr_BA',
'Brazil' => 'pt_BR',
'Bulgaria' => 'bg_BG',
'Canada' => 'en_CA',
'Chile' => 'es_CL',
'China' => 'zh_CN',
'Colombia' => 'es_CO',
'Costa Rica' => 'es_CR',
'Croatia' => 'hr_HR',
'Cyprus' => 'el_CY',
'Czech Republic' => 'cs_CZ',
'Denmark' => 'da_DK',
'Dominican Republic' => 'es_DO',
'Ecuador' => 'es_EC',
'Egypt' => 'ar_EG',
'El Salvador' => 'es_SV',
'Estonia' => 'et_EE',
'Finland' => 'fi_FI',
'France' => 'fr_FR',
'Germany' => 'de_DE',
'Greece' => 'el_GR',
'Guatemala' => 'es_GT',
'Honduras' => 'es_HN',
'Hong Kong' => 'zh_HK',
'Hungary' => 'hu_HU',
'Iceland' => 'is_IS',
'India' => 'hi_IN',
'Indonesia' => 'in_ID',
'Iraq' => 'ar_IQ',
'Ireland' => 'en_IE',
'Israel' => 'iw_IL',
'Italy' => 'it_IT',
'Japan' => 'ja_JP',
'Jordan' => 'ar_JO',
'Kuwait' => 'ar_KW',
'Latvia' => 'lv_LV',
'Lebanon' => 'ar_LB',
'Libya' => 'ar_LY',
'Lithuania' => 'lt_LT',
'Luxembourg' => 'de_LU',
'Macedonia' => 'mk_MK',
'Malaysia' => 'ms_MY',
'Malta' => 'mt_MT',
'Mexico' => 'es_MX',
'Montenegro' => 'sr_ME',
'Morocco' => 'ar_MA',
'Netherlands' => 'nl_NL',
'New Zealand' => 'en_NZ',
'Nicaragua' => 'es_NI',
'Norway' => 'no_NO',
'Oman' => 'ar_OM',
'Panama' => 'es_PA',
'Paraguay' => 'es_PY',
'Peru' => 'es_PE',
'Philippines' => 'en_PH',
'Poland' => 'pl_PL',
'Portugal' => 'pt_PT',
'Puerto Rico' => 'es_PR',
'Qatar' => 'ar_QA',
'Romania' => 'ro_RO',
'Russia' => 'ru_RU',
'Saudi Arabia' => 'ar_SA',
'Serbia and Montenegro' => 'sr_CS',
'Serbia' => 'sr_RS',
'Singapore' => 'zh_SG',
'Slovakia' => 'sk_SK',
'Slovenia' => 'sl_SI',
'South Africa' => 'en_ZA',
'South Korea' => 'ko_KR',
'Spain' => 'es_ES',
'Sudan' => 'ar_SD',
'Sweden' => 'sv_SE',
'Switzerland' => 'it_CH',
'Syria' => 'ar_SY',
'Taiwan' => 'zh_TW',
'Thailand' => 'th_TH',
'Tunisia' => 'ar_TN',
'Turkey' => 'tr_TR',
'Ukraine' => 'uk_UA',
'United Arab Emirates' => 'ar_AE',
'United Kingdom' => 'en_GB',
'United States' => 'en_US',
'Uruguay' => 'es_UY',
'Venezuela' => 'es_VE',
'Vietnam' => 'vi_VN',
'Yemen' => 'ar_YE',
);
return isset ( $map[ $country_name ] ) ? $map[ $country_name ] : false;
}
This snippet uses the active Polylang language because it calls pll_current_language, a Polylang function. If you don't use Polylang, you can specify any language there instead of pll_current_lang.
Replace product field options
Use this code to change the options in a product field.
add_filter( 'frm_setup_new_fields_vars', 'frm_populate_posts', 20, 2 );
add_filter( 'frm_setup_edit_fields_vars', 'frm_populate_posts', 20, 2 );
function frm_populate_posts( $values, $field ) {
$target_field_id = 278; // Replace 278 with the ID of the product field.
if ( (int) $field->id !== $target_field_id ) {
return $values;
}
$values['options'] = array(
0 => array( 'label' => '', 'price' => '', 'value' => '', 'user_def' => true ),
1 => array( 'label' => 'My first product', 'price' => '2.99', 'value' => 'My first product', 'user_def' => true ),
2 => array( 'label' => 'My second product', 'price' => '3.99', 'value' => 'My second product', 'user_def' => true ),
//Replace My first product and My second product with the product names you want in the field. Replace 2.99 and 3.99 with the price you want in the field for each product
);
return $values;
}
Replace field label
Use this code example to dynamically change the label of the field.
add_filter( 'frm_setup_new_fields_vars', 'change_field_name', 10, 2 );
add_filter( 'frm_setup_edit_fields_vars', 'change_field_name', 10, 2 );
function change_field_name( $values, $field ) {
$target_field_id = 22160; // Replace 22160 with the ID of your field.
if ( $target_field_id !== (int) $field->id ) {
return $values;
}
if ( FrmAppHelper::is_admin_page() && ! FrmAppHelper::is_preview_page() ) {
return $values;
}
$values['name'] = 'My dynamic field name'; // Replace My dynamic field name with your preferred field label
return $values;
}