gform_after_save_form

Description

Use this action hook to perform actions right after a form is created or updated.

Usage

add_action( 'gform_after_save_form', 'log_form_save', 10, 2 );

Parameters

  • $form Form Object
    Current form.
  • $is_new bool
    True if this is a new form being created. False if this is an existing form being updated.

Examples

1. Log form creation/update

This example adds a log file entry when a form is updated or created.

add_action( 'gform_after_save_form', 'log_form_saved', 10, 2 );

function log_form_saved( $form, $is_new ) {
    $log_file = ABSPATH . '/gf_saved_forms.log';
    $f = fopen( $log_file, 'a' );
    $user = wp_get_current_user();

    if ( $is_new ) {
        fwrite( $f, date( 'c' ) . " - Form created by {$user->user_login}. Form ID: {$form['id']}. \n" );
    } else {
        fwrite( $f, date( 'c' ) . " - Form updated by {$user->user_login}. Form ID: {$form['id']}. \n" );
    }

    fclose( $f );
}

2. Add default fields on form creation

This example shows how you can add some default fields to the form when creating new forms.

add_action( 'gform_after_save_form', 'add_default_fields', 10, 2 );

function add_default_fields( $form, $is_new ) {
    if ( $is_new ) {
        $form['fields'] = array(
            array(
                'type'         => 'hidden',
                'label'        => 'First Name',
                'id'           => 1,
                'defaultValue' => '{user:first_name}',
                'formId'       => $form['id'],
            ),
            array(
                'type'         => 'hidden',
                'label'        => 'Email',
                'id'           => 2,
                'defaultValue' => '{user:user_email}',
                'formId'       => $form['id'],
            ),
        );
        GFAPI::update_form( $form );
    }
}

3. Set default notification properties

This example shows how you can set the default notification properties when creating new forms.

add_action( 'gform_after_save_form', 'set_default_notification_to', 10, 2 );

function set_default_notification_to( $form, $is_new ) {
    if ( $is_new ) {
        foreach ( $form['notifications'] as &$notification ) {
            $notification['to'] = '[email protected]';
        }

        $form['is_active'] = '1';
        GFAPI::update_form( $form );
    }
}

Source Code

This action hook is located in GFFormDetail::save_form_info() in form_detail.php.