Passing variables via URL to WordPress Posts and Pages
I was asked today if I knew of a way to pass a string to WordPress – specifically to add a variable to an Aweber form.
The main reason we want to pass a variable to an AWeber form is so we can capture an Affiliate name or ID for later tracking etc. I’ve used this method previously on html and php files, but not with WordPress – so I went to find out.
Essentially, what we wanted to do was have a url formatted as follows:
http://thewpwarrior.com/wordpress/616/passing-variables-via-url/?refid=Charly
The word ‘Charly’ would then be used to populate one of the hidden fields in my AWeber form.
My investigations led me to a post by Webopius, discussing custom URL paramaters in WordPress – which has proved incredibly helpful.
From this post, I took three steps to implement the functionality.
Part 1 – Add a new URL paramater to WordPress
I first added the following code to my functions.php file for my theme:
add_filter('query_vars', 'parameter_queryvars' );
function parameter_queryvars( $qvars )
{
$qvars[] = 'refid';
return $qvars;
}
(A note to say that this code is placed within the <?php and ?> tags in fuctions.php)
This specific code adds a single new URL parameter name ‘refid’ to WordPress.
Part 2 – Add A Function To Retrieve And Display The URL Paramater
The next part is to actually be able to get the passed variable from the URL and then display where ever you want.
I decided to implement this part as a function within my theme because I knew it was something that I would want to do many times and it’s neater to call a function, than duplicate the code multiple times.
In fuctions.php, I added:
function echorefid()
{
global $wp_query;
if (isset($wp_query->query_vars['refid']))
{
print $wp_query->query_vars['refid'];
}
}
(Remember, this code goes in between the <?php and ?> tags)
Step 3 – Display The Passed Variable In My AWeber Form Hidden Field
This part can be replicated for anything really – not just displaying the variable in the AWeber Form Hidden Field. However, I’m going to share how I added the passed variable to my AWeber form….
To be able to achieve this, we have to use the AWeber HTML code (not the javascript code). Once you’ve created your AWeber Form and you are at the ‘publish your form’ stage, click on the Raw HTML Version tab and copy the code that appears in there.
In the post or page you want to display your form, go to the HTML tab (not visual editor) and paste the code where you want the form to appear. (Please note, that there is a bit of work required when using the new formatted forms from AWeber, which is not covered here).
Once you’ve pasted the HTML, locate the line that looks like:
<input type="hidden" name="meta_adtracking" value="">
If that line doesn’t exist, add it between other lines in the form code…
Modify the line to look like:
<input type="hidden" name="meta_adtracking" value="<?php echorefid();?>">
The code:
<?php echorefid();?>
calls the function echorefid() (that you added to your functions.php file) and displays the value for refid that is passed in the URL.
