Gravity forms: Load the previous entry of a logged in user into a gravity forms
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 | |
/** | |
* Function that will prepopulate the previous logged-in user entry | |
* | |
*@author Johan De Reiziger | |
*@link <https://geekontheroad.com> | |
*/ | |
function gotr_get_latest_user_entry($form) { | |
//below you can define any form where this should not be applied to. Just separate them with a comma such as array(1,2,3) | |
$excluded_forms = array(); | |
if(!class_exists("GFAPI")) { | |
//check if gravity forms class exist otherwise abort | |
return $form; | |
} | |
if(!is_user_logged_in()) { | |
//only run for logged in users otherwise abort | |
return $form; | |
} | |
$current_user = wp_get_current_user();//get the curent user | |
$form_id = $form["id"]; //get id of this form | |
if(in_array($form_id, $excluded_forms)) { | |
//only proceed if this form is not excluded | |
return $form; | |
} | |
$search_criteria = array( | |
'status' => 'active', | |
'field_filters' => array( | |
array( | |
'key' => 'created_by', 'value' => $current_user->ID, //get entry of the current user only | |
) | |
) | |
); | |
$sorting = array( 'direction' => 'DESC'); | |
$paging = array( 'offset' => 0, 'page_size' => 1 ); | |
$total_count = 1; | |
$entry = GFAPI::get_entries( $form_id, $search_criteria, $sorting, $paging, $total_count ); //get our latest entry | |
if(!$entry) { | |
//abort if anything is wrong with the entry | |
return $form; | |
} | |
$normal_fields = array("text", "textarea", "phone", "email", "website", "number", "radio", "select", "hidden", "date"); | |
//loop through the fields and load the corresponding data from the entry | |
foreach( $form['fields'] as $field ) { | |
if(in_array($field->type, $normal_fields) ) { | |
$field->defaultValue = $entry[0][$field->id]; | |
} else if($field->type == "checkbox") { | |
$choices = $field->choices; //get all choices | |
$amount_of_choices = count($choices); //count how many we have so we can loop through | |
for ($x = 1; $x <= $amount_of_choices; $x++) { | |
if(!empty($entry[0][$field->id . "." .$x])) { //not empty so must be selected | |
$choices[($x-1)]["isSelected"] = 1; | |
} | |
} | |
$field->choices = $choices; //save | |
} else if($field->type == "address") { | |
$inputs = $field->inputs; | |
foreach ($inputs as $key => $value) { | |
//loop through all the fields and complete them | |
$inputs[$key]["defaultValue"] = $entry[0][$field->id . "." . ($key+1)]; | |
} | |
$field->inputs = $inputs; //save | |
} else if ($field->type == "name") { | |
$inputs = $field->inputs; | |
$inputs[0]["defaultValue"] = $entry[0][$field->id . "." . 2]; //prefix | |
$inputs[1]["defaultValue"] = $entry[0][$field->id . "." . 3]; //first name | |
$inputs[2]["defaultValue"] = $entry[0][$field->id . "." . 4]; //midle name | |
$inputs[3]["defaultValue"] = $entry[0][$field->id . "." . 6]; //last name | |
$inputs[4]["defaultValue"] = $entry[0][$field->id . "." . 8]; //suffix | |
$field->inputs = $inputs; //save | |
} | |
} | |
return $form; | |
} | |
add_filter( 'gform_pre_render', 'gotr_get_latest_user_entry' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment