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 am trying to put variables into the URL, which I can then use in my Wordpress template. So for example I have created a wordpress template something.php and the page "somepage" with this template. Now in my something.php I am trying to get a variable (for example something.php?variable=whatever) into my script in order to use it there. Im doing this with $_GET["variable"]. Now everything worked fine on the test server. Now I have put it onto the real server (which is actually the same server, just another copy of wordpress) and there it doesnt work. Opening the URL www.example.com/somepage?variable=whatever will directly lead to www.example.com/somepage. So Wordpress is removing every variable in the URL. Another thing that doesnt work and that probably has to do with the same problem is a search, which I created and which has the post type "post", not "get". What do you think could be the problem, since it worked perfectly fine on the same server with another wordpress copy (same version).

Thank you very much for your help! phpheini

share|improve this question
    
Did you really name it variable? I guess in this case beeing specific won't much of a failure. –  kaiser Feb 28 '11 at 22:09
1  
The reason that occurs is because your query variables are not actually the first in the URL, the page name is, apache just rewrites the URL so you can't see the real URL, eg. example.com/index.php?pagename=somepage. So in essence what you end up with is example.com/index.php?pagename=somepage?var=val which gets ignored because the ? is invalid after the first. Golden Apples has you covered in his answer.. ;) –  t31os Feb 28 '11 at 22:24
    
But wouldnt that mean that writing example.com/index.php?pagename=somepage&var=val (replaced the exclamation mark) should be working? But it doesnt. And how come it does work exactly like I wrote on my second copy of wordpress? –  user3585 Mar 1 '11 at 12:03

1 Answer 1

For one, you can't call template files directly as you're doing... If you want the page somepage, you should call it directly, with the query string arguments added after a trailing slash.

Look into the function add_query_arg() - its a much more bulletproof way of tacking GET variables onto the end of WordPress urls. For example, if you need to call www.example.com/somepage with the query string ?variable=whatever, try calling

add_query_arg( 'variable', 'whatever', 'http://www.example.com/somepage' )

to get the proper url to use. This guarantees getting the proper url in a format that won't clash with your rewrite rules.

share|improve this answer
    
But I guess add_query_arg will only work within the loop, no? Because I am not using the loop and usually I can not use the functions of Wordpress. –  user3585 Mar 1 '11 at 12:03
    
No you don't have to be within the loop to use add_query_arg (), any time the WP environment has loaded it should work. In any case, you can try echoing its output to see what form your url should be in, if you need to get the url to use outside of WP. Usually its as simple as adding a trailing slash before your query string, but different rewrite rules and different server environments can change that. –  goldenapples Mar 1 '11 at 16:00
    
Thank you, but there is one thing I dont understand. What will I do now when I want to have a link from one side to example.com/somepage?id=2 and then get this number 2 into my script. Using add_query_arg wont give me the ID, but it will only give me whatever I type into the function (in this case 'whatever'). I cant open the URL www.example.com/somepage?id=2 or www.example.com/somepage/?id=2 although in the example here it is possible: codex.wordpress.org/Function_Reference/add_query_arg –  user3585 Mar 1 '11 at 19:40

Your Answer

 
discard

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