Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 579
Chuck D Avatar asked Jan 03 '11 17:01

Chuck D


People also ask

How do I pass multiple values in a URL?

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 "&".

How do you append a variable in URL?

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.

How do I pass query string in WordPress?

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.


2 Answers

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:

  • 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 front-end, 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.

Note: there's no need to even touch the superglobals ($_GET) if you do it this way.

Example

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' );

On the Back End (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.

like image 140
Tom Auger Avatar answered Oct 03 '22 10:10

Tom Auger


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
like image 22
Sarfraz Avatar answered Oct 03 '22 10:10

Sarfraz