Using Gravity Forms ‘gform_after_save_form’ PHP action

The gform_after_save_form action hook in Gravity Forms is used to perform specific actions immediately after a form is created or updated.

Usage

Here’s a basic example of using this action:

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

function my_custom_function( $form, $is_new ) {
    // your custom code here
    return $form;
}

Parameters

  • $form (Form Object): This represents the current form.
  • $is_new (bool): This variable is true if a new form is being created, and false if an existing form is being updated.

More information

For additional details, refer to the Gravity Forms Docs: gform_after_save_form

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

Examples

Logging form creation or update

In this case, an entry is added to a log file 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 );
}

Adding default fields on form creation

This example illustrates how to add 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 );
    }
}

Setting default notification properties

This example demonstrates how you can set 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 );
    }
}