0

I recently found solution for populating dropdown list with terms relative to the post. Everything works but I need to use this code on multiple dropdown lists within one form.

My code looks something like this but it doesn’t work, can you help me with that? Thank you.

add_filter('cf7sg_dynamic_dropdown_custom_options', 'filter_options',10,3);
function filter_options($options, $field_name, $form_key){
 if($form_key != 'bez-nazvu') return $options; //check this is the correct form.
 if($field_name != 'dynamic_select-461') return $options; //check this is the correct field.
 $options = array(); 
 //get your terms
$terms = get_the_terms( $post->ID , 'strava' );
 if( ! empty( $terms ) && ! is_wp_error( $term_args ) ) {

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

function filter_options($options, $field_name, $form_key){
 if($form_key != 'bez-nazvu') return $options; //check this is the correct form.
 if($field_name != 'dynamic_select-462') return $options; //check this is the correct field.
 $options = array(); 
 //get your terms
$terms = get_the_terms( $post->ID , 'doprava' );
 if( ! empty( $terms ) && ! is_wp_error( $term_args ) ) {

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

function filter_options($options, $field_name, $form_key){
 if($form_key != 'bez-nazvu') return $options; //check this is the correct form.
 if($field_name != 'dynamic_select-463') return $options; //check this is the correct field.
 $options = array(); 
 //get your terms
$terms = get_the_terms( $post->ID , 'terminy' );
 if( ! empty( $terms ) && ! is_wp_error( $term_args ) ) {

   foreach( $terms as $term ) {
     //this is the <option value="">label</opton> value->label pairs.
     $options[$term->name] = $term->name;
   }
 }  return $options;
 
} 
1
  • you are erroneously using the php filter coding mechanism. Every add_filter hook must call a single and unique function, you are trying to call 3 functions! Please read this tutorial on how to use php hooks. – Aurovrata Jun 29 '20 at 7:09
0

Try this instead, combine all 3 functions into a single one,

add_filter('cf7sg_dynamic_dropdown_custom_options', 'filter_options',10,3);
function filter_options($options, $field_name, $form_key){
  //field 'dynamic_select-461'
  if($form_key == 'bez-nazvu' && $field_name == 'dynamic_select-461') {
    $options = array(); 
    //get your terms
    $terms = get_the_terms( $post->ID , 'strava' );
    if( ! empty( $terms ) && ! is_wp_error( $term_args ) ) {

      foreach( $terms as $term ) {
        //this is the <option value="">label</opton> value->label pairs.
        $options[$term->name] = $term->name;
      }
    }
  }
  //field 'dynamic_select-462'
  if($form_key == 'bez-nazvu' && $field_name == 'dynamic_select-462') {
   $options = array(); 
   //get your terms
   $terms = get_the_terms( $post->ID , 'doprava' );
   if( ! empty( $terms ) && ! is_wp_error( $term_args ) ) {

     foreach( $terms as $term ) {
       //this is the <option value="">label</opton> value->label pairs.
       $options[$term->name] = $term->name;
     }
   }  
  } 
 //field 'dynamic_select-463'
 if($form_key == 'bez-nazvu' && $field_name == 'dynamic_select-463') {
   $options = array(); 
   //get your terms
   $terms = get_the_terms( $post->ID , 'terminy' );
   if( ! empty( $terms ) && ! is_wp_error( $term_args ) ) {
     foreach( $terms as $term ) {
       //this is the <option value="">label</opton> value->label pairs.
       $options[$term->name] = $term->name;
     }
   }  
   
  }
  return $options;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.