I am having trouble trying to pass an extra variable in the url to my wordpress installation.
For example /news?c=123
For some reason, it works only on the website root www.example.com?c=123
but it does not work if the url contains any more information www.example.com/news?c=123
. I have the following code in my functions.php file in the theme directory.
if (isset($_GET['c']))
{
setcookie("cCookie", $_GET['c']);
}
if (isset($_SERVER['HTTP_REFERER']))
{
setcookie("rCookie", $_SERVER['HTTP_REFERER']);
}
Any Ideas?
This was the only way I could get this to work
http://codex.wordpress.org/Function_Reference/add_query_arg
You can add any page inplace of "news".
There are quite few solutions to tackle this issue. First you can go for a plugin if you want:
Or code manually, check out this post:
Also check out:
add_query_arg
add following code in function.php
then you will b able to use $_GET['var1']
Since this is a frequently visited post i thought to post my solution in case it helps anyone. In WordPress along with using query vars you can change permalinks too like this
For this you have to add these lines of code in functions.php or your plugin base file.
From shankhan's anwer
And additionally this snipped to add custom rewriting rules.
For the case where you need to add rewrite rules for a specifc page you can use that page slug to write a rewrite rule for that specific page. Like in the question OP has asked about
We can change it to the desired behaviour by adding a little modification to our previous function.
Hoping that it becomes useful for someone.
One issue you might run into is
is_home()
returns true when a registered query_var is present in the home URL. For example, ifhttp://example.com
displays a static page instead of the blog,http://example.com/?c=123
will return the blog.See https://core.trac.wordpress.org/ticket/25143 and https://wordpress.org/support/topic/adding-query-var-makes-front-page-missing/ for more info on this.
What you can do (if you're not attempting to affect the query) is use
add_rewrite_endpoint()
. It should be run during theinit
action as it affects the rewrite rules. Eg.This should give you access to
$_GET['c']
when the url contains more information likewww.example.com/news?c=123
.Remember to flush your rewrite rules after adding/modifying this.