Support » Plugins » Passing variables using the permalink structure

Viewing 3 replies - 1 through 3 (of 3 total)
  • Adding GET params to a URL in WordPress with add_query_arg
    I am finding myself using this function a lot now, and I constantly forget the function name:
    It allows you to pass in some additional parameters, and a URL, and receive back the URL with the query string params added. For example:

    $some_url = "";
    $params = array( 'wp_siteurl' => "" );
    $some_url = add_query_arg( $params, $some_url );

    This will result in a url something like this:
    Pretty cool, huh? So now you don’t need to worry about what parameters are already in the URL, you can just hand all that hassle to add_query_arg.

    Using custom URL parameters in WordPress
    Sometimes when you are building WordPress sites you need to pass a parameter via the URL like this:
    The problem is that WordPress is designed to reject any URL query parameters that it doesn’t recognise so your URL parameter will be dropped before you get a chance to use it.
    The solution
    Step 1
    We need to tell WordPress about the new parameter(s) we will be sending via the URL. We want WordPress to recognise any url parameter sent in the format ‘’ in any page on our WordPress site.
    The easiest way to do this is to create a WordPress plugin that uses a query filter to tell WordPress about new parameters. Here’s the plugin code:

    /* Plugin Name: Parameter
    Plugin URI:
    Description: A plugin to allow parameters to be passed in the URL and recognized by WordPress
    Author: Adam Boyse
    Version: 1.0
    Author URI:
    add_filter('query_vars', 'parameter_queryvars' );
    function parameter_queryvars( $qvars )
    $qvars[] = ' myvar';
    return $qvars;

    The plugin is configured to add one new URL parameter name ‘myvar’ to WordPress. Just copy the above code to a new .php file which you then copy to the plugins directory of your WordPress install. You then need to activate the new plugin from within your WordPress admin screens.

    Step 2.
    Now from any WordPress page (e.g. Theme page) or standalone page that is WordPress aware you can use your variable like this:
    global $wp_query;

    if (isset($wp_query->query_vars['myvar']))
    print $wp_query->query_vars['myvar'];

    Thank you for having responded
    but I’m trying to pass variables using the structures permalink


    that’s my problem?

    Your answer helped me a lot! 😉

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Passing variables using the permalink structure’ is closed to new replies.