admin管理员组

文章数量:1426901

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 5 years ago.

Improve this question

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

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 5 years ago.

Improve this question

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

Share Improve this question edited May 16, 2019 at 19:54 mad2kx asked May 16, 2019 at 10:30 mad2kxmad2kx 1722 silver badges14 bronze badges 5
  • 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 Commented May 16, 2019 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 Commented May 16, 2019 at 16:43
  • @Rup check my edit – mad2kx Commented May 16, 2019 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 Commented May 17, 2019 at 13:41
  • I've tried that but it does not pull through the result – mad2kx Commented May 17, 2019 at 13:55
Add a comment  | 

1 Answer 1

Reset to default -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.

本文标签: plugin contact form 7CF7 SplitSeparate fields