PHP - How To Pass Extra Variables in URL With Wordpress - Stack Overflow
PHP - How To Pass Extra Variables in URL With Wordpress - Stack Overflow
How to pass extra variables in URL with Wordpress
I am having trouble trying to pass an extra variable in the url to my wordpress installation.
For example /news?c=123
if (isset($_GET['c']))
{
setcookie("cCookie", $_GET['c']);
}
if (isset($_SERVER['HTTP_REFERER']))
{
setcookie("rCookie", $_SERVER['HTTP_REFERER']);
}
Any Ideas?
edited Jan 3 '11 at 17:48 asked Jan 3 '11 at 17:44
ajreal Chuck D
39.3k 10 67 109 693 3 12 22
8 Answers
Não encontrou uma resposta? Pergunte em Stack Overflow em Português. ✕
There are quite few solutions to tackle this issue. First you can go for a plugin if
you want:
WordPress Quickie: Custom Query String Plugin
Or code manually, check out this post:
Passing Query String Parameters in WordPress URL
Also check out:
add_query_arg
answered Jan 3 '11 at 17:52
Sarfraz
272k 57 437 530
To make the round trip "The WordPress Way" on the "frontend" (doesn't work in the context of
Join Stack Overflow to learn, share knowledge, and build your career.
wp-admin ), you need to use 3 WordPress functions: Email Sign Up OR SIGN IN WITH Google Facebook
add_query_arg() to create the URL with your new query variable ('c' in your example)
the query_vars filter to modify the list of public query variables that WordPress knows
about (this only works on the frontend, because the WP Query is not used on the back
end wp-admin so this will also not be available in admin-ajax )
get_query_var() to retrieve the value of your custom query variable passed in your URL.
On the page where you need to create the link / set the query variable:
if it's a link back to this page, just adding the query variable
if it's a link to some other page
In your functions.php, or some plugin file or custom class (frontend only):
On the page / function where you wish to retrieve and work with the query var set in
your URL:
On the Back End ( wp-admin )
In this case, you'll need to revert to the more standard approach of examining your $_GET
superglobal. The best way to do this is probably:
though in a pinch you could do the tried and true
or some variant thereof.
edited May 19 '16 at 19:57 answered Aug 21 '13 at 16:03
Tom Auger
9,560 13 61 77
This is a great answer, been looking ages for a simple example like this, thanks – Sam Skirrow Nov 30 '15
at 9:56
Great description! Does wordpress strip out unregistered query arguments (it seems to be for me, but I'm
not totally sure what's going on)? it would be good to state this if so, to alleviate confusion :) – Damon Apr 6
'16 at 0:47
It doesn't take them out of $_GET, but they're not available to your rewrite rules or within your
$query_vars – Tom Auger Apr 6 '16 at 13:07
1 @electroid modified my answer to reflect your best practice. – Tom Auger May 19 '16 at 19:00
add following code in function.php
then you will b able to use $_GET['var1']
answered Jan 3 '11 at 17:52
shankhan
4,419 2 12 19
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
www.example.com?c=123 to www.example.com/c/123
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.
function custom_rewrite_basic()
{
add_rewrite_rule('^c/([0-9]+)/?', '?c=$1', 'top');
}
add_action('init', 'custom_rewrite_basic');
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
www.example.com/news?c=123 to www.example.com/news/123
We can change it to the desired behaviour by adding a little modification to our previous
function.
function custom_rewrite_basic()
{
add_rewrite_rule('^news/([0-9]+)/?', 'news?c=$1', 'top');
}
add_action('init', 'custom_rewrite_basic');
Hoping that it becomes useful for someone.
edited Aug 13 at 5:24 answered Jul 29 at 5:21
shazyriver
923 6 31
Thanks. Much helpfull – tousif Dec 7 at 6:35
<?php
$edit_post = add_query_arg('c', '123', 'news' );
?>
You can add any page inplace of "news".
answered Jul 9 '14 at 12:05
user3777827
121 1 2 11
This was the only way I could get this to work
add_action('init','add_query_args');
function add_query_args()
{
add_query_arg( 'var1', 'val1' );
}
http://codex.wordpress.org/Function_Reference/add_query_arg
answered Sep 26 '12 at 17:52
brenjt
10.3k 11 61 101
to add parameter to post url (to perma link), i use this:
output:
http://yoursite.com/pagename?my_pid=12345678
answered Apr 1 '14 at 14:00
T.Todua
22k 8 90 97
See https://core.trac.wordpress.org/ticket/25143 and
https://wordpress.org/support/topic/addingqueryvarmakesfrontpagemissing/ 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 the init action as it affects the rewrite rules. Eg.
function add_custom_setcookie_rewrite_endpoints() {
//add ?c=123 endpoint with
//EP_ALL so endpoint is present across all places
//no effect on the query vars
add_rewrite_endpoint( 'c', EP_ALL, $query_vars = false );
}
Remember to flush your rewrite rules after adding/modifying this.
edited Oct 19 at 6:30 answered Oct 19 at 6:18
niall.campbell
301 3 3