admin管理员组文章数量:1335624
I'm new to wordpress development.
I'm trying to develop a simple plugin for practice. It involves a form submission from the frontend. The form has an input with the following code.
<input type='hidden' name='test' value='{"id": 1}'/>
When I submit the form, the value of 'test' field gets altered after the "init" hook.
My plugin code looks like the following
add_action('init', [$this, 'init']);
function init() {
printe_r($_POST);
}
Output:
Array
(
[test] => {\"id\":1}
)
The problem is that json_decode errors out. I have to use stripslashes like below to decode the json string.
json_decode(stripslashes($_POST['test']));
Is this behavior expected in Wordpress?
I'm new to wordpress development.
I'm trying to develop a simple plugin for practice. It involves a form submission from the frontend. The form has an input with the following code.
<input type='hidden' name='test' value='{"id": 1}'/>
When I submit the form, the value of 'test' field gets altered after the "init" hook.
My plugin code looks like the following
add_action('init', [$this, 'init']);
function init() {
printe_r($_POST);
}
Output:
Array
(
[test] => {\"id\":1}
)
The problem is that json_decode errors out. I have to use stripslashes like below to decode the json string.
json_decode(stripslashes($_POST['test']));
Is this behavior expected in Wordpress?
Share Improve this question asked Jun 27, 2020 at 11:39 왕뚜껑왕뚜껑 651 silver badge3 bronze badges2 Answers
Reset to default 0Yes, it's the expected behaviour. There's information on why it works like this here, and this function which can help in removing slashes from larger data structures: https://developer.wordpress/reference/functions/stripslashes_deep/
In the short term, if you can live without JSON for this field, why not just remove it, and if you need multiple hidden fields add them separately, e.g.:
<input type='hidden' name='testId' value='1'/>
<input type='hidden' name='otherId' value='123'/>
And you can get those directly with:
echo $_POST['testId'];
echo $_POST['otherId'];
Will output:
1
123
As @mozboz explained, WordPress is changing the values of $_POST
, $_GET
, $_REQUEST
, and $_COOKIE
. If you need the raw values, use the function filter_input()
, and make sure to keep security concerns in mind.
Example:
$test = filter_input( INPUT_POST, 'test', FILTER_SANITIZE_STRING );
本文标签: pluginsPOST field value gets altered after quotinitquot
版权声明:本文标题:plugins - $_POST field value gets altered after "init" 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742313491a2451345.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论