Populate Gravity Form from Database

//Adds a filter to form id 26. Replace 26 with your actual form id
add_filter("gform_pre_render_26", populate_checkbox);
add_filter("gform_admin_pre_render_26", populate_checkbox);

function populate_checkbox($form){
	global $wpdb;
	$sample_table = $wpdb->prefix."sample"; // replace with your table name.
	$q = "SELECT id, name from $sample_table"; // whatever query you need to get your field info
	$results = $wpdb->get_results($q);
	$choices = array();
	$inputs = array();
	$x = 0;
	foreach ($results as $result) {
		$x++;
		$choices[] = array("text" => $result->name, "value" => $result->id);
		$inputs[] = array("label" => $result->name, "id" => "3.".$x); // replace the 3 in "3." with your field ID.
	}
    //Adding items to field id 3. Replace 3 with your actual field id. You can get the field id by looking at the input name in the markup.
    foreach($form["fields"] as &$field){
        //replace 3 with your checkbox field id
        if($field["id"] == 3){
            $field["choices"] = $choices;
            $field["inputs"] = $inputs;
        }
    }

    return $form;
}
War dieser Artikel hilfreich?
Nach oben scrollen