Capturing Page URL from CF7
This only applies if you are using Contact Form 7.
Situation: you have a form defined with Contact Form 7 and it appears on more than one page. You wish to capture the page URL in the form submission.
Solution (Experimental):
Using the technique described in Changing Form Data Before it is Saved create a filter like the following.
- Assumes that your site addresses posts using “?p=postid” in the URLs.
- Change the value of $urlBase in line 9 to be your site’s URL
- Change ‘page’ in line 10 to be the name of the new field to contain the URL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | function set_cf7_page_url($formData) { if ($formData && isset($formData->posted_data['_wpcf7_unit_tag'])) { // Parse out the post id from the _wpcf7_unit_tag field // example: [_wpcf7_unit_tag] => wpcf7-f590-p1533-o1 // where the post id is "1533" $pieces = explode('-', $formData->posted_data['_wpcf7_unit_tag']); if (count($pieces) >= 3) { $piece = substr($pieces[2], 1); $urlBase = 'http://blog.michael-simpson.com/?p='; $formData->posted_data['page'] = $urlBase . $piece; } } return $formData; } add_filter('cfdb_form_data', 'set_cf7_page_url'); |
Hi Michael! First of all, thank you for such a great and useful plugin
I just tried your solution and it works perfectly!!!
I was wondering if instead of parsing the wpcf7_unit_tag, is it possible to store the Page Title/Name? Like for example, using any of the CF7 Tags like these: http://contactform7.com/special-mail-tags/
I suppose that [_post_name] or [_post_title] should work, but I tried replacing _wpcf7_unit_tag with those tags and it didn’t work. I’d be grateful with your help to discover what I’m doing wrong. Have a great day!
I took a quick look at that. I put all those tags in the mail section of a default CF7 form. Then I took a look at the data structure that gets pushed to my plugin. Unfortunately, it lists those tags, but not their values.
I did this using the debug technique described on the page about filtering data at the bottom (I just updated that w/more info), I have the data structure writing to a log file. I encourage you to do the same so you can see all the data that is available and its format.
Hi Michael! Thanks for your answer
I did this: I created a php.ini file inside the plugin directory, inside this file I only added the path: error_log=/home/interexp/error_log.txt
The error_log is located at the same level than my public_html file.
Then I added the function to my functions.php file and sent the email. The form is working correctly but nothing happened with the error_log, nothing was recorded. Maybe I should be doing something wrong.
Is there any filer or tag like the _wpcf7_unit_tag that could be used to grab the ‘friendly-url’ instead of the page ID? Because it doesn’t need to be the Page Title, with the friendly URL everything would be solved
Thanks for your help again Michael.
Not sure what the problem is writing to the log file. Might try creating a blank file and ensuring write permission are set on it.
Here is an example of what I got from a form post. That should give you an idea of the kind of metadata you get.
[05-Dec-2012 20:39:02] Form Sumbission: WPCF7_ContactForm Object ( [initial] => [id] => 15 [title] => Contact form 1 [unit_tag] => [responses_count] => 0 [scanned_form_tags] => Array ( [0] => Array ( [type] => text* [name] => your-name [options] => Array ( ) [raw_values] => Array ( ) [values] => Array ( ) [pipes] => WPCF7_Pipes Object ( [pipes] => Array ( ) ) [labels] => Array ( ) [attr] => [content] => ) [1] => Array ( [type] => email* [name] => your-email [options] => Array ( ) [raw_values] => Array ( ) [values] => Array ( ) [pipes] => WPCF7_Pipes Object ( [pipes] => Array ( ) ) [labels] => Array ( ) [attr] => [content] => ) [2] => Array ( [type] => text [name] => your-subject [options] => Array ( ) [raw_values] => Array ( ) [values] => Array ( ) [pipes] => WPCF7_Pipes Object ( [pipes] => Array ( ) ) [labels] => Array ( ) [attr] => [content] => ) [3] => Array ( [type] => textarea [name] => your-message [options] => Array ( ) [raw_values] => Array ( ) [values] => Array ( ) [pipes] => WPCF7_Pipes Object ( [pipes] => Array ( ) ) [labels] => Array ( ) [attr] => [content] => ) [4] => 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 ( [_wpcf7] => 15 [_wpcf7_version] => 3.3.1 [_wpcf7_unit_tag] => wpcf7-f15-p16-o1 [_wpnonce] => 54af42248e [your-name] => MIke [your-email] => mike@nowhere.com [your-subject] => hi [your-message] => hi [_wpcf7_is_ajax_call] => 1 ) [uploaded_files] => Array ( ) [skip_mail] => [form] =>Your Name (required)
[text* your-name]
Your Email (required)
[email* your-email]
Subject
[text your-subject]
Your Message
[textarea your-message]
[submit "Send"]
[mail] => Array ( [subject] => [your-subject] [sender] => [your-name] < [your-email]> [body] => From: [your-name] < [your-email]> Subject: [your-subject] Message Body: [your-message] [_remote_ip] [_user_agent] [_url] [_date] [_time] [_post_id] [_post_name] [_post_title] [_post_url] [_post_author] [_post_author_email] -- This mail is sent via contact form on Test http://blog3.michael-simpson.com [recipient] => michael.d.simpson@gmail.com [additional_headers] => [attachments] => [use_html] => ) [mail_2] => Array ( [active] => [subject] => [your-subject] [sender] => [your-name] < [your-email]> [body] => Message body: [your-message] -- This mail is sent via contact form on Test http://blog3.michael-simpson.com [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. [spam] => Failed to send your message. Please try later or contact the administrator by another method. [accept_terms] => Please accept the terms to proceed. [invalid_email] => Email address seems invalid. [invalid_required] => Please fill the required field. [quiz_answer_not_correct] => Your answer is not correct. [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. ) [additional_settings] => [submit_time] => 1354739942.1937 [ip] => 209.183.232.44 [user] => msimpson )Thank you again Michael, I am going to review your code and see what I can do here. I’ll let you know. Regards!
Michael, I integrated your plugin with ‘Contact Form 7 – Dynamic Text Extension’ and it worked perfectly
Thanks a lot for all of your support!!