Developer Hooks
GravityWP Field to Entries has PHP filters and an action for developers who want to change which entries a feed creates and what happens after. You do not need them to use the add-on; all settings are in the feed settings of the form.
Add the code to the functions.php file of your (child) theme or to a code snippets plugin.
gravitywp/fieldtoentries/source_values
Changes the list of items a feed turns into entries, before any entry is created. The feed creates one entry for every item in the returned array.
add_filter( 'gravitywp/fieldtoentries/source_values', function ( $values, $source_field ) {
return $values;
}, 10, 2 );
| Parameter | Description |
|---|---|
$values | The items. Checkboxes and Multi Select: the selected choice values. List: the rows; each row of a multi-column List field is an array with the column labels as keys. Single Line Text: the parts after splitting on the Text Field Separator. Radio Buttons and Drop Down: one item with the selected value. |
$source_field | The source field object (GF_Field). |
Example: remove spaces around the items of a text field and skip empty items.
add_filter( 'gravitywp/fieldtoentries/source_values', function ( $values, $source_field ) {
if ( $source_field->type === 'text' ) {
$values = array_filter( array_map( 'trim', $values ), 'strlen' );
}
return $values;
}, 10, 2 );
gravitywp/fieldtoentries/new_entry
Changes a new entry just before it is saved in the target form. Return an empty array to skip this entry.
add_filter( 'gravitywp/fieldtoentries/new_entry', function ( $new_entry, $entry, $form, $target_form, $add_on ) {
return $new_entry;
}, 10, 5 );
| Parameter | Description |
|---|---|
$new_entry | The new entry: target field IDs as keys, plus form_id. |
$entry | The source entry. |
$form | The source form. |
$target_form | The target form. |
$add_on | The Field to Entries add-on instance. |
Example: do not create an entry when field 3 of the new entry in target form 8 is “None”.
add_filter( 'gravitywp/fieldtoentries/new_entry', function ( $new_entry, $entry, $form, $target_form, $add_on ) {
if ( (int) $target_form['id'] === 8 && rgar( $new_entry, '3' ) === 'None' ) {
return array();
}
return $new_entry;
}, 10, 5 );
gravitywp/fieldtoentries/after_add_entry
Runs after each new entry is saved in the target form.
add_action( 'gravitywp/fieldtoentries/after_add_entry', function ( $new_entry, $target_form, $feed ) {
}, 10, 3 );
| Parameter | Description |
|---|---|
$new_entry | The new entry as it was saved, with its ID in id. |
$target_form | The target form. |
$feed | The Field to Entries feed that created the entry. |
Example: send the notifications of the target form for every new entry. Field to Entries saves the entries directly, so the target form’s notifications are not sent by default.
add_action( 'gravitywp/fieldtoentries/after_add_entry', function ( $new_entry, $target_form, $feed ) {
$entry = GFAPI::get_entry( $new_entry['id'] );
if ( ! is_wp_error( $entry ) ) {
GFAPI::send_notifications( $target_form, $entry );
}
}, 10, 3 );
gravitywp/fieldtoentries/use_choice_text
Mapped Checkboxes, Radio Buttons, Drop Down and Multi Select fields give the choice value by default. Return true to give the choice text (the label) instead. This filter applies to the other mapped values, not to the ‘Field -> Entry’ value.
add_filter( 'gravitywp/fieldtoentries/use_choice_text', function ( $use_choice_text, $source_field, $entry, $add_on ) {
return $use_choice_text;
}, 10, 4 );
| Parameter | Description |
|---|---|
$use_choice_text | false by default. |
$source_field | The mapped source field object (GF_Field). |
$entry | The source entry. |
$add_on | The Field to Entries add-on instance. |
Example: use the choice labels for all mapped fields of form 5.
add_filter( 'gravitywp/fieldtoentries/use_choice_text', function ( $use_choice_text, $source_field, $entry, $add_on ) {
return (int) $source_field->formId === 5 ? true : $use_choice_text;
}, 10, 4 );