Import Gravity Forms through code in WordPress

Author - Mamta Sharma
3.10.2019
|
0 Comments
||

Import Gravity Forms through code – WordPress

Gravity form is a WordPress plugin that is basically used to build and publish the WordPress forms.

Sometimes the situation arises when we need to import Gravity Forms through code.
For instance, I needed gravity forms to be available in a child site when that (child site) will be created through code in a multisite network (refer to this post – https://blog.webnersolutions.com/multisite-network-wordpress).

So, for that, we will need the JSON file which is going to contain all the gravity Forms details that we need for those particular sites.

The process of creating that JSON file is –

1. Export all the forms that we have created in the parent site of a multisite network.
2. Keep that exported JSON file in your project’s directory(here I’m using the name gravityform.json).

Next, we are going to write a function that will read the content from the JSON file and finally import all those data in the database.

function createGravityForms(){
    global $wpdb;
    $jsonurl = plugins_url() . "/myproject/assets/gravityform.json";
    $jsondata = json_decode(trim(file_get_contents($jsonurl)),true);    
    if(count($jsondata) != 0){
     foreach($jsondata as $form) {
       $title = $form['title'];
       $count = 2;
           while (!RGFormsModel::is_unique_title($title)) {
               $title = $form_meta["title"] . "({$count})";
               $count++;
           }
           $form_id = RGFormsModel::insert_form($title);
           $form['title'] = $title;
           $form['id'] = $form_id;
           $form = GFFormsModel::trim_form_meta_values($form);
           if (isset($form['confirmations'])) {
               $form['confirmations'] = custom_set_property_as_key($form['confirmations'], 'id');
               $form['confirmations'] = GFFormsModel::trim_conditional_logic_values($form['confirmations'], $form);
               GFFormsModel::update_form_meta($form_id, $form['confirmations'], 'confirmations');
               unset($form['confirmations']);
           }
           if (isset($form['notifications'])) {
         	  	$form['notifications'] = custom_set_property_as_key($form['notifications'], 'id');
          		$form['notifications'] = GFFormsModel::trim_conditional_logic_values($form['notifications'], $form);
           	GFFormsModel::update_form_meta($form_id, $form['notifications'], 'notifications');
           	unset($form['notifications']);
       	}
       	RGFormsModel::update_form_meta($form_id, $form);
     	}
    }
    return;
}

In this above code, we are reading the JSON file that we have placed in our project’s directory previously, then looping over each of the form data placed in it.
After that, we are checking for the slug’s uniqueness in the database. If the form with that name already exists then it will name that particular form with a title+count, otherwise, it will insert the form details in the Gravity form table, then trimming the Gravity Form metadata like confirmation & notifications and hence inserting them in the metatable in the database.

Call this function, wherever you want to import the Gravity Forms in your plugin or any other WordPress hook.

IMPORTANT NOTE: The Gravity Form plugin must be installed on your site and hence activated using a valid activation key in order to use this code.

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

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