Created
February 28, 2014 03:32
-
-
Save mikeselander/9264675 to your computer and use it in GitHub Desktop.
This is an example of the gform_pre_render function - pulls from a custom table of contact
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
/** | |
* Creates a dropdown field based on a returned SQL query of a custom db table | |
* | |
* @since 0.1.0 | |
* | |
*/ | |
add_filter('gform_pre_render_7', 'populate_contacts'); // _7 relates this to a particular field | |
function populate_contacts( $form ){ | |
// Loop through fields | |
foreach($form['fields'] as &$field){ | |
// If the field has a population characteristic of 'contact_id' | |
// There are lots of options you can choose from to limit the fiels | |
if($field["inputName"] == "contact_id"){ | |
// Call the query functions | |
$contacts = otm_query_contacts( $_GET['company_id'] ); | |
// Set our first empty option | |
$choices = array(array('text' => 'Select a Contact', 'value' => ' ')); | |
// If we have values from the database | |
if ( !empty( $contacts ) ) : | |
// Loop through contacts | |
foreach( $contacts as $contact ) : | |
// Add the db value to the field as a choice | |
$choices[] = array('text' => $contact[fname]." ".$contact[lname], 'value' => $contact[log_id]); | |
endforeach; | |
endif; | |
$field["choices"] = $choices; | |
} | |
} | |
return $form; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment