0

I am designing a theme for wordpress, but when adding query to the url, the problem occur:

Both two situation, I use $_GET['var'] in WP->parse_request(..) in file class-wp.php:

It works when the url is like this "loscalhost/?var=123", $_GET['var'] return 123;

It does not work when the url is like this "localhost/category/test/?var=123", $_GET['var'] return null.

Can someone tell me What's wrong with this?

chentingpc
  • 1,283
  • 3
  • 18
  • 24
  • Possible dupe: http://stackoverflow.com/questions/4586835/how-to-pass-extra-variables-in-url-with-wordpress – Matt Dodge Mar 25 '13 at 16:21
  • Thanks. But as I carefully go through that post, I didn't find any real solution there, and I have also go through the wordpress query catching and parsing part, the real problem is that "you can't receive null $_GET when the url is not like http://example/?var=.., even in WP root index file". I used a very sloppy method (as below) to solve it.. – chentingpc Mar 26 '13 at 02:32

2 Answers2

1

WordPress is designed to ignore any URL query parameters that it does not expect. If you want to use a query variable, then you must inform WordPress to expect it. In your case:

function filter_add_query_vars($query_vars)
{   
    $query_vars[] = 'var'; 
    return $query_vars;
}
add_filter( 'query_vars', 'filter_add_query_vars' );

Put the code in your theme's functions.php

KalenGi
  • 1,766
  • 4
  • 25
  • 40
  • Thanks for the tip, actually I have done it.. I also try to use the rewrite rule, it also wouldn't work... it's weird that it works when the url is single level, but failed to work when multi-level.. – chentingpc Mar 26 '13 at 01:57
0

This is a sloppy solution: Since I can even use the $_GET to receive the variable in URL such as "localhost/category/test/?var=123" in the wp-blog-header file, but I can use the $_SERVER['REQUEST_URI'] to retrieve the whole URL, so I change the code in wp-blog-header.php. I change

wp();

to be:

$url_array = explode('?', $_SERVER['REQUEST_URI']);
wp($url_array[1]);

It works, since wp() also receive extra queries.

But I still don't understand why $_GET does not work, so I also wait for a valid explanation...

chentingpc
  • 1,283
  • 3
  • 18
  • 24