Contact form 7 get value before pipe (_raw_ value equivalent)

On one of our WordPress sites we used the excellent contact form 7 plugin. The plugin allows you to easily create forms for your site. One of the features we needed was to distribute our incoming leads to our agents (sales people) according to the country the user selected.  Contact form 7 allows this by using pipes (“|” pipe sign). This is a great solution and it works well. In the email sent to the agent you use the _raw_{field name} so it shows the country the visitor selected in the form while sending the email to the address indicated after the pipe sign.

What was our problem? well, we need to save our forms to a database. No problem, there are several plugins that do exactly that, but they don’t save _raw_ values. They only save the final value passed from CF7 which is the agent’s email. Our database record had email addresses instead of country names in the country field.  We were sure this is easy to retrieve since the email has the _raw_ value, but no, we couldn’t find any easy solution. Well, we had to circumvent.. here is our solution:

  1. adding a hidden field which will eventually hold the country name:
    [hidden countryname default:”waiting for setup”]
    the “waiting for setup” is just a placeholder
  2. on each select option write the country + pipe sign + emails of agents + identifier/break sign + country name:
    we use country names and agents for each country so it looks like this-
    [select* country class:custom-select class:custom-select-lg first_as_label “Country*” “Italy|[email protected]~Italy” “Spain|[email protected],[email protected]~Spain”]
    the break sign above is “~” and it separates between emails and the country name which is repeated twice (before pipe and at the end)
  3. add the following code to the selected theme’s “functions.php” file:

The goal was to achieve a solution which will allow controlling future changes via the form without going into the “functions.php” file for changes.

Let me know if you found this useful and leave a comment if you need any help or want to suggest other solutions.

And now, let’s play the pipes of peace!

5 comments

  1. Hello, I came across your code and used it exactly as displayed above (my use scenario is nearly identical to yours) but when I go through the steps and add the field, options and function/filters, my form does not send info. The form sends empty, any insight into what could be going on?

    Thanks!

  2. Hi,
    Verify that your form id is correct .
    Are you using the same break sign as above (“~”)?
    Uncomment the log line (line #7) and check log.txt file for info.
    Can you post your code and settings here? I will be happy to help you solve this.

    1. I did check the form ID and that is correct. I am using the same break sign also. I’ll check the config again and turn on logging to see what I come up with.

      Thanks again for your help, I’ll let you know what I find.

    1. If you uncomment the log line, you need to comment out the “for debugging” part – which means adding the “//” you removed to uncomment the entire line:
      “error_log(print_r($array,true),3,__DIR__.”/log.txt”); //for debugging”

      Let me know if that’s not it.

Leave a Reply

Your email address will not be published. Required fields are marked *