防止 WordPress 的默认输入清理

D P*_* P. 6 php validation wordpress

每当我从 a<textarea>或 ainput文件中获得输入时,WordPress 都会清理我的输入并转义所有特殊字符。如何禁用此功能?例如,如果我有以下接受 C++ 代码(例如cout<<"hello world";WordPress)的html 代码将其转换为cout<<\"hello world\";.

<!--HTML code-->
<form action="/action.php" method="post">
  <input type="text" name="mycode" value="cout<<'hello world';">
  <input type="submit" value="Submit">
</form>
Run Code Online (Sandbox Code Playgroud)

.

<?php
    //PHP code for action.php file
    echo $_POST['mycode'];//This output will have all the special characters escaped
    //I need this to give me the original text entered by the user without /s. 
?>
Run Code Online (Sandbox Code Playgroud)

我使用的是 WordPress 5.7.2 版。任何时候我使用像 \, ', " 他们这样的特殊字符都会出现\在他们面前。我使用不同的 WordPress 主题尝试过这个,结果仍然相同。如果我用stripcslashes($_POST['mycode'])这个得到这些\。但是想知道是否有办法从一开始就阻止 WordPress 这样做。下面显示了我得到的输入和输出的图像。

在此处输入图片说明

Joh*_*ner 1

这是一个非常简单的黑客想法

在 的顶部/index.php,在 WP 贪婪地获取传入数据之前,添加以下行:

$_SPOST = null;
if (isset($_SERVER['REQUEST_METHOD']) && strtoupper($_SERVER['REQUEST_METHOD']) === 'POST') {
   $_SPOST = $_POST;
}
Run Code Online (Sandbox Code Playgroud)

然后,每当您知道您会将代码内容传递回浏览器时

<?php
    //PHP code for action.php file
    echo $_SPOST['mycode'];//This output will have all the special characters escaped
    //I need this to give me the original text entered by the user without /s. 
?>
Run Code Online (Sandbox Code Playgroud)

但是等等,还有更多……我们可以重新连接到 WordPress 生态系统中,并在我们的帖子被修改和清理后对其进行转换。

此页面给了我使用parse_request 的想法,它会在解析当前请求的所有查询变量后触发。

function use_spost() {
  if (isset($_SPOST)) $_POST = $_SPOST;
}
add_action('parse_request', 'use_spost', 1);
Run Code Online (Sandbox Code Playgroud)

  • 感谢您的所有评论和回答 (2认同)