The gform_post_form_trashed action is triggered after a form is moved to trash in Gravity Forms, allowing further actions to be performed.
Usage
add_action('gform_post_form_trashed', 'my_function', 10, 1);
Parameters
- $form_id (int): The ID of the form being moved to trash.
More information
See Gravity Forms Docs: gform_post_form_trashed
This action hook is located in forms_model.php.
Examples
Send an email notification when a form is trashed
function send_trashed_form_email($form_id) {
// Replace with your email address
$to = '[email protected]';
$subject = 'Form Trashed';
$message = "A form with ID {$form_id} has been moved to trash.";
wp_mail($to, $subject, $message);
}
add_action('gform_post_form_trashed', 'send_trashed_form_email', 10, 1);
Log trashed form details in a custom table
function log_trashed_form($form_id) {
global $wpdb;
$table_name = $wpdb->prefix . 'trashed_forms';
$wpdb->insert($table_name, array('form_id' => $form_id, 'trashed_at' => current_time('mysql')));
}
add_action('gform_post_form_trashed', 'log_trashed_form', 10, 1);
Automatically empty trash after a form is trashed
function empty_trash_after_form_trashed($form_id) {
GFAPI::delete_forms(array($form_id), true);
}
add_action('gform_post_form_trashed', 'empty_trash_after_form_trashed', 10, 1);
Update a custom post type metadata when a form is trashed
function update_custom_post_on_form_trashed($form_id) {
$args = array(
'post_type' => 'your_custom_post_type',
'meta_query' => array(
array(
'key' => 'form_id',
'value' => $form_id,
'compare' => '='
)
)
);
$query = new WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
update_post_meta(get_the_ID(), 'form_trashed', true);
}
}
wp_reset_postdata();
}
add_action('gform_post_form_trashed', 'update_custom_post_on_form_trashed', 10, 1);
Redirect user after a form is trashed
function redirect_after_form_trashed($form_id) {
wp_redirect(home_url('/form-trashed'));
exit;
}
add_action('gform_post_form_trashed', 'redirect_after_form_trashed', 10, 1);