首页 > 解决方案 > 防止 WordPress 的默认输入清理

问题描述

每当我从一个<textarea>或一个文件中获得输入时input,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>

.

<?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. 
?>

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

在此处输入图像描述

标签: phpwordpressvalidation

解决方案


这是一个非常简单的 hack-y 想法

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

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

然后,只要您知道您会将代码内容传回浏览器

<?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. 
?>

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

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

function use_spost() {
  if (isset($_SPOST)) $_POST = $_SPOST;
}
add_action('parse_request', 'use_spost', 1);

推荐阅读