我创建了一个wordpress主题,在wordpress主题定制器中,我添加了几个文本区域,用户可以在其中输入自己的CSS或JS以添加到头部。
文本区域的位置很好,即当用户添加代码时,它显示在页面的正确位置,但是它的格式不同。
例如,我将以下代码添加到其中一个文本区域:
jQuery(document).ready(function($){
$('full_page').css('min-height',($(window).height()-195));
});
在我的主题中,它的输出如下所示:
jQuery(document).ready(function($){
$('full_page').css('min-height',($(window).height()-195));
});
如您所见,'
正在替换为'
这是我的定制器.php文件中用于创建文本区域的代码:
$controls[] = array(
'type' => 'textarea',
'setting' => 'js',
'label' => __( 'Custom JS', 'skizzar_bootstrap' ),
'subtitle' => __( 'You can write your custom JavaScript/jQuery here. The code will be included in a script tag appended to the top of the page.', 'skizzar_bootstrap' ),
'section' => 'advanced',
'priority' => 6,
'default' => '',
);
有没有办法阻止这种格式的发生?
Wordpress是对所有特殊字符进行HTML编码。如果用户输入 HTML,则结果应该没问题。它不适用于Javascript。
允许用户像这样输入Javascript是一个很大的安全漏洞。剧本可以做任何事情,不一定都是令人愉快的。
如果您真的想这样做,并且您的代码在主题中回显 Javascript,请在显示它之前运行它htmlspecialchars_decode。该PHP函数会将HTML代码转换回字符。