Have you ever tried to tweak your wordpress website in a way that requires you to pass parameters to a page? Perhaps you have tried to embed a piece of 3rd-party code into your page and found that it doesn’t work because the code requires a url parameter to have been passed to it – and the parameter seems to have vanished as the page loads?

The reason for this is that WordPress automatically strips out parameters it doesn’t recognise before it builds your pages. The solution is to trick WordPress into accepting your parameters as though they were integral to the framework. I couldn’t find an ‘official’ wordpress plugin to allow me to do this, but came across this neat solution….

http://www.webopius.com/content/137/using-custom-url-parameters-in-wordpress

You will need to edit the template file which loads the page you want to receive the parameter, and you will need to upload some code as a plugin to name the variable(s) that you want to use, but it seems to work well.

Here is the plugin code, which needs to get uploaded to your plugins folder and activated….

<?php
/* Plugin Name: Parameter
Plugin URI: http://webopius.com/
Description: A plugin to allow parameters to be passed in the URL and recognized by WordPress
Author: Adam Boyse
Version: 1.0
Author URI: http://www.webopius.com/
*/
add_filter('query_vars', 'parameter_queryvars' );
function parameter_queryvars( $qvars )
{
$qvars[] = 'yourvarname';
return $qvars;
}
?>

…and here is an example of how to use it in your template page….

global $wp_query;
if (isset($wp_query->query_vars['yourvarname']))
{
print $wp_query->query_vars['yourvarname'];
}

You need to change yourvarname to the name of the variable your page expects to receive.

Tags: , , ,