Using Gravity Forms ‘gform_pre_delete_feeds’ PHP action

The gform_pre_delete_feeds action is a hook in Gravity Forms that allows you to run custom code before feeds are deleted.

It can be useful in various scenarios, such as preventing specific feeds from being deleted or performing custom actions before the deletion.

Usage

To use the gform_pre_delete_feeds action, add the following code to your theme’s functions.php file or a custom plugin:

add_action( 'gform_pre_delete_feeds', 'your_function_name', 10, 2 );
function my_custom_function() {
// Your custom code here
}

In this example, ‘my_custom_function’ is the name of the function that you want to run when the ‘gform_print_scripts’ action is triggered.

Parameters

The gform_pre_delete_feeds action provides two parameters:

  1. $form_id (integer): The ID of the form whose feeds are being deleted.
  2. $feed_ids (array): An array of feed IDs that are about to be deleted.

Examples

Below are five practical scenarios where the gform_pre_delete_feeds action can be helpful.

In each of these scenarios, the custom function associated with the gform_pre_delete_feeds action is executed before the feed deletion, allowing you to perform various tasks like preventing specific feeds from being deleted, logging feed deletions, sending email notifications, removing related data, or exporting feed data. By using this action, you can enhance the functionality of your Gravity Forms application and make it more tailored to your needs.

Preventing specific feeds from being deleted

You might want to protect specific feeds from being deleted accidentally.

add_action( 'gform_pre_delete_feeds', 'prevent_specific_feeds_deletion', 10, 2 );

function prevent_specific_feeds_deletion( $form_id, $feed_ids ) {
// Array of protected feed IDs
$protected_feeds = array( 1, 5, 10 );

// Remove protected feeds from the feed IDs array
$feed_ids = array_diff( $feed_ids, $protected_feeds );

// Return the filtered array of feed IDs
return $feed_ids;
}

Logging feed deletions

You may want to keep a log of feed deletions for auditing purposes.

add_action( 'gform_pre_delete_feeds', 'log_feed_deletions', 10, 2 );

function log_feed_deletions( $form_id, $feed_ids ) {
// Loop through each feed ID
foreach ( $feed_ids as $feed_id ) {
// Log the deletion in a custom log file
error_log( "Feed ID {$feed_id} from Form ID {$form_id} was deleted.", 3, "/path/to/your/logfile.log" );
}
}

Sending email notifications

Notify the admin via email when a feed is deleted.

add_action( 'gform_pre_delete_feeds', 'email_notification_on_feed_deletion', 10, 2 );

function email_notification_on_feed_deletion( $form_id, $feed_ids ) {
$to = '[email protected]';
$subject = 'Feed Deleted';
// Loop through each feed ID
foreach ( $feed_ids as $feed_id ) {
// Create email content
$message = "Feed ID {$feed_id} from Form ID {$form_id} was deleted.";

// Send email notification
wp_mail( $to, $subject, $message );
}
}

If you have custom data related to the feeds, you might want to delete this data before deleting the feeds themselves.

add_action( 'gform_pre_delete_feeds', 'remove_related_data', 10, 2 );

function remove_related_data( $form_id, $feed_ids ) {
// Loop through each feed ID
foreach ( $feed_ids as $feed_id ) {
// Your function to delete the related data
delete_related_data( $form_id, $feed_id );
}
}

Exporting feed data

You may want to export feed data before deletion as a backup.

add_action( 'gform_pre_delete_feeds', 'export_feed_data', 10, 2 );

function export_feed_data( $form_id, $feed_ids ) {
// Loop through each feed ID
foreach ( $feed_ids as $feed_id ) {
// Your function to export the feed data
export_feed_data_to_csv( $form_id, $feed_id );
}
}