How to pass additional variables to URLs using WordPress

I am having trouble trying to pass an extra variable to a URL for my WordPress installation.

For example /news?c=123

For some reason, it only works in the root of the website www.example.com?c=123 but does not work if the URL contains additional 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?

+59
url php get wordpress
Jan 03 '11 at 17:44
source share
8 answers

To solve this problem is not enough. First you can choose a plugin if you want:

Or code manually, check this post:

Also check:

+32
Jan 03 '11 at 17:52
source share

To make the WordPress Path back path to the front-end (does not work in the wp-admin context), you need to use 3 WordPress features:

  • add_query_arg () - to create a URL with a new query variable ('c' in your example)
  • query_vars filter - change the list of public request variables that WordPress knows about (this only works on the interface because WP Query is not used on the back panel - wp-admin - so it will also not be available in admin-ajax )
  • get_query_var () - to get the value of the user query variable passed in your url.

Note: there is no need to even touch superglobals ( $_GET ) if you do so.

Example

On the page where you need to create the link / set the request variable:

if it links to this page simply by adding a query variable

<a href="<?php echo esc_url( add_query_arg( 'c', $my_value_for_c ) )?>">

if this is a link to another page

<a href="<?php echo esc_url( add_query_arg( 'c', $my_value_for_c, site_url( '/some_other_page/' ) ) )?>">

In your functions.php file or in some plugin file or in a special class (only for interfaces):

 function add_custom_query_var( $vars ){ $vars[] = "c"; return $vars; } add_filter( 'query_vars', 'add_custom_query_var' ); 

On the page / function where you want to receive and work with the var request specified in the url:

$my_c = get_query_var( 'c' );

At the back end ( wp-admin )

On the back, we never run wp() , so the main WP request does not start. As a result, there are no query vars , and query_vars does not start.

In this case, you will need to return to more standard approaches to checking your $_GET super $_GET . The best way to do this is probably:

$my_c = filter_input( INPUT_GET, "c", FILTER_SANITIZE_STRING );

although in a pinch you could perform a tried and true

$my_c = isset( $_GET['c'] ? $_GET['c'] : "";

or its variant.

+96
Aug 21 '13 at 16:03
source share

add the following code to function.php

 add_filter( 'query_vars', 'addnew_query_vars', 10, 1 ); function addnew_query_vars($vars) { $vars[] = 'var1'; // var1 is the name of variable you want to add return $vars; } 

then you can use $ _GET ['var1']

+15
Jan 03 '11 at 17:52
source share

Since this is a frequently visited post, I thought to post my decision if it helps anyone. In WordPress, along with using vars requests, you can also change permalinks like this:

 www.example.com?c=123 to www.example.com/c/123 

To do this, you need to add these lines of code to the functions.php file or your main plugin file.

From shankhan

 add_filter( 'query_vars', 'addnew_query_vars', 10, 1 ); function addnew_query_vars($vars) { $vars[] = 'c'; // c is the name of variable you want to add return $vars; } 

And, in addition, it is disabled to add custom rewrite rules.

 function custom_rewrite_basic() { add_rewrite_rule('^c/([0-9]+)/?', '?c=$1', 'top'); } add_action('init', 'custom_rewrite_basic'); 

In the case when you need to add rewrite rules for a page with specific parameters, you can use this page to write a rewrite rule for this specific page. As in the question A. was asking about.

 www.example.com/news?c=123 to www.example.com/news/123 

We can change it to the desired behavior by adding a small 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 this will be helpful to someone.

+9
Jul 29 '17 at 5:21
source share
 <?php $edit_post = add_query_arg('c', '123', 'news' ); ?> <a href="<?php echo $edit_post; ?>">Go to New page</a> 

You can add any page to the "news".

+3
Jul 09 '14 at 12:05
source share

One problem you may encounter is is_home() returns true when a registered var_query is present in the home URL. For example, if http://example.com displays a static page instead of a 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 information about this .

What you can do (if you are not trying to influence the request), use add_rewrite_endpoint() . It must be started during init , as it affects rewriting rules. For example.

 add_action( 'init', 'add_custom_setcookie_rewrite_endpoints' ); 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 ); } 

This should give you access to $_GET['c'] when the URL contains more information, such as www.example.com/news?c=123 .

Remember to reset the rewrite rules after adding / modifying this parameter.

+2
Oct 19 '17 at 6:18
source share

It was the only way to make it 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

+1
Sep 26 '12 at 17:52
source share

to add a parameter to publish urls (for perma links), I use this:

 add_filter( 'post_type_link', 'append_query_string', 10, 2 ); function append_query_string( $url, $post ) { return add_query_arg('my_pid',$post->ID, $url); } 

exit:

http://yoursite.com/pagename?my_pid=12345678

+1
Apr 01 '14 at 2:00
source share



All Articles