Quantcast
Channel: AJ Designs
Viewing all articles
Browse latest Browse all 69

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

$
0
0

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...';
} }
}

The post Check if the Current User has Submitted a Gravity From Before Showing Content appeared first on Aaron Jerad Designs.


Viewing all articles
Browse latest Browse all 69

Trending Articles