1

I cannot seem to find a solution, neither here nor Google. A client requested to have ONE name field in his CF7 Form but once the email is sent it needs to bcome TWO fields.

Example: [your-name] becomes [first-name] [surname] somewhere hidden in the form.

How is this even possible?

EDIT

I have built something with webhooks but for some reason it does not submit the data.

add_action( 'wpcf7_before_send_mail', 'wpcf7_strip_name', 10, 1 );
function wpcf7_strip_name($contact_form){

   $submission = WPCF7_Submission::get_instance();
   $posted_data = $submission->get_posted_data();
    if ($posted_data["your-name"]){
    $name = trim($posted_data["your-name"]);
    $last_name = (strpos($name, ' ') === false) ? '' : preg_replace('#.*\s([\w-]*)$#', '$1', $name);
    $first_name = trim( preg_replace('#'.$last_name.'#', '', $name ) );

  // Set default value for last name if none is provided   
      if ($last_name == NULL ){
      $last_name = "Not Provided";
      }
    }
}

In my Contact Form I have the following hidden fields

[hidden first_name]
[hidden last_name]

Both fields are declared in the email too as [first_name] [last_name] but the name-stripping does not pull through

  • If you want to do this in server-side code you can hook wpcf7_posted_data to split the name into two separate fields, although I don't know whether you can simply add the fields at this point or how you'd need to configure them. – Rup May 16 at 11:29
  • I've tried to find somethink but cannot find an example. Do you have an example how the code on server side would look like? I don't mind having them hidden somewhere – mad2kx May 16 at 16:43
  • @Rup check my edit – mad2kx May 16 at 19:54
  • In your function you're not saving the computed first and last names anywhere. You could try saving them into $posted_data["first_name"] = $first_name; etc. I don't know CF7 well enough to know if that's going to work though. – Rup May 17 at 13:41
  • I've tried that but it does not pull through the result – mad2kx May 17 at 13:55
-1

could be possible with jQuery. This Idea is to add a Hidden field and set value to it with jquery.

$("#name_field_id").focusout(function(){
     str = $(this).val();
     if(str){
      s = str.split(/(?<=^\S+)\s/); //split string in name field after first space found. 
      $("#hidden_field_id").val(s[1]); //s[1] value after the first space set to hidden field.
     }
  });

then you can use the hidden field in email.

  • check my edit which I have just posted – mad2kx May 16 at 19:55

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