php 从 WordPress 中的 URL 中提取参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13652605/
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
Extracting a parameter from a URL in WordPress
提问by Chuck
I am trying to pass a parameter to a WordPress site using a URL - for instance:
我正在尝试使用 URL 将参数传递给 WordPress 站点 - 例如:
www.fioriapts.com/?ppc=1will be the URL.
www.fioriapts.com/?ppc=1将是网址。
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.
我打算在functions.php 文件中编写一个函数,但是如何在WordPress 中提取参数的机制超出了我的范围。我找到了很多关于如何使用该函数向 URL 添加参数的示例,add_query_arg()但没有找到有关如何提取参数的任何示例。在此先感谢您的帮助。
回答by Grambot
When passing parameters through the URL you're able to retrieve the values as GET parameters.
通过 URL 传递参数时,您可以检索作为 GET 参数的值。
Use this:
用这个:
$variable = $_GET['param_name'];
//Or as you have it
$ppc = $_GET['ppc'];
It is safer to check for the variable first though:
不过,先检查变量更安全:
if (isset($_GET['ppc'])) {
$ppc = $_GET['ppc'];
} else {
//Handle the case where there is no parameter
}
Here's a bit of reading on GET/POST params you should look at: http://php.net/manual/en/reserved.variables.get.php
这里有一些关于 GET/POST 参数的阅读,你应该看看:http: //php.net/manual/en/reserved.variables.get.php
EDIT: I see this answer still gets a lot of traffic years after making it. Pleaseread comments attached to this answer, especially input from @emc who details a WordPress function which accomplishes this goal securely.
编辑:我看到这个答案在制作多年后仍然有很多流量。请阅读此答案附带的评论,尤其是来自 @emc 的输入,他详细介绍了可安全实现此目标的 WordPress 功能。
回答by Marc
Why not just use the WordPress get_query_var()function? Link to Codex
为什么不直接使用 WordPressget_query_var()功能呢?法典链接
// Test if the query exists at the URL
if ( get_query_var('ppc') ) {
// If so echo the value
echo get_query_var('ppc');
}
Since get_query_var can only access query parameters available to WP_Query, in order to access a custom query var like 'ppc', you will also need to register this query variable within your plugin or functions.phpby adding an action during initialization:
由于 get_query_var 只能访问 WP_Query 可用的查询参数,为了访问像“ppc”这样的自定义查询变量,您还需要在插件中注册此查询变量或functions.php在初始化期间添加一个操作:
add_action('init','add_get_val');
function add_get_val() {
global $wp;
$wp->add_query_var('ppc');
}
Or by adding a hook to the query_vars filter:
或者通过向 query_vars 过滤器添加一个钩子:
function add_query_vars_filter( $vars ){
$vars[] = "ppc";
return $vars;
}
add_filter( 'query_vars', 'add_query_vars_filter' );
回答by Mohan
In the call back function, use the $request parameter
在回调函数中,使用 $request 参数
$parameters = $request->get_params();
echo $parameters['ppc'];
回答by briankip
You can try this function
你可以试试这个功能
/**
* Gets the request parameter.
*
* @param string $key The query parameter
* @param string $default The default value to return if not found
*
* @return string The request parameter.
*/
function get_request_parameter( $key, $default = '' ) {
// If not request set
if ( ! isset( $_REQUEST[ $key ] ) || empty( $_REQUEST[ $key ] ) ) {
return $default;
}
// Set so process it
return strip_tags( (string) wp_unslash( $_REQUEST[ $key ] ) );
}
Here is what is happening in the function
这是函数中发生的事情
Here three things are happening.
这里正在发生三件事。
- First we check if the request key is present or not. If not, then just return a default value.
- If it is set, then we first remove slashes by doing wp_unslash. Read here why it is better than stripslashes_deep.
- Then we sanitize the value by doing a simple strip_tags. If you expect rich text from parameter, then run it through wp_kses or similar functions.
- 首先,我们检查请求密钥是否存在。如果没有,则只返回一个默认值。
- 如果设置了,那么我们首先通过 wp_unslash 删除斜线。在这里阅读为什么它比 stripslashes_deep 更好。
- 然后我们通过做一个简单的 strip_tags 来清理这个值。如果您期望参数中的富文本,则通过 wp_kses 或类似函数运行它。
All of this information plus more info on the thinking behind the function can be found on this link https://www.intechgrity.com/correct-way-get-url-parameter-values-wordpress/
所有这些信息以及有关该函数背后思想的更多信息都可以在此链接上找到https://www.intechgrity.com/correct-way-get-url-parameter-values-wordpress/

