I need dynamic number of input fields i.e. Question and answers in Contact form 7 with my custom form Field it's been generated based on the question set suits a candidate it can 1 or it can 10 also here is what i have added just to make it short i have hardcoded everything instead of showing you database queries and loops

add_action( 'wpcf7_init', 'my_questions_field' );

function my_questions_field() {
   wpcf7_add_form_tag( 'my_questions', 'my_custom_question_form_tag_handler' , array( 'name-attr' => true ) );

function my_custom_question_form_tag_handler( $tag ) { 
    $input ='<label>Ques1?</label><input value="Ques1?" name="tag[]" type="hidden">';
    $input .= '<input name="ans[]"  type="text" value="ans1">';
    $input .= '<label>Ques2?</label><input name="tag[]"  value="Ques2?" type="hidden">';
    $input .= '<input name="ans[]"  type="text" value="ans2">';
    return $input;

added :


in contact form 7 emails from the backend

The caveat here is i don't know the number of question it will going to render it is base on some of my custom queries the number of question may vary from 1-10 or maybe even more that's why i have used this syntax.

the output in email i am getting is


What i need in email is

Ques1: Ans1
Ques2: Ans2

I have found the function in contact-form-7/includes/functions.php

function wpcf7_flat_join( $input ) {
 $input = wpcf7_array_flatten( $input );
 $output = array();

 foreach ( (array) $input as $value ) {
  $output[] = trim( (string) $value );

 return implode( ',', $output );

and of email function contact-form-7/includes/mail.php

private function replace_tags_callback( $matches, $html = false ) {
  // allow [[foo]] syntax for escaping a tag
  if ( $matches[1] == '[' && $matches[4] == ']' ) {
   return substr( $matches[0], 1, -1 );

  $tag = $matches[0];
  $tagname = $matches[2];
  $values = $matches[3];

  if ( ! empty( $values ) ) {
   preg_match_all( '/"[^"]*"|\'[^\']*\'/', $values, $matches );
   $values = wpcf7_strip_quote_deep( $matches[0] );

  $do_not_heat = false;

  if ( preg_match( '/^_raw_(.+)$/', $tagname, $matches ) ) {
   $tagname = trim( $matches[1] );
   $do_not_heat = true;

  $format = '';

  if ( preg_match( '/^_format_(.+)$/', $tagname, $matches ) ) {
   $tagname = trim( $matches[1] );
   $format = $values[0];

in Class WPCF7_MailTaggedText

but not able to find the proper solution

just want contact form 7 to treat inputs from que and ans tag differently.


In the first step, the form to display, you can keep what you have coded.

Then you can do that to generate the result in the e-mail :

add_filter("wpcf7_posted_data", function ($posted_data) {

    $result = "";

    foreach ($posted_data["tag"] as $index => $question) {

        $answer = $posted_data["ans"][$index];

        $result .= "$question : $answer\n";


    $posted_data["my_questions"] = $result;

    return $posted_data;


And in the configuration of the e-mail, just put the tag [my_questions]

  • Thank you.. it works like a charm.. but it would be much better if you can tell me the answer of additional part of the question. – Taj Khan Jan 12 '18 at 4:49
  • which additional part ? my code output in the e-mail the informations that you ask. – mmm Jan 12 '18 at 8:05
  • sorry.. but the edit of my question is till pending for review.. so thank you.. actually i wanted the same thing to show on the flamingo database.. now there are 3 fields myquestion, tag and ans.. i only need my questions as other two are repeating. – Taj Khan Jan 12 '18 at 8:51
  • I don't see pending edit in your question. it may be better to create a new question. – mmm Jan 12 '18 at 9:03
  • here is my new question wordpress.stackexchange.com/questions/290895/… – Taj Khan Jan 12 '18 at 9:24

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.