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:
    add_query_arg();
    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 = "http://simonwheatley.co.uk/?stuff=whatever";
    $params = array( 'wp_siteurl' => "http://www.example.com" );
    $some_url = add_query_arg( $params, $some_url );

    This will result in a url something like this:
    http://domain.com/?stuff=whatever&wp_siteurl=http%3A%2F%2Fwww.example.com
    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
    http://www.webopius.com/content/137/using-custom-url-parameters-in-wordpress
    Sometimes when you are building WordPress sites you need to pass a parameter via the URL like this:
    http://www.mysite.com?myvar=222
    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 ‘http://www.yoursite.com?myvar=hello’ 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:

    <?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[] = ' 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

    would:

    http://mysite.it/mypage/myvar/Hello-this-is-value-vars

    that’s my problem?

    Thanks!
    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.