{"id":804,"date":"2012-11-24T19:24:25","date_gmt":"2012-11-25T00:24:25","guid":{"rendered":"http:\/\/cfdbplugin.com\/?page_id=804"},"modified":"2016-03-23T15:06:48","modified_gmt":"2016-03-23T19:06:48","slug":"cf7-menus-with-pipes","status":"publish","type":"page","link":"https:\/\/cfdbplugin.com\/?page_id=804","title":{"rendered":"CF7 Menus with Pipes"},"content":{"rendered":"<p>This comes form a user who is using pipe characters &#8220;|&#8221; in a menu field in a Contact Form 7 form. Here is an example Contact Form 7 form field definition:<\/p>\n<p><a href=\"http:\/\/cfdbplugin.com\/wp-content\/uploads\/2012\/11\/CF7_menu_with_pipes1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-876\" title=\"CF7_menu_with_pipes\" alt=\"\" src=\"http:\/\/cfdbplugin.com\/wp-content\/uploads\/2012\/11\/CF7_menu_with_pipes1.png\" width=\"397\" height=\"448\" srcset=\"https:\/\/cfdbplugin.com\/wp-content\/uploads\/2012\/11\/CF7_menu_with_pipes1.png 397w, https:\/\/cfdbplugin.com\/wp-content\/uploads\/2012\/11\/CF7_menu_with_pipes1-265x300.png 265w\" sizes=\"auto, (max-width: 397px) 100vw, 397px\" \/><\/a><\/p>\n<p>What does this do? Only the information before the &#8220;|&#8221; character display to the user in the form:<\/p>\n<p><a href=\"http:\/\/cfdbplugin.com\/wp-content\/uploads\/2012\/11\/CF7_menu.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-807\" title=\"CF7_menu\" alt=\"\" src=\"http:\/\/cfdbplugin.com\/wp-content\/uploads\/2012\/11\/CF7_menu.png\" width=\"92\" height=\"83\" \/><\/a><\/p>\n<p>But the value that is sent in email and saved to the database is the value after the &#8220;|&#8221;. In this example, if a person selects &#8220;location1&#8221; what is actually submitted is &#8220;email1@nowhere.com&#8221;.<\/p>\n<p>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 &#8220;location1&#8221; you might want the form email sent to &#8220;email1@nowhere.com&#8221;. Similarly, &#8220;location2&#8221; gets emailed to &#8220;email2@nowhere.com&#8221; and &#8220;location3&#8221; to &#8220;email3@nowherel.com&#8221;.<\/p>\n<p>To achieve this, in the Contact Form 7 edit page, simply put the &#8220;location&#8221; field tag in the mail&#8217;s &#8220;to&#8221; field.<\/p>\n<p>And in the form Email section:<br \/>\n<a href=\"http:\/\/cfdbplugin.com\/wp-content\/uploads\/2012\/11\/Screen-Shot-2016-01-14-at-8.32.05-PM.png\" rel=\"attachment wp-att-1372\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-1372\" src=\"http:\/\/cfdbplugin.com\/wp-content\/uploads\/2012\/11\/Screen-Shot-2016-01-14-at-8.32.05-PM-300x153.png\" alt=\"To field\" width=\"300\" height=\"153\" srcset=\"https:\/\/cfdbplugin.com\/wp-content\/uploads\/2012\/11\/Screen-Shot-2016-01-14-at-8.32.05-PM-300x153.png 300w, https:\/\/cfdbplugin.com\/wp-content\/uploads\/2012\/11\/Screen-Shot-2016-01-14-at-8.32.05-PM-768x392.png 768w, https:\/\/cfdbplugin.com\/wp-content\/uploads\/2012\/11\/Screen-Shot-2016-01-14-at-8.32.05-PM.png 874w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Remember: for the [location] field, the user may choose &#8220;location1&#8221; (what is before the &#8220;|&#8221;) but the value that CF7 reports is &#8220;email1@nowwhere.com&#8221; (what is after the &#8220;|&#8221;).<\/p>\n<p>So far, so good. But in the database, what is saved for the location field is the email address (what is after the &#8220;|&#8221;). But what if you want to save the other part or both? As it stands, we get data like:<\/p>\n<p><a href=\"http:\/\/cfdbplugin.com\/wp-content\/uploads\/2012\/11\/DB-submissions11.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-878\" title=\"DB-submissions1\" alt=\"\" src=\"http:\/\/cfdbplugin.com\/wp-content\/uploads\/2012\/11\/DB-submissions11.png\" width=\"566\" height=\"182\" srcset=\"https:\/\/cfdbplugin.com\/wp-content\/uploads\/2012\/11\/DB-submissions11.png 566w, https:\/\/cfdbplugin.com\/wp-content\/uploads\/2012\/11\/DB-submissions11-300x96.png 300w\" sizes=\"auto, (max-width: 566px) 100vw, 566px\" \/><\/a><\/p>\n<p>But what we might prefer is this:<br \/>\n<a href=\"http:\/\/cfdbplugin.com\/wp-content\/uploads\/2012\/11\/DB-submissions21.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-877\" title=\"DB-submissions2\" alt=\"\" src=\"http:\/\/cfdbplugin.com\/wp-content\/uploads\/2012\/11\/DB-submissions21.png\" width=\"619\" height=\"229\" srcset=\"https:\/\/cfdbplugin.com\/wp-content\/uploads\/2012\/11\/DB-submissions21.png 619w, https:\/\/cfdbplugin.com\/wp-content\/uploads\/2012\/11\/DB-submissions21-300x110.png 300w\" sizes=\"auto, (max-width: 619px) 100vw, 619px\" \/><\/a><\/p>\n<p>To achieve this, we must create a filter using the <a href=\"https:\/\/wordpress.org\/plugins\/add-actions-and-filters\/\" target=\"_blank\">Shortcodes, Actions and Filters<\/a> plugin. Create a new code item with the following code. <\/p>\n<p>(Updated code. Earlier version of code here no longer worked with the latest CF7)<\/p>\n<p><strong><span style=\"background-color: yellow;\">Note: Do not repeat any email addresses in the choices, otherwise it is not guaranteed to give you the correct associated location value. <a href=\"https:\/\/wordpress.org\/support\/topic\/cf7-menus-with-pipes-and-repeated-emails?replies=3\" target=\"_blank\">Please see this thread<\/a>. <\/span><\/strong><\/p>\n<p>Add this function. If using <a href=\"https:\/\/wordpress.org\/plugins\/add-actions-and-filters\/\" target=\"_blank\">Shortcodes, Actions and Filters<\/a>, I recommend that you add this as its own Action code element. Add this one first. Be sure to activate it. <\/p>\n<pre lang=\"php\" line=\"1\" escaped=\"true\">\r\nfunction form_with_pipes_handler($formName, $fieldName, $newFieldName, &$formData)\r\n{\r\n    if ($formData &&\r\n            $formName == $formData->title &&\r\n            property_exists($formData, 'WPCF7_ContactForm') &&\r\n            method_exists($formData->WPCF7_ContactForm, 'form_scan_shortcode')) {\r\n\r\n        $scanned_form_tags = $formData->WPCF7_ContactForm->form_scan_shortcode();\r\n        $emailSelected = $formData->posted_data[$fieldName];\r\n        if (is_array($emailSelected) && count($emailSelected) == 1) {\r\n            $emailSelected = $emailSelected[0];\r\n        }\r\n        $valueSelected = null;\r\n        foreach ($scanned_form_tags as $tag) {\r\n            if ($tag['name'] == $fieldName) {\r\n                foreach ($tag['raw_values'] as $rawValue) {\r\n                    \/\/ value|email\r\n                    $valuesArray = explode('|', $rawValue);\r\n                    if (count($valuesArray) == 2 && $valuesArray[1] == $emailSelected) {\r\n                        $valueSelected = $valuesArray[0];\r\n                        break;\r\n                    }\r\n                }\r\n            }\r\n            if ($valueSelected != null) {\r\n                break;\r\n            }\r\n        }\r\n        if ($valueSelected != null) {\r\n            $formData->posted_data[$fieldName] = $valueSelected;\r\n            $formData->posted_data[$newFieldName] = $emailSelected;\r\n        }\r\n    }\r\n    return $formData;\r\n}\r\n<\/pre>\n<p>The above is a re-usable function that is only added once. The following is a code function to deal with your specific form. You can add multiple versions of the following code. Add one for each form. In <a href=\"https:\/\/wordpress.org\/plugins\/add-actions-and-filters\/\" target=\"_blank\">Shortcodes, Actions and Filters<\/a>, make each one its own code element so that you can activate\/deactivate each independently. But you must be sure to have different function name for each. <\/p>\n<p>Make the following changes in the code below<\/p>\n<p>In the code, change the values for <strong>$formName<\/strong> and <strong>$fieldName<\/strong> to match your form and field.<\/p>\n<pre lang=\"php\" line=\"1\" escaped=\"true\">\r\nfunction location_form_handler($formData) \/\/ Use a different function name for each form\r\n{\r\n    $formName = 'Pipes'; \/\/ change this to your form's name\r\n    $fieldName = 'location'; \/\/ change this to your field's name\r\n    $newFieldName = $fieldName . '_email';\r\n    return form_with_pipes_handler($formName, $fieldName, $newFieldName, $formData);\r\n}\r\nadd_filter('cfdb_form_data', 'location_form_handler'); \/\/ ensure 2nd param matches above function name\r\n<\/pre>\n<p>This code makes sure that the &#8220;location&#8221; field now always has the value before the &#8220;|&#8221; (like &#8220;location1&#8221;) and never the value from after (like &#8220;email1&#8221;). At line 36, the code artificially injects a new field named &#8220;location_email&#8221; to hold what is after the &#8220;|&#8221;. At line 35 it re-assigns the value of the location field (which was the email value) to the display value (what was before the &#8220;|&#8221;). <\/p>\n<p><strong>GMail trick for testing multiple emails<\/strong><br \/>\nLet&#8217;s say you have a GMail email: &#8220;somebody@gmail.com&#8221;. You can send email to this address but with extra information in it using &#8220;+&#8221;. Examples:<\/p>\n<ul>\n<li>somebody+location1@gmail.com<\/li>\n<li>somebody+location2@gmail.com<\/li>\n<li>somebody+location3@gmail.com<\/li>\n<\/ul>\n<p>Messages sent to any of these emails will all go to the somebody@gmail.com account. Effectively everything from the &#8220;+&#8221; to the &#8220;@&#8221; is ignored for purposes of delivering the email. But you will see this extra information in the &#8220;To&#8221; field of the email you receive.<\/p>\n<p>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 &#8220;To&#8221; field to treat these messages differently from each other.<\/p>\n<p>This may work with other email providers, but I haven&#8217;t tried it.<\/p>\n<p>FYI: example structure of $scanned_form_tags <\/p>\n<pre lang=\"php\" line=\"1\" escaped=\"true\">\r\nArray\r\n(\r\n    [0] => Array\r\n        (\r\n            [type] => select\r\n            [basetype] => select\r\n            [name] => location\r\n            [options] => Array\r\n                (\r\n                )\r\n\r\n            [raw_values] => Array\r\n                (\r\n                    [0] => location1|email1@nowhere.com\r\n                    [1] => location2|email2@nowhere.com\r\n                    [2] => location3|email3@nowhere.com\r\n                )\r\n\r\n            [values] => Array\r\n                (\r\n                    [0] => location1\r\n                    [1] => location2\r\n                    [2] => location3\r\n                )\r\n\r\n            [pipes] => WPCF7_Pipes Object\r\n                (\r\n                    [pipes:WPCF7_Pipes:private] => Array\r\n                        (\r\n                            [0] => WPCF7_Pipe Object\r\n                                (\r\n                                    [before] => location1\r\n                                    [after] => email1@nowhere.com\r\n                                )\r\n\r\n                            [1] => WPCF7_Pipe Object\r\n                                (\r\n                                    [before] => location2\r\n                                    [after] => email2@nowhere.com\r\n                                )\r\n\r\n                            [2] => WPCF7_Pipe Object\r\n                                (\r\n                                    [before] => location3\r\n                                    [after] => email3@nowhere.com\r\n                                )\r\n\r\n                        )\r\n\r\n                )\r\n\r\n            [labels] => Array\r\n                (\r\n                    [0] => location1\r\n                    [1] => location2\r\n                    [2] => location3\r\n                )\r\n\r\n            [attr] => \r\n            [content] => \r\n        )\r\n\r\n    [1] => Array\r\n        (\r\n            [type] => submit\r\n            [basetype] => submit\r\n            [name] => \r\n            [options] => Array\r\n                (\r\n                )\r\n\r\n            [raw_values] => Array\r\n                (\r\n                    [0] => Send\r\n                )\r\n\r\n            [values] => Array\r\n                (\r\n                    [0] => Send\r\n                )\r\n\r\n            [pipes] => WPCF7_Pipes Object\r\n                (\r\n                    [pipes:WPCF7_Pipes:private] => Array\r\n                        (\r\n                            [0] => WPCF7_Pipe Object\r\n                                (\r\n                                    [before] => Send\r\n                                    [after] => Send\r\n                                )\r\n\r\n                        )\r\n\r\n                )\r\n\r\n            [labels] => Array\r\n                (\r\n                    [0] => Send\r\n                )\r\n\r\n            [attr] => \r\n            [content] => \r\n        )\r\n\r\n)\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This comes form a user who is using pipe characters &#8220;|&#8221; 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 &#8220;|&#8221; character display to the user in the form: But the value that is sent [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1250,"menu_order":2,"comment_status":"closed","ping_status":"closed","template":"page-without-sidebar.php","meta":{"jetpack_post_was_ever_published":false,"footnotes":""},"class_list":["post-804","page","type-page","status-publish","hentry"],"jetpack_shortlink":"https:\/\/wp.me\/P1mptf-cY","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/804","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=804"}],"version-history":[{"count":33,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/804\/revisions"}],"predecessor-version":[{"id":1429,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/804\/revisions\/1429"}],"up":[{"embeddable":true,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/1250"}],"wp:attachment":[{"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=804"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}