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?
Any word after the question mark (?) in a URL is considered to be a parameter which can hold values. The value for the corresponding parameter is given after the symbol "equals" (=). Multiple parameters can be passed through the URL by separating them with multiple "&".
To add a URL variable to each link, go to the Advanced tab of the link editor. In the URL Variables field, you will enter a variable and value pair like so: variable=value. For example, let's say we are creating links for each store and manager.
The Process. The first step was to create a function to add variables to WordPress' query string and then hook that function into the query_vars hook. Next, we created a function to add rules to WordPress' existing array of rewrite rules and hook that function into the rewrite_rules_array hook.
To make the round trip "The WordPress Way" on the "front-end" (doesn't work in the context of wp-admin
), you need to use 3 WordPress functions:
wp-admin
- so this will also not be available in admin-ajax
)Note: there's no need to even touch the superglobals ($_GET
) if you do it this way.
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
<a href="<?php echo esc_url( add_query_arg( 'c', $my_value_for_c ) )?>">
if it's a link to some other page
<a href="<?php echo esc_url( add_query_arg( 'c', $my_value_for_c, site_url( '/some_other_page/' ) ) )?>">
In your functions.php, or some plugin file or custom class (front-end only):
function add_custom_query_var( $vars ){ $vars[] = "c"; return $vars; } add_filter( 'query_vars', 'add_custom_query_var' );
On the page / function where you wish to retrieve and work with the query var set in your URL:
$my_c = get_query_var( 'c' );
wp-admin
)On the back end we don't ever run wp()
, so the main WP Query does not get run. As a result, there are no query vars
and the query_vars
hook is not run.
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:
$my_c = filter_input( INPUT_GET, "c", FILTER_SANITIZE_STRING );
though in a pinch you could do the tried and true
$my_c = isset( $_GET['c'] ? $_GET['c'] : "";
or some variant thereof.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With