Created
November 17, 2022 05:08
-
-
Save Archie22is/0d471794767dd73df2b281dcbb052844 to your computer and use it in GitHub Desktop.
Check/verify that the logged in user has submitted a specific Gravity Form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Check/verify that the logged in user has submitted a specific Gravity Form | |
* https://aaronjerad.com/blog/check-if-user-submitted-gravity-form/ | |
* | |
*/ | |
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...'; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment