从 WordPress 中的 URL 中提取参数

问题描述:

我正在尝试使用 URL 将参数传递给 WordPress 站点 - 例如:

I am trying to pass a parameter to a WordPress site using a URL - for instance:

www.fioriapts.com/?ppc=1 将是 URL.

我打算在 functions.php 文件中编写一个函数,但是如何在 WordPress 中提取参数的机制超出了我的范围.我找到了很多关于如何使用函数 add_query_arg() 向 URL 添加参数的示例,但没有找到关于如何提取参数的任何示例.在此先感谢您的帮助.

I am intending to write a function in the functions.php file but the mechanics of how to extract a parameter in WordPress is beyond me. I am finding a lot of examples on how to add a parameter to a URL using the function add_query_arg() but have found nothing on how to extract a parameter. Thanks in advance for any help.

通过 URL 传递参数时,您可以将值作为 GET 参数进行检索.

When passing parameters through the URL you're able to retrieve the values as GET parameters.

使用这个:

$variable = $_GET['param_name'];

//Or as you have it
$ppc = $_GET['ppc'];

首先检查变量更安全:

if (isset($_GET['ppc'])) {
  $ppc = $_GET['ppc'];
} else {
  //Handle the case where there is no parameter
}

这里有一些关于 GET/POST 参数的阅读,你应该看看:http://php.net/manual/en/reserved.variables.get.php

Here's a bit of reading on GET/POST params you should look at: http://php.net/manual/en/reserved.variables.get.php

我看到这个答案在制作多年后仍然有很多流量.阅读此答案附带的评论,尤其是来自 @emc 的输入,他详细介绍了安全地实现此目标的 WordPress 功能.

I see this answer still gets a lot of traffic years after making it. Please read comments attached to this answer, especially input from @emc who details a WordPress function which accomplishes this goal securely.