Capture Post Content in a Gravity Form Field

by

In this post we will learn how to grab a portion of the content from a WordPress post containing a Gravity Form and send it into a field of that form, so that this content can be recorded with the submission and repurposed for confirmations, notifications, and any generated PDFs.

Step 1: Add a Receiving Field

The first thing we will need in order to capture the content into a Gravity Form submission is a field that will hold the content.

  1. Add a Paragraph Text field.
  2. In the Advanced tab, for Visibility, select Hidden.
  3. In the Advanced tab, select Use the Rich Text Editor option.
  4. In the Appearance tab, add content_receiver as a Custom CSS Class.

Step 2: Add the Script

This script will grab – from the .entry-content element of your page – content delineated by a unique <!-- START CONTRACT --> and <!-- END CONTRACT --> HTML comment and send it to the content_receiver field.

<script>
   jQuery(document).ready(function(){
      const contentStart = '<!-- START CONTRACT -->';
      const contentEnd = '<!-- END CONTRACT -->';
      const contentFull = jQuery('.entry-content').html();
      const contentSent = contentFull.substr(0,contentFull.indexOf(contentEnd)).substr(contentFull.indexOf(contentStart)).replace(contentStart,'');
      jQuery('.content_receiver textarea').val(contentSent.replace(/\n/gm,' '));
   });
</script>

The selector used to identify your page content may need to be updated per your specific theme.

You may also customize the HTML comment text to better suit your use case.

Find this script and others at snippet.farm.

Add this script alongside your form in whatever way you deem best. I’d suggest either adding it to an HTML field in the form or utilizing the handy and free Gravity Forms Custom Javascript plugin by Gravity Wiz.

Step 3: Identify the Content

Your final step is to identify the content you want to capture from the page that houses your Gravity Form.

  1. Add a Custom HTML block.
  2. Insert your <!-- START CONTRACT --> comment.
  3. Position the block just before the content you want to capture.
  4. Add another Custom HTML block.
  5. Insert your <!-- END CONTRACT --> comment.
  6. Position the block just after the content you want to capture.

Finishing Well

At this point, any Gravity Form submissions received on that page will grab the specific content of that page. You can now repurpose WordPress content (specific contract terms, event details, etc.) for use in confirmations, notifications, and/or generated PDFs.

Check It Out

In the Gravity Form field shown below, you can see that I have auto-captured the content of Step 3 from this post. In production, I would have this field set to hidden, so the user won’t be troubled with it. Pretty slick, eh?

Now, get to it. Start something good!

Create fields once. Use everywhere.