Contact Form 7 – Populating dropdown list with terms relative to the post

Use the dynamic_dropdown tag functionality offered by the Smart Grid layout extension. The dynamic tag can take 3 sources of data (a given taxonomy, or branch within that taxonomy, a list of post titles, or the results of a filtered hook function). Use the filter hook to populated your dropdown as,

add_filter('cf7sg_dynamic_dropdown_custom_options', 'filter_options',10,3);
function filter_options($options, $field_name, $form_key){
  if($form_key != 'my-form') return $options; //check this is the correct form.
  if($field_name != 'custom-dropdown') return $options; //check this is the correct field.
  $options = array(); 
  //get your terms
  $terms = get_terms( $term_args );
  // Add terms to values
  if( ! empty( $terms ) && ! is_wp_error( $term_args ) ) {

    foreach( $terms as $term ) {
      //this is the <option value="">label</opton> value->label pairs.
      $options[$term->id] = $term->name;
    }
  }
  return $options;
}

Leave a Comment