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.