Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a WP plugin which uses the request parameter "type" to determine what code-file to load in order to do some processing. The code which handles this is as follows:

add_shortcode ('myplugin_frontend_main', 'MyPlugin_user_dispatcher');

function MyPlugin_user_dispatcher ($args=array())
{
    $type = $_GET['type'];
    $func = $_GET['func'];

    $typefile = ucwords($type). '.php';
    require_once(dirname(__FILE__) . "/$typefile");
    $class  = 'MyPlugin_Controller_' . ucfirst($type);
    $object = new $class();
    echo $object->$func($args);
}

The strange thing is this, if I call

http://localhost/wordpress/?page_id=4&type=userform&func=edit&noheader=true&ot=product_add&id=14&store=1

and add

var_dump ($_GET);
exit ();

to the code, my var_dump() prints out the following:

array
  'page_id' => string '4' (length=1)
  'type' => string 'user' (length=4)
  'func' => string 'main' (length=4)
  'store' => string '1' (length=1)

So somehow, $_GET does not contain what I call through the address bar (or by clicking on a link with the same content).

Does anybody have any idea what's going on here?

Greetings/Thanks R

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

WordPress will typically filter off query arguments unless you tell it about those arguments and ask it not to.

From this older tutorial, you can see how to add a new query variable to WordPress (so it won't get stripped out):

add_filter('query_vars', 'parameter_queryvars' );
function parameter_queryvars( $qvars )
{
    $qvars[] = 'myvar';
    return $qvars;
}

There is an alternative suggestion in the first answer of this question:

//add author_more to query vars
function add_author_more_query_var() {
    global $wp;
    $wp->add_query_var('author_more');
}

add_filter('init', 'add_author_more_query_var');

Once you do that, though, you need to also be aware of some reserved terms within WordPress. There are just some terms you can't use for custom data because they're already used elsewhere. The full list of reserved terms is in the Codex.

A quick glance shows that "type" is in the reserved list ... which could be why it's changing when you try to use it.

share|improve this answer
    
Thank you so much, I will use your suggestions to fix my code first thing tomorrow morning. –  user13955 Jul 10 '12 at 22:28
    
Please allow me one more clarifying point: this is only for GET parameters, right. I can put anything through post without registering that parameter name, right? –  user13955 Jul 10 '12 at 23:17
    
So far as I know, yes. But you'll still want to avoid anything in the reserved terms list. –  EAMann Jul 10 '12 at 23:20
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.