WordPress.org

Ready to get started?Download WordPress

Forums

Contact Form 7 Input Fields Values as PHP Get-Viarables (33 posts)

  1. TylorFamous
    Member
    Posted 4 years ago #

    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?

  2. mrchambers
    Member
    Posted 4 years ago #

    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,
    Matt

  3. tenaciousJk
    Member
    Posted 4 years ago #

    This 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.

  4. micacomputers
    Member
    Posted 4 years ago #

    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.

  5. egjweb
    Member
    Posted 4 years ago #

    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?

  6. fishnick
    Member
    Posted 4 years ago #

    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)
    Thanks

  7. autotransport
    Member
    Posted 4 years ago #

    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/

  8. autotransport
    Member
    Posted 4 years ago #

    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/

  9. floydman
    Member
    Posted 4 years ago #

    @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
    Floyd

  10. billbailey25
    Member
    Posted 4 years ago #

    @floydman

    The line you would replace be around line 80 looks like this:

    $value = isset( $values[0] ) ? $values[0] : '';

  11. 5hot6un
    Member
    Posted 4 years ago #

    @mrchambers -

    Excellent! Your solutions works perfectly! Thank you for posting!

  12. bt-chadski
    Member
    Posted 4 years ago #

    @mrchambers,

    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.

  13. juliedc
    Member
    Posted 4 years ago #

    Hello, I am getting parse errors when I try mrchambers fix. Any ideas? Thanks!

  14. 5hot6un
    Member
    Posted 4 years ago #

    I've updated to the latest version of Contact Form 7.
    The fix mrchambers posted no longer works

  15. 5hot6un
    Member
    Posted 4 years ago #

    <ahem> I apologize. After updating Contact Form 7, you have to "reapply" the fix in text.php

  16. shirgans
    Member
    Posted 4 years ago #

    @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

  17. dibbit
    Member
    Posted 4 years ago #

    I came up against this problem using Contact Form 7 - I needed to include a custom field value in the subject of the form. I added this simple fix to the file modules\text.php. First find this code at line 76 on:

    // Value
    	if ( is_a( $wpcf7_contact_form, 'WPCF7_ContactForm' ) && $wpcf7_contact_form->is_posted() ) {
    		if ( isset( $_POST['_wpcf7_mail_sent'] ) && $_POST['_wpcf7_mail_sent']['ok'] )
    			$value = '';
    		else
    			$value = stripslashes_deep( $_POST[$name] );
    	} else {
    		$value = isset( $values[0] ) ? $values[0] : '';
    	}

    I then added the following lines:

    if ( $name == 'your-subject' )
    	{
    		global $post;
    		$result = get_post_meta($post->ID, 'Reference', true);
    		$value = "Ref: " . $result ;
    	}

    My hack places the value of the Custom Field 'Reference' as the subject line, so when a user sends a contact form, it contains the reference number of the property they are looking at. If you want to use this with a different custom field, just change the name 'Reference' to the one you want.

    If you want to see it in action have a look at the site:

    http://www.nerja-holidayrentals.com/

    Hope this is of use to someone else.

  18. one3rdnerd
    Member
    Posted 4 years ago #

    Hi Dibbit, I wanted to do the same but cannot get this to work. Do you paste that code directly after? or instead of? Im unsure. Thanks.

    Also I like the idea of being able to use a custom field to control the TO: field of the contact form, so I can have contact form 1 in my template file and set the agents email on a post for post basis using the custom meta box I create. Any ideas?

  19. skoskie
    Member
    Posted 4 years ago #

    In regards to making the fields hidden, just use css.

    [text crc_header id:crc_notification class:hidden "GET_crc"]

    …and then in the style.css file just add …

    .hidden {
         display:hidden;
         }
  20. one3rdnerd
    Member
    Posted 4 years ago #

    Good call Skoskie, however what if the field is already hidden? have you played much with custom meta boxes yet?

  21. samisave
    Member
    Posted 4 years ago #

    I tried following autotransport...

    but

    When sending this url to my formpage:
    http://mywebsite.dk/site/?page_id=881&Annonce-overskrift=Here is the text

    instead of writing the variable in my txt field i just get the text <?php echo $_GET['Annonce-overskrift']; ?> written inside the text field.

    Anyone knows why?

  22. Casemon
    Member
    Posted 4 years ago #

    Any chance of getting this behavior rolled into the release code?

    Having to manually add this feature with every release is tiresome and counter productive to using 3rd party plugins.

  23. richarduk
    Member
    Posted 3 years ago #

    I'm trying to get the last part of the referring page url to show in the form that's sent to admin so that the administrator has a good idea of what triggered off the contact request. I'm thinking of using either a cookie or http_referrer, though http_referrer is apparently not reliable and cookies of course can be turned off.

    Anyone been here before and got a better idea or a snippet of code that will save me some time?

  24. bt-chadski
    Member
    Posted 3 years ago #

    richarduk,
    I am after the same, please post your finding afterwards.

    Also,
    I learned you can use Gravity Forms + plugin from Yoast.com: http://yoast.com/gravity-forms-widget-extras/

  25. richarduk
    Member
    Posted 3 years ago #

    Same with Cforms 2, very powerful (and complicated) and free. Like Gravity Forms it has an option to do stuff with referrer data.

    http://www.deliciousdays.com/cforms-plugin/

  26. bt-chadski
    Member
    Posted 3 years ago #

    richarduk,
    thank you for that!

    i didn't see any info specifically about how to implement referrer tracking. Do you know how to do that?

  27. richarduk
    Member
    Posted 3 years ago #

    It's not well documented on the site but once installed there's a ton of documentation within the plugin.

    On the plugin home page:

    API / Creating dynamic forms

    Do you require more flexibility in your forms? Then create and deploy forms at run-time. Mind you, to use this feature it requires basic and possibly more advanced PHP knowledge, examples you’ll find in the HELP! section of plugin admin pages.

    This will essentially allow you to pull data from an external source and feed it into your ad-hoc created form fields. I’ve deliberately not included any sort of external data administration, as everyone will have a different file or database table syntax. Any of the fields described in the HELP! section can be created at run-time and shown online. Minor drawback: this works only in non-Ajax mode.

  28. richarduk
    Member
    Posted 3 years ago #

    I've got nowhere. I post this in the hope someone might cast some light on it. This is to go either in functions.php or header.php
    Page('3') is the contact page. Note that echoing things would show referring urls etc. nicely at the top of the page but stopped the contact form from submitting.

    <?php global $filename;
             if (is_page('3')) {
    if(!isset($HTTP_REFERER))
    {$referer = strtolower($_SERVER["HTTP_REFERER"]);
    //echo 'TESTING TESTING';
    //echo $referer;
    $url2 = $referer;
    $filename = basename($url2);
     //echo $filename;
    }
     } ?>

    Then, in classes.php in the plugin, right before the line

    return @wp_mail( $recipient, $subject, $body, $headers, $for_this_mail );

    I added this:

    $subject = $subject.$filename;

    Since that didn't work, I removed the code from either header.php or functions.php and from classes.php and tried adding this instead :

    $filename = wp_get_referer();
    $subject = $subject.$filename;
    
    return @wp_mail( $recipient, $subject, $body, $headers, $for_this_mail );

    All the time my emails came back with the same damned referrer, whch was the name of the page the contact form was on i.e. contact.php

    Is functions.php loaded before plugins?

    If so, I can't see why this didn't work.

  29. esmi
    Forum Moderator
    Posted 3 years ago #

    @richarduk: That was a little OTT, don't you think? A modlook tag and a discrete pointer towards the offending post is all that is normally needed.

  30. richarduk
    Member
    Posted 3 years ago #

    I have no idea what a modlook tag is :-)

    I'll google it now

Topic Closed

This topic has been closed to new replies.

About this Topic