Gravity Forms – How to apply default settings for new forms

The following PHP code shows how to apply default settings to new forms in Gravity Forms.

To use this code you will need to have an understanding of implementing PHP code and the ability to access and understand settings stored in the WordPress database.

The example shows how to make the ‘Require user to be logged in’ setting enabled by default for all newly created forms — but you can add your own settings by creating a new form, configuring as you require, going into the WordPress database and inspecting the rg_form_meta table and settings applied to your new form.

Once you apply the settings to this script you can delete this form.

<?php
/*
 *   Runs when form is created - applies default form settings by running
 *   SQL query to update a forms settings.
 *   NOTE - Currently only sets 'Require user to be logged in' by default when form is created.
 */
if (!class_exists('Default_GF_Form_Settings')) {
    class Default_GF_Form_Settings
    {
        /*
         * Construct the plugin object
         */
        function __construct()
        {        
            // register actions
            add_action('gform_after_save_form', array(&$this,'gf_custom_default_form_settings'), 10, 2);
        } // END public function __construct

        /*
         * Main function -runs SQL update statement, adding default settings to forms 'display_meta' table
         *  Currently only sets 'Require user to be logged in' by default when form is created.
         */        
        public function gf_custom_default_form_settings($form, $is_new)
        {
            if ($is_new) {   // only run when form is being created - is_new
                global $wpdb;
                $form_meta_table = $wpdb->prefix . "rg_form_meta";
                $form_id         = $form["id"];
                
                $result = $wpdb->query( $wpdb->prepare( "UPDATE $form_meta_table SET display_meta = replace(display_meta, '\"id\":%d}','\"id\":%d,\"requireLogin\":\"1\"}') WHERE form_id = %d", $form_id, $form_id, $form_id ) );
            }
        }  // END gf_custom_default_form_settings

    }
    $Default_GF_Form_Settings = new Default_GF_Form_Settings();
}

Was this article helpful?

Related Articles