Adding Gravity Forms Entries from Code and Firing Notifications
Sometimes data reaches WordPress through an API, an import or a custom form rather than a Gravity Form. You can still store it as a proper Gravity Forms entry, so it appears in the admin and triggers the usual notifications.
The code
Build the entry array with field IDs as keys, add it with GFAPI::add_entry, then send the form’s notifications for the new entry:
<?php
// Manually create entries and send notifications with Gravity Forms Programatically
$form_id = 10;
// add entry
$entry = array(
"form_id" => $form_id,
"1" => "Entry for field ID 1",
"2" => "Entry for field ID 2"
);
$entry_id = GFAPI::add_entry($entry);
// send notifications
function send_notifications($form_id, $entry_id){
// Get the array info for our forms and entries
// that we need to send notifications for
$form = RGFormsModel::get_form_meta($form_id);
$entry = RGFormsModel::get_lead($entry_id);
// Loop through all the notifications for the
// form so we know which ones to send
$notification_ids = array();
foreach($form['notifications'] as $id => $info){
array_push($notification_ids, $id);
}
// Send the notifications
GFCommon::send_notifications($notification_ids, $form, $entry);
}
send_notifications($form_id, $entry_id);
?>
Validate and sanitise incoming data before saving it, exactly as you would for a public form.
Stuck on something similar? Our PHP development team is happy to take a look.