Apps, Contract, Contract generation
comments 5

How to build a contract generator with WordPress and Gravity Forms

Background and introduction

I purchased a Gravity Forms developers licence back in October 2009. It was one of the best WordPress-related purchases I’ve ever made, as the forms plugin has gone from strength to strength over the years and is now so polished, with so many useful add-ons, that it can truly convert WordPress into an app machine of sorts.

In the intervening five years, I’ve put Gravity Forms to all manner of uses, including making a number of contract and licence generation tools with it. Among other things, I’ve used it to build:

  • a website terms of use, mutual confidentiality agreement and privacy policy generator (see ubuildcontracts.com);
  • a Creative Commons licence chooser that built upon the code output of the Creative Commons licence chooser by adding government-specific elements to the code to reflect guidance in the New Zealand Government Open and Accessing Licensing framework (known as NZGOAL) (I’ve since taken this licence generator down); and
  • a generator that enables one to build a fully populated instance of a Government Model Contract for Services, with additional clauses where required (this is fully functional but not publicly available).

I’ve also used it to enable blogging readers of this site to build their one privacy policies based on the one I drafted for this site.

Over the years I’ve noticed significant interest in the Gravity Forms forums on developments that would enable this sort of thing to be done. I thought it timely, therefore, and in keeping with the theme of this blog, to explain how I’ve used Gravity Forms to build these generators.

I’ve actually used six different techniques, which you could summarise as:

  • Bespoke PHP: using bespoke PHP in an underlying template file that is used to populate gaps in that underlying template (the PHP would grab field inputs from a form submission, including conditionally, and then insert the complete populated text into a post or page; the Gravity Forms guys were incredibly helpful in providing me with sample PHP statements); this is the approach I used for ubuildcontracts.com.
  • Custom add-on: using a custom Gravity Forms post templates add-on that I paid to have developed that would do the same thing but through a new backend interface that meant I didn’t need to get my novice hands dirty with php (I didn’t distribute it and I couldn’t release it as I didn’t own the new code; it’s pretty old now and I’m sure wouldn’t work with the latest Gravity Forms release).
  • Second custom add-on: using a separate Gravity Forms post templates add-on that I paid to have developed that worked in a similar way but, instead of outputting a post or page, it would call up a Word document template, populate that template by reference to inputs in a form submission and then make the completed document available by a secret download link (this add-on allowed for conditional shortcode-type outputs and worked pretty well but was a bit buggy; unfortunately, Gravity Forms moved on by several iterations and I got too busy to ask the developer to update it).
  • Post body content template: one can use the post body content template feature that is now built into Gravity Forms to produce a post (or page) into which default content is inserted together with inputs that come from field inputs in a form submission.
  • Form submission confirmation: recently I realised that the Gravity Forms crew had made it possible to use field shortcodes in a custom form submission confirmation page. I’ve used this approach for the simple privacy policy generator on this site.
  • Gravity Forms + Zapier + WebMerge: this is the most powerful and robust combination I’ve used to date and is what powers the Government Model Contract for Services builder mentioned above.

In the remainder of this post, I’m going to provide step-by-step instructions for the fourth, fifth and sixth approaches. (The first approach can be put to one side, as it’s too labour intensive to be scalable. The second and third approaches can likewise be put to one side, as they are not publicly available and, in any event, are most likely to be past their “use by” dates.) This post assumes that you have purchased, installed and activated the Gravity Forms plugin and have some familiarity with it.

Post body content template method

Introduction

A Gravity Forms form can be set up to produce a post from each form submission. The post can be set to “Draft” or “Published” and, most importantly for present purposes, the content of the post can consist of default content together with the content of user-submitted inputs in a form submission to either fill gaps in the default content or supplement that default content.

I’m going to explain all this with a sample form that takes user inputs to produce a simple privacy policy. Before doing that, though, it might help to explain a few potential use cases for this approach and to explain a constraint of this approach.

In terms of use cases, you might want to use this approach in these kinds of scenarios:

  • You’re a lawyer, web designer or other service provider that has a standard document (e.g., a standard confidentiality agreement or a standard services contract) the completion of which requires specific user inputs. Those inputs might be provided by you on a per-client or per-matter basis (you’re the one that fills in the form on your website, which could be publicly accessible or on a secured/private page; alternatively, your whole site might be private as you might use it for this specific purpose and other private purposes) or the client him or herself might enter the data into a form on your website. You’re the one that will take the content generated upon form submission but, because you don’t want the generated posts to be public, you set the post creation option to “Draft” so that only you, as site administrator, can see the post.
  • Alternatively, you want to enable people on your website to complete a form and see a contract or similar document created before their very eyes, as a post (or a page). You don’t want the world to be able to see these posts, though, so people who complete the form need to be registered users and the posts they create need to be private, accessible only by them when they are logged in. You might also want to charge users to create these documents and/or to enable your users to amend the contracts or other documents that are generated for them. This scenario is more involved than the first in that, depending on your requirements, you may need to use one or more of a membership or content permission plugin, a roles capability plugin, the Gravity Forms User Registration Add-On, the Gravity Forms PayPal Add-On (or another payment mechanism), a login redirect plugin and/or additional plugins or custom code to achieve the desired result. I won’t go into these additional requirements in this post; suffice to say, though, that it certainly is doable.

Walk-through

The following walk-through will focus on the first scenario. I’ve used a simple privacy policy for this example.

Step 1 – Create form: Create a form with the fields that will collect the information you need to complete your contract or similar document. Ensure that one of your fields is a “Post Body” field (this field is necessary to build a content template).

Gravity Forms form

In my test form, I’ve set the Post Body field to “Admin Only” visibility. This means users completing the form can’t actually see this field; only the administrator sees it in the Gravity Forms administrator area. You can access this option in the “Advanced” tab of the Post Body field. After you’ve added the fields, make sure you click the “Update Form” button.

Step 2 – Content template: Create your content template. To do this, click on your Post Body field and then click the “Create content template” option. That will cause a “Insert Merge Tag” drop-down menu and a text box to appear. To create your template, paste your contract or similar document into the text field. Then, in the places where you have gaps that need to be filled, select the relevant field from the “Insert Merge Tag” drop-down menu.

Ember 2

Step 3 – Visibility of output: Decide what you’d like your user to see once she has completed the form. Gravity Forms provides different options which you can access from the “Form Settings” > “Confirmations” for your form.

Default confirmation

If you’re asking people to fill out the form to provide you with the data you need to complete a contract or similar document, but without showing them the draft contract or document just yet, you may wish to show them a simple form submission confirmation page. In this scenario you can use a default confirmation with a message of your choice, such as: “Thank you for providing your information. We’ll compile your contract and send a draft to you shortly for review and execution.”

In addition, to avoid the posts created through the form submission from becoming public, you’ll need to make sure either that:

  • your site is password-protected; or
  • it has membership or access controls that lock down the created posts; or
  • the “Post Status” for the body field is set to “Draft”.

Redirect confirmation

By contrast, if you’d like a person who fills out the form to be taken to the completed contract or other document, then you can select the “Redirect” Confirmation Type. When you choose this option, you need to add your base URL (e.g., http://yoursiteurl.com/) in the “Redirect URL” field. You also need to select the “Pass Field Data Via Query String” option. In the text area you’ll see appear, type in: p={post_id}

Ember 3

The next step here is to set the Post Status in the Post Body field to “Published”.

(Now, when a person fills out the form, she will be taken to the completed document. Bear in mind here, though, that unless you’ve added some access controls to your site, anyone will be able to see the created post. If you don’t want that (and in many use cases you wouldn’t), you’ll need to add access controls to the created post, including (I suspect) a control that only the author of the post can see it.)

Step 4 – Add form to a page of your site: Insert your form into a page on your site (whether you want to make that page public or password protect it is up to you) and look forward to receiving submissions from the form!

Pros and cons

Here are the pros and cons of this approach:

Pros

  • The approach is comparatively quick and easy to set up.
  • It creates a semi-permanent record in the form of the created post.

Cons

  • Conditional shortcodes that can be used in other areas of Gravity Forms cannot be used in the content template.
  • You need to deal with access controls if you want only the user to be able to see the created document.

Form submission confirmation method

Introduction

The form submission confirmation method is similar to the previous method but it does not rely on post creation and it is possible to use conditional shortcodes (for an explanation of how the conditional shortcodes work, see this Gravity Forms page).

Walk-through

The main steps are these:

Step 1 – Create form: Create a form with the fields that will collect the information you need to complete your contract or similar document.

Step 2 – Create confirmation text: In the Form Settings > Confirmations area of Gravity Forms, either edit the default confirmation or create a new confirmation which has a “Confirmation Type” of “Text”. In the text box, add your default text together with the merge fields which will insert the field inputs into your default text upon form submission.  You can select the merge fields from a small drop down menu to the right of the text box:

Confirmation text

Step 3 – Add form to a page of your site: Insert your form into a page on your site (again, whether you want to make that page public or password protected is up to you).

Now, when a user completes the form, he or she will be taken to a confirmation page that displays the completed document. The user can then copy and paste the text and use it for his or her own purposes. Here’s an example of what it looks like on my site:

Document created through form submission confirmation

Pros and cons

Here are the pros and cons of this approach:

Pros

  • Comparatively quick and easy to set up.
  • Shows the completed document only to the person who submits the form. The merged output is not a permanent record (which to some might be a pro).
  • You can use conditional shortcodes in the text that you enter for your confirmation.

Cons

  • Does not create a permanent record of the merged document. (An administrator will still be able to find the field inputs in the Gravity Forms database, unless the form has been tweaked so as not to save – or delete – form entries, something that can be done with Gravity Wiz’s Gravity Perks.)

Gravity Forms + Zapier + WebMerge

Introduction

As I mentioned above, this method is the most powerful of the methods. At the same time, it is also the most involved to set up and you need to have a Zapier account and a WebMerge account. You also need to have the Gravity Forms Zapier Add-On.

For those not familiar with Zapier, it’s an excellent service that enables you to “connect the apps you use to easily move data between them”. For those not familiar with WebMerge, it’s an “online platform that allows you to easily collect data, populate a document and send it to any contact automatically”. It allows you to merge data with fillable PDFs, HTML documents created with WebMerge’s online editor, or Microsoft Word documents. Great stuff.

Walk-through

Here’s the basic process. It assumes you have a Zapier account and a WebMerge account and have installed the Gravity Forms Zapier Add-On. I’m using the scenario where you’re using form inputs to populate a Word template (rather than, say, a fillable PDF).

Step 1 – Create form: Create a form with Gravity Forms that will collect the information you need to complete your contract or similar document.

Step 2 – Create template: Create a Word document template that contains your default text together with the merge fields that will be replaced by a person’s entries in the form. Note that you can also use conditional merge fields. For information on how WebMerge approaches merge fields and what it requires, take a look at these pages on the WebMerge support site:

Before I move to Step 3, I should note that, when I created my first complex contract template, with multiple merge fields (including conditionals), it took me a while to figure out all the relevant statements to use in my template. I’ve documented what I think are the main ones below. (If I’ve missed anything, I suggest you approach WebMerge support. They’re super helpful.)

Sample statements to use in your Word template document

Populate document with content of a field:

{$Address}

Populate document with content of a field if the submitted content equals a certain value:

{if $Name == ‘John’}
{$Name} is a wonderful name.
{/if}

Populate document with some predefined text, and the content of a field, if there is any entry in the submitted field (i.e., it is not empty):

{if !empty($Context)}
Context and other text
{$Context}
{/if}

Radio buttons – yes or no options:

{if $AnyChangesToSchedule2 == ‘No’}None.{elseif $AnyChangesToSchedule2 ==’Yes’}Schedule 2 of this Contract is amended as set out below:{/if}

Checkboxes – For each checkbox item:

{if strpos($checkbox, ‘the value selected’) !== false}Content here{/if}

One other question you may have is how to remove extra whitespace around your if statements. To achieve this, you need to make sure there are no spaces around your if statements.  Here’s an example that uses a single if statement and additional content with merge field afterwards. Note how the “Description of Services” text starts immediately after the closing if tag:

{if !empty($Context)}Context

{$Context}

{/if}Description of Services

{$DescriptionServices}

Step 3 – Create a Zap: Set up a new “Zap” in Zapier. As explained on the WebMerge support site, login to your Zapier account and click Create New Zap. From there, choose GravityForms as the trigger and have that fire a WebMerge action (i.e., a new document merge):

Zapier

The Gravity Forms crew have produced a helpful guide that details the steps required at the Gravity Forms end (i.e., within your website) as part of this process. It walks you through the zap creation process from the Gravity Forms perspective. The key point is that you need to add a webhook, that Zapier will give you, to the Gravity Forms ‘Zapier’ Form Settings for the form you’ll be using. See the Gravity Forms guide referred to above for the detail. The screenshot below is of a Gravity Forms admin page (Form Settings > Zapier) that shows where this is done:

Zapier's webhook for Gravity Forms

At the Zapier end, once you select from your WebMerge account the document  that you wish to use, Zapier will load the fields in your document so you can map them to the Gravity Forms form fields (this is much easier than it may sound).

Ember

Once you’ve done this, you can test your new Zap and then name it and turn it on.

Ember

Now you’ll be ready to start producing merged documents. Fill out and submit the Gravity Forms form and WebMerge will create a merged document (contract, policy or whatever it is) for you.

There are additional settings that you can configure within your WebMerge account. They’re fairly self-explanatory so I won’t go into the detail here.

Pros and cons

Here are the pros and cons of this approach:

Pros

  • Enables you to use a fully formatted Microsoft Word template that, when merged, will retain its formatting.
  • WebMerge also enables you to create a merged PDF.
  • The ability to use conditional statements in your template is a major plus, particularly for documents of any complexity where you need to include or exclude content depending on a user’s form inputs

Cons

  • Comparatively more time-consuming to set up.
  • You need to have not only Gravity Forms but a Zapier account, a WebMerge account and the Gravity Forms Zapier Add-On, which will increase your costs.

Concluding comment

So that’s it. Three different ways to use Gravity Forms to build a contract (or other document) generator. They all have their pros and cons but, for documents of any complexity, the Gravity Forms + Zapier + WebMerge approach is the most powerful.

Let me know if you have any questions.

5 Comments

  1. I am trying to create a contract my clients can sign and submit to me but I can’t get what I need. I created a new form and inserted a Post Body field. I then clicked the Create content template option. I pasted my contract into the text field, but it’s not visible, even though I have Visibility Everyone selected. Then I tried pasting my contract into the Placeholder field, but that looks awful because it doesn’t accept formatting. I need my clients to see the text.

    What can I do to make that happen. Thank you.

    • Richard Best says

      Hi Kirsten. I’m not entirely sure how you’re trying to set things up. If you’re trying to get your contractual content to show in the form as it appears on the front end, by inserting it into the content template, that won’t work. The content template contents do not show in the form itself; rather, they’re used to generate a post once the form is submitted. You might be able to achieve what you’re after by using an HTML block in your form or by using the Gravity Wiz “GP Terms of Service” add-on; http://gravitywiz.com/documentation/gp-terms-of-service/ Another potential option is GravityPDF: https://developer.gravitypdf.com/ Or the Zapier-WebMerge discussed in the post. Hope this helps. All the best.

      P.S. When you say you want your clients to sign the contract, do you mean a handwritten signature?

  2. There’s also a seventh option: Gravity PDF, which is a free third-party add on for Gravity Forms which allows you to generate custom PDF documents using HTML and CSS. It’s a more technical than the Zapier-WebMerge option but it’s also cheaper too – you only need Gravity Forms Personal License. Plus, your user data never leaves your server so there’s a lesser chance of a data breach.

    Disclaimer: I’m the head developer over at Gravity PDF.

  3. Richard – this has been a big help. Thank you for writing it up.

    I’ve fooled around with the Zap + WM + GF version, and have found the WM Data Route option to be quite powerful when added to the workflow. It allows you to combine PDFs and dynamic word documents into a single, large PDF. With conditional logic, this can make for quite wonderful custom document packets.

  4. Richard Best says

    Hi Damjan. Thanks for leaving your comment and great to hear the post was helpful.

Leave a Reply

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