wordpress / PHP - 访问帖子和获取变量

时间:2010-12-16 15:52:38

标签: php wordpress post get

有人能告诉我为什么我不从我在wordpress模板中提交的表单中检索信息吗?变量正在传递,但它们没有值?!?

4 个答案:

答案 0 :(得分:2)

对一个古老问题的新答案!

我发现这篇文章没有帮助,并编写了我自己的实用程序(愉快地分享并随意改进)

/* Get Parameters from $_POST and $_GET (WordPress)
    $param = string name of specific parameter requested (default to null, get all parameters
    $null_return = what you want returned if the parameter is not set (null, false, array() etc

    returns $params (string or array depending upon $param) of either parameter value or all parameters by key and value

    Note:   POST overrules GET (if both are set with a value and GET overrules POST if POST is not set or has a non-truthful value
            All parameters are trimmed and sql escaped
*/

function wordpress_get_params($param = null,$null_return = null){
    if ($param){
        $value = (!empty($_POST[$param]) ? trim(esc_sql($_POST[$param])) : (!empty($_GET[$param]) ? trim(esc_sql($_GET[$param])) : $null_return ));
        return $value;
    } else {
        $params = array();
        foreach ($_POST as $key => $param) {
            $params[trim(esc_sql($key))] = (!empty($_POST[$key]) ? trim(esc_sql($_POST[$key])) :  $null_return );
        }
        foreach ($_GET as $key => $param) {
            $key = trim(esc_sql($key));
            if (!isset($params[$key])) { // if there is no key or it's a null value
                $params[trim(esc_sql($key))] = (!empty($_GET[$key]) ? trim(esc_sql($_GET[$key])) : $null_return );
            }
        }
        return $params;
    }
}

答案 1 :(得分:1)

刚刚遇到了同样/类似的问题;在Wordpress上使用get变量并不理想,因为URL是使用mod_rewrite构造的,并且有一些保留的查询参数。 Wordpress Docs on query vars为您提供了一些列表,但它并不全面。

总之,您使用的变量可能是Wordpress保留或修改或处理的变量之一?

(我知道这是一个老问题,但需要回答或澄清。)

答案 2 :(得分:0)

请检查表格方法

<form name="frmlist" method="post">

答案 3 :(得分:-2)

试试这个

print var_dump($_GET);
print var_dump($_POST);