Skip to content

Instantly share code, notes, and snippets.

@wpsmith
Created May 25, 2012 21:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpsmith/2790690 to your computer and use it in GitHub Desktop.
Save wpsmith/2790690 to your computer and use it in GitHub Desktop.
Prepopulate Radios in Gravity Forms
<?php
// Filter is applied to all forms
//add_filter('gform_pre_render', 'wps_populate_person');
// Filter only applied to Form id = 3
add_filter('gform_pre_render_3', 'wps_populate_person');
/**
* Pre-populates radios based on previous name field
* Searches post type 'person'
*
* @param array @form Current Form Object
* @return array @form Modified Form Object
*/
function wps_populate_person( $form ){
foreach( $form['fields'] as &$field ) {
if ( 'radio' != $field['type'] || false === strpos( $field['cssClass'], 'person' ) || ! rgar( $field, 'allowsPrepopulate' ) )
continue;
// For Simple Name input
//$query = new WP_Query( array( 's' => $_POST['input_1'], 'post_type' => array( 'wps_person' ), 'gform_search' => true ) );
// For Normal Name input
$query = new WP_Query( array( 's' => $_POST['input_1_3'] . '+' . $_POST['input_1_6'] , 'post_type' => array( 'wps_person' ), 'gform_search' => true ) );
$field['choices'] = array();
if ( empty( $query->posts ) ) {
// Empty field if needed
$field['choices'] = array( array( 'text' => 'No Persons Found', 'value' => '' ) );
}
else {
foreach( $query->posts as $post )
$field['choices'][] = array( 'text' => $post->post_title, 'value' => $post->ID );
}
// Add Other Choice
$field['enableOtherChoice'] = 1;
break;
}
return $form;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment