Contact form 7 set field value with get request

62,302

Solution 1

I just installed the plugin you linked and tested it. The plugin isn't meant to pull in a GET variable for a field within Contact Form 7. The plugin will do two things

  1. It will grab the $_GET variable and create a hidden field with it.
  2. It will show the variable on the page (just as text, not in a field)

The shortcode that you have in your example is for use by this http://wordpress.org/plugins/contact-form-7-dynamic-text-extension/ plugin. I downloaded and tested that plugin as well, and it seems to work just fine.

Here is the page I created the example on. http://jtwebb.com/stackoverflow-question/?someemail=asdf if you want to take a look to see it is working with the dynamic-text-extension plugin.

UPDATE: This is my contact form 7 code:

<p>Your Name (required)<br />
    [text* your-name] </p>

<p>[showparam someemail] <-- this is the shortcode of show param, just text no field</p>

<p>[getparam someemail] If you inspect this you'll see a hidden get field with the value of 'someemail' in it.</p>

<p>Your Email (required)<br />
    [dynamictext* dynamictext-380 "CF7_GET key='someemail'"]<br>This field was created with <a href="http://wordpress.org/plugins/contact-form-7-dynamic-text-extension/">http://wordpress.org/plugins/contact-form-7-dynamic-text-extension/</a></p>

<p>Subject<br />
    [text your-subject] </p>

<p>Your Message<br />
    [textarea your-message] </p>

<p>[submit "Send"]</p>

Solution 2

[text* your-name default:get] The field will obtain its default value from the GET variable with the same name (“your-name”). Try this by accessing the URL of the page of the form with an additional query string:

http://example.com/contact/?your-name=John+Smith

But what if you have two or more default options in a single form-tag? Let’s consider this form-tag’s case:

[text* your-name default:get default:post_meta "Your Name"]

Solution 3

You can initially in the form field set a unique text, and then use the hook to replace it with the desired value. And then no plugin will be needed.

Example. In form code:

<p>Phone<br />
[text phone "PHONE_VALUE"] </p>

in functions.php:

add_filter( 'wpcf7_form_elements', function( $form ) {
  $form = str_replace( 'PHONE_VALUE', $_GET['phone'], $form );
  return $form;
} );

in URL:

example.com/page?phone=111111

Solution 4

I know this question has already been answered but for anyone looking for a solution which doesn't require a plugin I opted to do the following.

First I created my form from within the plugin via the Wordpress dashboard. I added the field I wanted to hold the parameter from URL and assigned it an ID.

[text page-name class:form-control id:source minlength:2 maxlength:80]

Next I added a link which would pass the parameter to the form like so

<a href='http://mycoolwebsite.com/contact?id=homepage'>Contact Us</a>

Then using some Javascript and JQuery I get the id parameter from the URL and set it as the value for my input in the form. (The getParameterByName(name,url) function was taken from this answer: How can I get query string values in JavaScript?)

function getParameterByName(name, url) {
    if (!url) url = window.location.href;
    url = url.toLowerCase(); 
    name = name.replace(/[\[\]]/g, "\\$&");
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, " "));
}
var param = getParameterByName('id');
jQuery('#source').hide();
jQuery('#source').prop('disabled', true);
jQuery('#source').attr('value', param);
jQuery('#source').val(param);

I also hide and disable the input field so it is not seen and cannot be modified (easily). I also hide the input field using CSS

#source{visibility:hidden}

This way I can link to the form from any where within my site and append the source of where the person came from and place it in the email that I get.

I don't see any problems with this method and it removes the need to use a plugin. I'm sure it's not ideal to depend on Javascript but equally it's not ideal to use to many plugins on your site as they can become outdated very quickly and often can cause conflicts between one another.

Hope this helps anyone looking for an alternative. I'd like to know peoples opinions on this way as I'm open to suggestions on how to improve it.

Solution 5

as per this article

if you want to send a value to a select list, this can be done easily by adding the parameter default:get to the field code:

[select* the-recipient default:get "Employee One|[email protected]" "Employee Two|[email protected]" "Employee Three|[email protected]"]

and then send the parameter in the GET request, like:

http://yourdomain.com/contact/?the-recipient=Employee%20Two
Share:
62,302
Meneer Venus
Author by

Meneer Venus

Updated on November 29, 2020

Comments

  • Meneer Venus
    Meneer Venus over 3 years

    I want to set a field value with get request

    example.com/subscribe/?email=asfafs

    but when i load the page which has the form on it, the form does not show. I get why it could not be showing. Because the form itself could submit a get request as well. I also installed this plugin which should enable me to set a field value, but it does not.

    this is what i have:

    <p>Uw naam (verplicht)<br />
        [text* input-name] </p>
    
    <p>Uw email (verplicht)<br />
        [dynamictext dynamicname "CF7_GET key='email'"] </p>
    <p>Onderwerp<br />
        [text your-subject] </p>
    
    <p>Uw bericht<br />
        [textarea your-message] </p>
    
    <p>[submit "Verzenden"]</p>
    

    In my page:

    <?php echo do_shortcode('[contact-form-7 id="1062" title="Contactformulier 1"]'); ?>
    

    I wouldn't mind using a different plugin for this. If there is one which suits my needs please tell me.

  • Meneer Venus
    Meneer Venus almost 10 years
    Thank you. I kind of figured this out myself. I just had to tweak the code of the dynamic text extension plugin a bit.
  • Jules Colle
    Jules Colle almost 4 years
    This should be the accepted answer. This is documented on the official CF7 site: contactform7.com/getting-default-values-from-the-context