Check if the Current User has Submitted a Gravity From Before Showing Content

On a recent project I needed to check if a Gravity Form had been submitted before allowing the user to access content. Additionally we needed to check if the submission had happened within a specific time frame: within the past year.

It is easy to use Gravity Forms built in confirmations to redirect to a given page after submission. But, what if we are already on a page and want to check if a specific form has previously been submitted by the user?

To achieve this we need to have a look at the API Functions of Gravity Forms, specifically the count_entries function, here.

Count_entries returns the total number of entries for an array of criteria that we set up. The options we have are outlined in another function: get_entries.

The array of search criteria for get_entries can include any field that has been submitted and some other standard fields. The ones we are going to use are: created_by, start_date, and end_date.

Created_by, stores the user ID of the logged in user who submitted the form. This will be essential to check when and if that user has submitted the form.

Start_date and end_date allow us to set the time frame that we would like to look within. If these are left out, we will be searching all entries, with no time limitations.

Here’s the working code (7/17/2016) to verify that the logged in user has submitted a specific form.

The code is commented to help understand the various parts. I was using the code in a Genesis child theme page template, so some parts are specific to Genesis.



//This is where we hook into Genesis.

add_action ('genesis_before_loop', 'submitted_form_check');

function submitted_form_check(){
    
if ( is_user_logged_in() ) {//This is only relevant to users who are logged in. 

    $current_user = wp_get_current_user();//The curent user
    
    $date = strtotime(current_time( 'mysql' ). ' -1 year'); //Today minus one year 
    
    $startdate= date('Y-m-d', $date); //Today minus one year (Y-m-d format) 
    
    $enddate = current_time( 'mysql' ); // Today
    
    $search_criteria = array(
    
        'status'     => 'active', //Active forms 
        'start_date' => $startdate, //Get entires starting one year ago 
        'end_date'   => $enddate,   //upto now
        'field_filters' => array( //which fields to search
        
            array(
            
                'key' => 'created_by', 'value' => $current_user->ID, //Current logged in user
            )
          )
        );

    $form_id= 2; //Set the ID of the form to check.
    
    // Now the main Gravity form api function to count the entries 
    // using our custom search criteria.
    $entry_count = GFAPI::count_entries( $form_id, $search_criteria );

        //Test the output        
        //echo $current_user->display_name;
        //echo $form_id;
        //echo $entry_count;
        //echo $startdate; 
        //echo $enddate;

if($entry_count >= "1") { // If they have submitted the form:
    
    echo 'Hello ' . $current_user->display_name ;
    
    echo 'You have submitted the form. Etc, etc...';
    
} else {
//What to do if they have not submitted the form.  
  
remove_action( 'genesis_loop', 'genesis_do_loop' ); //Remove the page content

echo 'Hello ' . $current_user->display_name ;
 
echo 'You have not submitted the form. Etc, etc...';
} }
}

Share Aaron Jerad Designs on Facebook

5 Comments

  1. David · January 14, 2017 Reply

    This is fantastic! I’m embedding a form on a single custom post page. I’d like to add the criteria a method to check the current post id and see if it matches a field stored in the entry. Any ideas how to add that to the array?

    • Aaron Jerad · January 14, 2017 Reply

      Hi David,
      Yes, you only need to modify the ‘field_filters’ part to check for whatever you want, it is an array of arrays. All you need is the key number of the field you want to check.
      I find the key number by looking at the code for the GF field in the form edit screen:

      Here’s the HTML from a text field:

      
      <input name="input_2" id="input_2" value="" class="medium" tabindex="1" aria-required="true" aria-invalid="false" disabled="disabled" type="text">
      

      The key number from this form field is 2.

      Now to check field #2 to see if it matches the current post id:

      
      global $post; 
      $postID = $post->ID; //assign the current post ID to a variable
      
      'field_filters' => array(
      
                            array('key' => 'created_by', 'value' => $current_user->ID, //Current logged in user),
      	              array('key' => '2', 'value' => $postID))// Check the postID against field #2
                         );
      
      

      Now the entry count will only be 1 if the field was created by the current user AND the post id field matches the current post ID.

  2. Nat · June 17, 2017 Reply

    Hi Aaron, Thank you for posting this as this seems to be something very close to what I am looking to achieve. Basically I want to check if a (logged-in) user has already completed a specific form and if so display a message (or image) instead of the form, but I’m just not quite sure how to implement it using your example.

    I am using Gravity Forms and the Divi Builder from Elegant Themes and not Genesis so not sure which parts of the code is required (or not) or needs to be changed, or where to put the code

    Any help would be greatly appreciated.

    • Aaron Jerad · June 17, 2017 Reply

      Hi Nat,
      In my example the Genesis specific code is only the add_action and remove_action hooks. So the easiest route is to create a new page template and insert the code there, but strip out the Genesis action hook and just run it as a function or hook it to something Divi provides. Your challenge is learning how to create a Divi page template, because the Genesis specific code is minimal.

      If you use a basic WordPress page template like this: (name it: my-template-name.php )


      < ?php /* Template Name: CustomPageT1 */ ?>

      < ?php get_header(); ?>

      //insert the code here, using a difference action hook.
      //see this for an example of a custom Divi template: https://quiroz.co/create-404-page-using-divi-builder/

      < ?php get_footer(); ?>

      • Nat · June 26, 2017 Reply

        Thank you for taking the time to respond and for the advice Aaron. I wil be giving this a try.

Leave a Reply