php 如何使用 WordPress 在 URL 中传递额外的变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4586835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:34:59  来源:igfitidea点击:

How to pass extra variables in URL with WordPress

phpwordpressurlget

提问by Chuck D

I am having trouble trying to pass an extra variable in the url to my WordPress installation.

我在尝试将 url 中的额外变量传递给我的 WordPress 安装时遇到问题。

For example /news?c=123

例如 /news?c=123

For some reason, it works only on the website root www.example.com?c=123but 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.

出于某种原因,它仅适用于网站根目录,www.example.com?c=123但如果 url 包含更多信息,则它不起作用www.example.com/news?c=123。我在主题目录中的functions.php 文件中有以下代码。

if (isset($_GET['c'])) 
{
  setcookie("cCookie", $_GET['c']); 
}

if (isset($_SERVER['HTTP_REFERER']))
{
  setcookie("rCookie", $_SERVER['HTTP_REFERER']);
}

Any Ideas?

有任何想法吗?

采纳答案by Sarfraz

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:

还请查看:

回答by Tom Auger

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)进行往返“WordPress 方式” ,您需要使用 3 个 WordPress 功能:

  • add_query_arg()- to create the URL with your new query variable ('c' in your example)
  • the query_varsfilter - to modify the list of public query variablesthat 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.
  • add_query_arg()- 使用您的新查询变量创建 URL(在您的示例中为“c”)
  • query_vars过滤器-修改的列表公众查询变量是WordPress的知道(这仅适用于前端,因为WP查询不是在后端使用- wp-admin-所以这也不会提供admin-ajax
  • get_query_var()- 检索在您的 URL 中传递的自定义查询变量的值。

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

注意:$_GET如果您这样做,甚至不需要触及超全局变量 ( )。

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 ) )?>">

<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/' ) ) )?>">

<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):

在您的functions.php,或一些插件文件或自定义类(仅限前端)中:

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:

在您希望检索和使用 URL 中设置的查询变量的页面/函数上:

$my_c = get_query_var( 'c' );

$my_c = get_query_var( 'c' );

On the Back End (wp-admin)

在后端 ( 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 varsand the query_varshook is not run.

在后端,我们从不运行wp(),因此不会运行主 WP 查询。结果,没有query vars并且query_vars钩子没有运行。

In this case, you'll need to revert to the more standard approach of examining your $_GETsuperglobal. The best way to do this is probably:

在这种情况下,您需要使用更标准的方法来检查您的$_GET超全局变量。最好的方法可能是:

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

$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'] : "";

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

or some variant thereof.

或其一些变体。

回答by shankhan

add following code in function.php

在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 will b able to use $_GET['var1']

那么你将能够使用 $_GET['var1']

回答by shazyriver

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

由于这是一个经常访问的帖子,我想发布我的解决方案,以防它对任何人有所帮助。在 WordPress 中使用查询变量,您也可以像这样更改永久链接

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.

为此,您必须在 functions.php 或您的插件基本文件中添加这些代码行。

From shankhan'sanwer

来自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 additionally this snipped to add custom rewriting rules.

此外,这剪断了添加自定义重写规则。

function custom_rewrite_basic() 
{
    add_rewrite_rule('^c/([0-9]+)/?', '?c=', '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

对于需要为特定页面添加重写规则的情况,您可以使用该页面 slug 为该特定页面编写重写规则。就像在 OP 提出的问题中一样

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

Hoping that it becomes useful for someone.

希望它对某人有用。

回答by user3777827

<?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 inplace of "news".

您可以添加任何页面来代替“新闻”。

回答by niall.campbell

One issue you might run into is is_home()returns true when a registered query_var is present in the home URL. For example, if http://example.comdisplays a static page instead of the blog, http://example.com/?c=123will return the blog.

您可能会遇到的一个问题is_home()是,当主 URL 中存在已注册的 query_var 时返回 true。例如,如果http://example.com显示静态页面而不是博客,http://example.com/?c=123将返回博客。

See https://core.trac.wordpress.org/ticket/25143and https://wordpress.org/support/topic/adding-query-var-makes-front-page-missing/for more info on this.

有关更多信息,请参阅https://core.trac.wordpress.org/ticket/25143https://wordpress.org/support/topic/adding-query-var-makes-front-page-missing/

What you can do (if you're not attempting to affect the query) is use add_rewrite_endpoint(). It should be run during the initaction as it affects the rewrite rules. Eg.

你可以做的(如果你不试图影响查询)是 use add_rewrite_endpoint()。它应该在init操作期间运行,因为它会影响重写规则。例如。

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 like www.example.com/news?c=123.

$_GET['c']当 url 包含更多信息时,这应该让您访问www.example.com/news?c=123

Remember to flush your rewrite rules after adding/modifying this.

请记住在添加/修改后刷新您的重写规则。

回答by T.Todua

to add parameter to post urls(to 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);
}

output:

输出:

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

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

回答by brenjt

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

http://codex.wordpress.org/Function_Reference/add_query_arg