Using Gravity Forms ‘gform_post_form_duplicated’ PHP action

The gform_post_form_duplicated action in Gravity Forms is triggered after a form is duplicated, allowing further actions to be performed. The hook provides the ID of the form duplicated and the ID of the new form created.

Table of contents

Usage

add_action('gform_post_form_duplicated', 'my_function', 10, 2);

Parameters

  • $form_id (int): The ID of the form being duplicated.
  • $new_id (int): The ID of the newly created, duplicate form.

More information

See Gravity Forms Docs: gform_post_form_duplicated This hook replaces the deprecated “gform_after_duplicate_form” hook. Source code location: GFFormsModel::duplicate_form() in forms_model.php.

Examples

Send an email notification when a form is duplicated

function send_email_on_form_duplication($form_id, $new_id) {
    $subject = 'A form has been duplicated';
    $message = "Form with ID: {$form_id} has been duplicated. New form ID is: {$new_id}.";
    $to = '[email protected]';
    wp_mail($to, $subject, $message);
}
add_action('gform_post_form_duplicated', 'send_email_on_form_duplication', 10, 2);

Update a custom table with duplicate form information

function update_custom_table_on_duplication($form_id, $new_id) {
    global $wpdb;
    $table_name = $wpdb->prefix . 'your_custom_table';
    $data = array('form_id' => $new_id, 'duplicated_from' => $form_id);
    $wpdb->insert($table_name, $data);
}
add_action('gform_post_form_duplicated', 'update_custom_table_on_duplication', 10, 2);

Log form duplication to a custom file

function log_form_duplication($form_id, $new_id) {
    $logfile = 'gravity_forms_duplication.log';
    $log_entry = date('Y-m-d H:i:s') . " - Form ID: {$form_id} duplicated to form ID: {$new_id}" . PHP_EOL;
    file_put_contents($logfile, $log_entry, FILE_APPEND | LOCK_EX);
}
add_action('gform_post_form_duplicated', 'log_form_duplication', 10, 2);

Change form title after duplication

function change_form_title_on_duplication($form_id, $new_id) {
    $form = GFAPI::get_form($new_id);
    $form['title'] = $form['title'] . ' (Duplicated)';
    GFAPI::update_form($form);
}
add_action('gform_post_form_duplicated', 'change_form_title_on_duplication', 10, 2);

Add a prefix to all field labels in the duplicated form

function prefix_field_labels_on_duplication($form_id, $new_id) {
    $form = GFAPI::get_form($new_id);
    foreach ($form['fields'] as &$field) {
        $field->label = 'Dup: ' . $field->label;
    }
    GFAPI::update_form($form);
}
add_action('gform_post_form_duplicated', 'prefix_field_labels_on_duplication', 10, 2);

Leave a Comment

Your email address will not be published. Required fields are marked *