WHAT'S NEW?
Loading...

[Wordpress] Passing parameters (extra variables) in URL and read them from a page

Sometimes you need a simple way of passing a custom URL variable to WordPress. I will expose in this article my custom solution.

Create this php page and copy in plugin directory

<?php
/* Plugin Name: My Parameter
Plugin URI:  http://twitter.com/orfeomorello
Description: A plugin to allow parameter to be passed in the URL and recognized by WordPress
Author: Orfeo Morello
Version: 1.0
Author URI: http://twitter.com/orfeomorello
*/
add_filter('query_vars', 'parameter_queryvars' );
function parameter_queryvars( $qvars )
{
$qvars[] = 'myvar';
return $qvars;
}
?>

Install the Insert PHP plugin 

Activate the plugin and create a wordpress page "myexample" to read the variable:

[insert_php]
global $wp_query;
if (isset($wp_query->query_vars['myvar']))
{
$myvar= urldecode($wp_query->query_vars['myvar']);
print myvar;
}
[/insert_php]

From the browser call the page
http://www.mywebsite.com/myexample/?myvar=123456



0 comments:

Post a Comment