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 manipulate the values in an entry prior to it’s creation.
Usage
add_filter('frm_pre_create_entry', 'adjust_my_field'); function adjust_my_field($values)
Parameters
- $values (array)
Examples
Prepend a value
This example allows you to prepend a static message before the user's field entry. For example, if the user entered '1234' and you wanted the value to be saved as 'ss-001234', you could use the code below to accomplish this.
add_filter('frm_pre_create_entry', 'adjust_my_field');
function adjust_my_field($values){
if ( $values['form_id'] == 5 ) { //change 5 to your form id
$current_value = $values['item_meta'][25]; // change 25 to to id of your field
if ( strpos( $current_value, 'ss-00' ) === false ) { // check if the value is already included
$values['item_meta'][25] = 'ss-00' . $values['item_meta'][25];
}
}
return $values;
}
Import file from URL
This can come in handy in a few different cases. First, when using the API, you can accept a file url, and import it onto your site. Second, if you would like to accept links to files that are already online, rather then requiring users to download and then upload an image.
This example uses two different fields: one for the file url, and one for the upload field. The value in the url field will be used to fill the upload field when possible.
add_filter('frm_pre_create_entry', 'frm_upload_from_url');
function frm_upload_from_url( $values ) {
if ( $values['form_id'] == 5 ) { //change 5 to your form id
$upload_field_id = 25; // replace 25 with the id of your upload field
$url_field = 24; // replace 24 with the id of your url field
$has_url = isset( $values['item_meta'][ $url_field ] ) && ! empty( $values['item_meta'][ $url_field ] );
$has_upload = isset( $values['item_meta'][ $upload_field_id ] ) && ! empty( $values['item_meta'][ $upload_field_id ] );
if ( $has_url && ! $has_upload ) {
$_REQUEST['csv_files'] = 1;
$field = FrmField::getOne( $upload_field_id );
$values['item_meta'][ $upload_field_id ] = FrmProFileImport::import_attachment( $values['item_meta'][ $url_field ], $field );
}
}
return $values;
}
Custom entry name
Since a name field will now be used in an entry name, you can use this code example to choose which field gets used for the entry name.
add_filter('frm_pre_create_entry', 'frm_choose_field_entry_name');
function frm_choose_field_entry_name( $values ) {
$target_form_id = 781; // change 781 to your form ID
if ( $target_form_id === (int) $values['form_id'] ) {
$target_field_id = 4529; // change 4529 to the field that you want to use for the entry name.
$values['item_name'] = $values['item_meta'][ $target_field_id ];
}
return $values;
}
Save entry without authoring user ID
Use this code snippet to set the authoring user ID to 0 for every new entry in form 239. This allows you to make an entry anonymous while a user is logged in.
add_filter('frm_pre_create_entry', 'save_entry_without_userID');
function save_entry_without_userID( $values ) {
$target_form_id = 239; //Replace 239 with your form ID
if ( $target_form_id === (int) $values['form_id'] ) {
$values['frm_user_id'] = 0;
}
return $values;
}
Replace less than operator reference
Use this code snippet to convert every <; to a < on create and update for a target field. Replace 7173 with your target field ID.
add_filter( 'frm_pre_create_entry', 'convert_encoded_lt_back' );
add_filter( 'frm_pre_update_entry', 'convert_encoded_lt_back' );
function convert_encoded_lt_back( $values ) {
$target_field_id = 7173; //Change 7173 to the ID of your target field.
if ( ! empty( $values['item_meta'][ $target_field_id ] ) ) {
$values['item_meta'][ $target_field_id ] = str_replace(
'<;',
'<',
$values['item_meta'][ $target_field_id ]
);
$_POST['item_meta'][ $target_field_id ] = $values['item_meta'][ $target_field_id ];
}
return $values;
}