Skip to content

Instantly share code, notes, and snippets.

@mikeselander
Created February 28, 2014 03:32
Show Gist options
  • Save mikeselander/9264675 to your computer and use it in GitHub Desktop.
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
/**
* 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