Contact Form 7 Input Fields Values as PHP Get-Viarables
-
Hello,
I am using the Contact form 7 plugin, and what I would like to do is have the default text for a input field, be generated via query string.
So I would think that the code would look something like this:
[text* postID “<?php echo $_GET[‘postid’];?>”]and the url would obviously end in ?postid=3
But that causes the text fied on the page to crash. Any ideas how to accomplish this?
-
Tylor,
I’ve written a hack for this as I had the same requirement and could not find a solution anywhere! I’m not a WordPress expert so hopefully someone who is can take this and refine it. It does work though π
in plugins/contact-form-7/modules open up text.php.
find the line:
$value = $values[0];
(about line 73)replace with:
$match = strpos($values[0], 'GET_'); if ($match === false) { $value = $values[0]; } else { $values[0] = str_replace('GET_', '', $values[0]); if (isset($_GET[$values[0]])) { $value = $_GET[$values[0]]; } }
Save the file.
In the contact form admin area, when you have a field that requires a GET var to appear insert ‘GET_’ directly before the variable name – ie;
[text your-name "GET_ref"]
If the GET var exists it will appear otherwise the field will be left blank!
There’s no support with this but I hope it solves your problem until a better option becomes available π
Thanks,
MattThis works great – Thanks Matt!
Any idea how to get something working on a select statement?
[select* VARIABLE include_blank "one" "two" "three" "four" "GET_VARIABLE"]
In the case above the GET_VARIABLE ends up being treated as one of the select options. If you put it prior to the include_blank it breaks the select box.
Thanks Matt,that works great and put me onto the track of adapting this to my own wishes. I needed to pass parameters to the form but could not use the URL; what I’ve done below is slightly adapt your code so I can pass parameters to text fields using WP’s ‘Custom Fields’.
What I do is add a custom field to a post, let’s say with the name “Itin” (for Itinerary) where the key will hold the Itinerary-code I want to pass to the form. Then, I add “CF_fieldname” to the text-field definition in the contact form. So for instance, to get the ‘Itin’ custom field into the ‘itin-code’ textfield, I can use:
Itinerary code: [text itin-code 30/30 "CF_Itin"]
That way, any custom field can be put in any text field. The adapted code (replacing, as above:
$value = $values[0];
with:$match = strpos($values[0], 'CF_'); if ($match === false) { $value = $values[0]; } else { global $post, $key; $values[0] = str_replace('CF_', '', $values[0]); $key = $values[0]; $value = get_post_meta($post->ID, $key, true); } </code></p>
Though in no way beautiful (not a PHP programmer) it works, and might be useful to others.
Niels.im trying to get this to work with the [select] dropdown box, instead of text field, but it doesnt seem to work. Anyone can help?
Really could use a mod to get on of the values in dropdown to be preselected according to custom field value (if exists and/or matches one)
ThanksA good solution is to use “Id” in Contact Form 7 values.
1. Set up an html form that will pass variables
<form action="URL_TO_CONTACT_FORM_7_PAGE" method="get"> <select name="YOUR_VALUE" id="YOUR_VALUE"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> </select> </form>
2 Set up a Contact Form 7 element with “id”:
[select YOUR_VALUE id:YOUR_VALUE "1" "2" "3" ]
3. Use javascript on the page, and php (use exec php plugin or similar to execute php code)
[CONTACT FORM 7 TAG] <script type="text/javascript"> document.getElementById('YOUR_VALUE').value = "<?php echo $_GET['YOUR_VALUE'];?>"; </script>
See it in action here:
http://www.crowleyautotransport.com/A good solution is to use “Id” in Contact Form 7 values.
1. Set up an html form that will pass variables
<form action="URL_TO_CONTACT_FORM_7_PAGE" method="get"> <select name="YOUR_VALUE" id="YOUR_VALUE"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> </select> </form>
2 Set up a Contact Form 7 element with “id”:
[select YOUR_VALUE id:YOUR_VALUE "1" "2" "3" ]
3. Use javascript on the page, and php (use exec php plugin or similar to execute php code)
[CONTACT FORM 7 TAG] <script type="text/javascript"> document.getElementById('YOUR_VALUE').value = "<?php echo $_GET['YOUR_VALUE'];?>"; </script>
See it in action here:
http://www.crowleyautotransport.com/@mrchambers
your hack would be the solution for my problems. The only problem I have right now: in my file “text.php” is no line like the one you posted:$value = $values[0]; (about line 73)
So what to replace now? π What version of Contact Form 7 do you use? I downloaded the latest 2.2 last updated 2010-3-22. I am using WordPress 2.9.2.
Thanks for your help
FloydThe line you would replace be around line 80 looks like this:
$value = isset( $values[0] ) ? $values[0] : '';
Excellent! Your solutions works perfectly! Thank you for posting!
Would you know how to pass variables to each submission that will let me know visitor’s cookie/session information, specifically – how did they originally get to the site? I am trying to track conversion by source. Thank you.
Hello, I am getting parse errors when I try mrchambers fix. Any ideas? Thanks!
I’ve updated to the latest version of Contact Form 7.
The fix mrchambers posted no longer works<ahem> I apologize. After updating Contact Form 7, you have to “reapply” the fix in text.php
@mrchambers – thanks for the advice. It’s really worked.
I made some small changes additionally to yours, to allow hidden fields. on text.php:add at top:
wpcf7_add_shortcode( 'hidden', 'wpcf7_text_shortcode_handler', true );
there is need to make a change in two “if” statements at lines 36 and 39, so the whole part will look like the following:
if ( 'email' == $type || 'email*' == $type || 'hidden' == $type) $class_att .= ' wpcf7-validates-as-email'; if ( 'text*' == $type || 'email*' == $type || 'hidden' == $type) $class_att .= ' wpcf7-validates-as-required';
and finally, the line:
$html = '<input type="text" name="' . $name . '" value="' . esc_attr( $value ) . '"' . $atts . ' />';
change into:
if ($type == 'hidden'){ $html = '<input type="hidden" name="' . $name . '" value="' . esc_attr( $value ) . '"' . $atts . ' />'; } else { $html = '<input type="text" name="' . $name . '" value="' . esc_attr( $value ) . '"' . $atts . ' />'; }
now you just add to your form the following line:
[hidden get-field-name "GET_fieldname"]
That’s it. hope it helps
- The topic ‘Contact Form 7 Input Fields Values as PHP Get-Viarables’ is closed to new replies.