Create a Gravity Form for Two Users

Recently, a client contacted me with request to finesse a Gravity Form–specifically, they wanted to re-purpose the built in “save and continue” button and use that to allow two different users to fill out one form.

This seemed like a creative and somewhat unique way to use a Gravity Form, so of course I was intrigued. The client had done good work setting up the form, had run into a blocking issue. Gravity allows you to change the label on the “save and continue” button, so changing that to “send to User 2” was easy, but having that button and the final “submit” button was confusing to both users; the client therefore asked if I could help with managing the buttons.

In an ideal scenario, what would happen would be:

  • User 1 would see only the “send to User 2” button
  • User 2 would see only the “submit” button
Gravity Form settings for two users

Gravity Forms has a lot of options for field visibility, using conditional logic, so this seemed like a reasonable starting point. The final “submit” button can also be shown/hidden based on conditional logic. I tinkered with this a bit, and had the final “submit” button set to show only if the second user had entered their data, but the “save and continue” button doesn’t allow for conditional logic, so I knew that I needed a different solution. This also didn’t give me any way to prevent User 1 from submitting the fully completed form, which was what the client really wanted.

This tutorial requires Gravity Forms. It may be possible to use similar code with a different forms plugin, if you have a different one you prefer to use.

Setting up a form for two users: the code

I had originally thought that I would have to manage the buttons with some JavaScript–doable, but I realized that I wouldn’t need that at all. Another user pointed me to the filter for the “save and continue” button, which meant that I could tinker with that using just PHP.

The “save and continue” button works by saving the initial form data to the server and allowing the user to send themselves an email on the confirmation screen. That email contains a link back to the partially filled form submission. The way the link works is that it contains a customized, unique query string. The URL will look something like this:

https://example.com/two-user-form/?gf_token=7818e1d2ce96441ea7404af1fb10e7ee

The key is that when User 1 visits the page, the URL is simple, and just ends in two-user-form (as an example), but when User 2 clicks the link in their email, the URL includes this gf_token, which Gravity Forms uses to retrieve the previously submitted data. So, all I needed to do was to harness that for my own purposes. It ended up being fairly simple, using the filter on the “save and continue” link, and also using the filter on the “submit” button. First, the code:

<?php
add_filter( 'gform_savecontinue_link_1', 'prefix_savecontinue_form_id_1', 10, 2 );
/**
* Maybe remove the "save and continue" button from Gravity Form ID 1.
* Checks to see if the "save and continue" token is present--
* if no, the button will render, otherwise not.
*
* @param string $save_button
* @param object $form
* @return string
*/
function prefix_savecontinue_form_id_1( $save_button, $form ) {
if ( prefix_does_token_exist() ) {
return '';
}
return $save_button;
}
add_filter( 'gform_submit_button_1', 'prefix_submit_form_id_1', 10, 2 );
/**
* Maybe remove the "submit" button from Gravity Form ID 1.
* Checks to see if the "save and continue" token is present--
* if yes, the submit button will render, otherwise not.
*
* @param string $button
* @param object $form
* @return string
*/
function prefix_submit_form_id_1( $button, $form ) {
if ( ! prefix_does_token_exist() ) {
return '';
}
return $button;
}
/**
* Check to see if the second user is viewing the form (based on whether the Gravity Forms token is present).
*
* @return boolean
*/
function prefix_does_token_exist() {
return (bool) filter_input( INPUT_GET, 'gf_token', FILTER_SANITIZE_STRING );
}

You’ll see that the two filter hooks look different from what’s in the Gravity Forms documentation–each ends in _1. This is because Gravity Forms provides the general filter hooks, but you can also make the hook specific to the form by adding the form ID (without this, or conditional logic in the function, the code will run on every form on your site, which could be problematic). In my example code, the form is form 1, but you’ll want to change that to whatever your two user form is.

Because the code for this form depends only on the presence of the gf_token string, I created a helper function which checks to see if that’s present (prefix_does_token_exist).

My prefix_savecontinue_form_id_1 function modifies the “save and continue” button–it checks to see if the token is present, meaning that User 2 is editing the form. If it is present, the function returns an empty string, and the “save and continue” button is not output at all. If it isn’t present, then the button is output.

My prefix_submit_form_id_1 function modifies the “submit” button, in much the same way, but the logic is simply reversed–the “submit” button shows only if the token is present.

That’s all the code that’s required, but more work needs to be done. The code is actually the simplest part of the process.

Conditional logic for transferring the form from one user to the other

At this point, I can use the conditional logic options for the form settings and fields:

  • I set the “submit” button to show only after User 2 has entered their data (Second User Input is not empty)
  • I set the Second User Input to show only if the Second User’s Email field has a value (this is not ideal, as User 1 could go ahead and fill this out, but setting the Second User Input to show based on the token presence would require more code).

Here’s what User 1 sees when opening the form:

Gravity Forms for two users: User 1 view

Here’s what User 2 sees after adding their data:

Gravity Forms for two users: User 2 view

Note: because the buttons are added/removed with PHP instead of JavaScript, they’re not hidden for either user; they are simply not added to the page at all. So even though User 1 could theoretically put in data for User 2, User 1 cannot perform the final submission without the link that’s generated by Gravity Forms. (User 2 could modify User 1’s inputs, so this flow does presuppose that the users have some level of trust in each other.)

Update the form notifications

Gravity Forms for two users: notification setup

The next part of this process is to set up a new notification email, as the default “save and continue” notification email won’t be used, but User 2 needs to know that they have work to do. Based on the form I set up, here’s mine (click the image for a large view). I’ve copied the content from the default “save and continue” notification, but set the “to” email address to be User 2, and the “from name” to be User 1, as well as the “reply to”. Additionally, the “event” at the top has to be “Form is saved”, so the email will be sent only when User 1 clicks the button to send the form to User 2.

Update the form confirmations

The final edits which need to be made are to the form confirmations. When the “save and continue” button is enabled, Gravity Forms adds two new confirmation messages to the form. The first one includes the “save and continue” link and allows the user to enter their email address, so that the link can be emailed to them; the second one confirms that the link was sent to that address. With this setup, you would want to edit the first confirmation (called “Save and Continue Confirmation”), and remove the default content. I replaced mine with:

Thank you! The form has been sent to {Second User\'s Email:2} for completion.

This is because we’re including the email address for User 2 in the form itself, and managing the email for that automatically (your merge tag will obviously be different than mine, based on your form). This also prevents User 1 from having the link to complete and finish the form on their own.

I expect there are other ways to accomplish this, but I enjoyed the process and end result. If you have a process which requires input from two users, especially if one has to approve the data from this other, this may be a helpful approach.

Photo credit: Markus Spiske

Do something awesome. Tell a friend:

Reader Interactions

Comments

  1. Rachel Robbins says

    Oh, interesting! I recently set up a Gravity Form for a client, to be saved and sent to another user. This will be great for refining the process. Thank you!

    • Robin says

      Thanks, Rachel! If you have recommendations to improve the process, I would love to hear them. This was a new kind of process for me.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.