CF7 Menus with Pipes

November 19th, 2014

This comes form a user who is using pipe characters “|” in a menu field in a Contact Form 7 form. Here is an example Contact Form 7 form field definition:

What does this do? Only the information before the “|” character display to the user in the form:

But the value that is sent in email and saved to the database is the value after the “|”. In this example, if a person selects “location1″ what is actually submitted is “email1@notwhere.com”.

This is very useful if you want your form submissions emailed to a different email address depending on what a user selects. For example, if the user selects “location1″ you might want the form email sent to “email1@nowhere.com”. Similarly, “location2″ gets emailed to “email2@nowhere.com” and “location3″ to “email3@nowherel.com”.

To achieve this, in the Contact Form 7 edit page, simply put the “location” field tag in the mail’s “to” field.

And in the form Email section:

Remember: for the [location] field, the user may choose “location1″ (what is before the “|”) but the value that CF7 reports is “email1@nowwhere.com” (what is after the “|”).

So far, so good. But in the database, what is saved for the location field is the email address (what is after the “|”). But what if you want to save the other part or both? As it stands, we get data like:

But what we might prefer is this:

To achieve this, we must create a filter following the instruction on the Changing Form Data Before it is Saved page, but using the following code instead.

In the code, change the values for $formName and $fieldName to match your form and field.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
function location_form_handler($formData)
{
    $formName = 'Form with Pipes'; // change this to your form's name
    $fieldName = 'location'; // change this to your field's name
    if ($formData && $formName == $formData->title && $formData->scanned_form_tags) {
        $emailSelected = $formData->posted_data[$fieldName];
        $valueSelected = null;
        foreach ($formData->scanned_form_tags as $tag) {
            if ($tag['name'] == $fieldName) {
                foreach ($tag['raw_values'] as $rawValue) {
                    // value|email
                    $valuesArray = explode('|', $rawValue);
                    if (count($valuesArray) == 2 && $valuesArray[1] == $emailSelected) {
                        $valueSelected = $valuesArray[0];
                        break;
                    }
                }
            }
            if ($valueSelected != null) {
                break;
            }
        }
        if ($valueSelected != null) {
            $formData->posted_data[$fieldName] = $valueSelected;
            $formData->posted_data[$fieldName . '_email'] = $emailSelected;
        }
    }
    return $formData;
}
add_filter('cfdb_form_data', 'location_form_handler');

Finally, update your form definition in Contact Form 7 thus:

This code makes sure that the “location” field now always has the value before the “|” (like “location1″) and never the value from after (like “email1″). At line 26, the code artificially injects a new field named “location_email” to hold what is after the “|”.

GMail trick for testing multiple emails
Let’s say you have a GMail eamil: “somebody@gmail.com”. You can send email to this address but with extra information in it using “+”. Examples:

  • somebody+location1@gmail.com
  • somebody+location2@gmail.com
  • somebody+location3@gmail.com

Messages sent to any of these emails will all go to the somebody@gmail.com account. Effectively everything from the “+” to the “@” is ignored for purposes of delivering the email. But you will see this extra information in the “To” field of the email you receive.

This is useful to testing when you want to use different email addresses but you want them to come to the same account. In GMail you can also set up filters on the “To” field to treat these messages differently from each other.

This may work with other email providers, but I haven’t tried it.

Debugging

You can log the form output to a log file. Follow the instructions in the debugging section of this page.

Here is example log output which may help you better understand the data structure:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
WPCF7_ContactForm Object
(
    [initial] => 
    [id] => 1484
    [title] => Location Select
    [unit_tag] => 
    [responses_count] => 0
    [scanned_form_tags] => Array           // ** INFO ABOUT THE FORM FIELD DEFINITIONS
        (
            [0] => Array
                (
                    [type] => select
                    [name] => location    // ** INFO ABOUT THE "location" FIELD DEFINITION
                    [options] => Array
                        (
                        )
 
                    [raw_values] => Array  // ** DESCRIBES THE FIELD DEFINITION
                                           // ** USE THIS TO LOOK UP LOCATION GIVEN EMAIL BELOW IN [posted_data]
                        (
                            [0] => Location 1|email1@nowhere.com
                            [1] => Location 2|email2@nowhere.com
                            [2] => Location 3|email3@nowhere.com
                        )
 
                    [values] => Array
                        (
                            [0] => Location 1
                            [1] => Location 2
                            [2] => Location 3
                        )
 
                    [pipes] => WPCF7_Pipes Object
                        (
                            [pipes] => Array
                                (
                                    [0] => WPCF7_Pipe Object
                                        (
                                            [before] => Location 1
                                            [after] => email1@nowhere.com
                                        )
 
                                    [1] => WPCF7_Pipe Object
                                        (
                                            [before] => Location 2
                                            [after] => email2@nowhere.com
                                        )
 
                                    [2] => WPCF7_Pipe Object
                                        (
                                            [before] => Location 3
                                            [after] => email3@nowhere.com
                                        )
 
                                )
 
                        )
 
                    [labels] => Array
                        (
                            [0] => Location 1
                            [1] => Location 2
                            [2] => Location 3
                        )
 
                    [attr] => 
                    [content] => 
                )
 
            [1] => Array
                (
                    [type] => submit
                    [name] => 
                    [options] => Array
                        (
                        )
 
                    [raw_values] => Array
                        (
                            [0] => Send
                        )
 
                    [values] => Array
                        (
                            [0] => Send
                        )
 
                    [pipes] => WPCF7_Pipes Object
                        (
                            [pipes] => Array
                                (
                                    [0] => WPCF7_Pipe Object
                                        (
                                            [before] => Send
                                            [after] => Send
                                        )
 
                                )
 
                        )
 
                    [labels] => Array
                        (
                            [0] => Send
                        )
 
                    [attr] => 
                    [content] => 
                )
 
        )
 
    [posted_data] => Array                    // ** HERE IS THE FORM SUBMITTED DATA
        (
            [_wpcf7] => 1484
            [_wpcf7_version] => 3.3.1
            [_wpcf7_unit_tag] => wpcf7-f1484-p1485-o1
            [_wpnonce] => f6c3315826
            [location] => email1@nowhere.com  // ** USER'S SELECTION
            [_wpcf7_is_ajax_call] => 1
        )
 
    [uploaded_files] => Array
        (
        )
 
    [skip_mail] => 
    [form] =>

Location
[select location “Location 1|email1@nowhere.com” “Location 2|email2@nowhere.com” “Location 3|email3@nowhere.com”]

[submit “Send”]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
    [mail] => Array
        (
            [subject] => [location]
            [sender] => mike@nowhere.com
            [body] => Message Body:
[location]
 
--
This mail is sent via contact form on Blog (deleted)
            [recipient] => [location_email]
            [additional_headers] => 
            [attachments] => 
            [use_html] => 
        )
 
    [mail_2] => Array
        (
            [active] => 
            [subject] => [your-subject]
            [sender] => [your-name] 
            [body] => Message body:
[your-message]
 
--
This mail is sent via contact form on Blog (deleted)
            [recipient] => [your-email]
            [additional_headers] => 
            [attachments] => 
            [use_html] => 
        )
 
    [messages] => Array
        (
            [mail_sent_ok] => Your message was sent successfully. Thanks.
            [mail_sent_ng] => Failed to send your message. Please try later or contact the administrator by another method.
            [validation_error] => Validation errors occurred. Please confirm the fields and submit it again.
            [accept_terms] => Please accept the terms to proceed.
            [invalid_email] => Email address seems invalid.
            [invalid_required] => Please fill the required field.
            [akismet_says_spam] => Failed to send your message. Please try later or contact the administrator by another method.
            [captcha_not_match] => Your entered code is incorrect.
            [upload_failed] => Failed to upload file.
            [upload_file_type_invalid] => This file type is not allowed.
            [upload_file_too_large] => This file is too large.
            [upload_failed_php_error] => Failed to upload file. Error occurred.
            [quiz_answer_not_correct] => Your answer is not correct.
            [spam] => Failed to send your message. Please try later or contact the administrator by another method.
        )
 
    [additional_settings] => 
    [submit_time] => 1354886748.3493
    [ip] => 216.40.66.61
    [user] => admin
)
  1. Michael Simpson
    January 29th, 2013 at 16:34 | #1

    A comment from a user:

    In my form, visitors who fill out the form select the state they are located in, then the salesperson who is responsible for that state receives the email. As well, I needed the email to be sent to a central (corporate) email address.

    However, not all states have an assigned salesperson, being the company focuses on the mid-west.

    With Contact Form 7, if the email address after the pipe was empty, emails would not be sent. The method outlined by Michael actually allows an empty field after the pipe.

    I set up “Mail” in the form to send to the central email and “Mail (2)” to send to the salesperson (if one exists).

  2. wala
    February 5th, 2013 at 04:15 | #2

    hey ,
    Thank you for this helpful post .
    I want ask you ask you about somethings , I want store ID from textbox in database , Can I check first If this id exists in db or not ? and show message to user

    • Michael Simpson
      February 5th, 2013 at 10:32 | #3

      To do that, you need to hook into CF7 when it validates data. This post describes a hook called after validation is done and the post is submitted. Have a look at this post.

  3. wala
    February 6th, 2013 at 05:09 | #4

    Thank you very much : )

  4. Dee
    August 5th, 2013 at 11:08 | #5

    I have followed the steps listed in your article to send mails to separate IDs depending on the selection. However I get the red box wrror message: Failed to send your message. Please try later or contact the administrator by another method.
    Any suggestions on how i can solve this problem?
    Thank you!

    • Michael Simpson
      August 5th, 2013 at 14:04 | #6

      This indicates there is some error happening in your PHP code. You will need to figure out what that is and fix it.

Comments are closed.  Go To Support Forum