在WordPress页面中,可以使用以下函数来转义字符:
esc_html()
函数将HTML标记转换为实体字符,比如<
会被转换成<
:echo esc_html('<div class="example">Some text here</div>');
// 输出:<div class="example">Some text here</div>
esc_attr()
函数将HTML属性中的特殊字符转换为实体字符:echo esc_attr('Some text with "quotes"');
// 输出:Some text with "quotes"
esc_js()
函数将JavaScript代码中的特殊字符转换为实体字符:echo esc_js('console.log("Hello, world!");');
// 输出:console.log("Hello, world!");
wp_kses()
函数通过提供白名单来允许某些HTML标记及其属性:$content = '<div class="example"><script>alert("Hello, world!");</script></div>';
$allowed_tags = array(
'div' => array(
'class' => array(),
),
);
echo wp_kses($content, $allowed_tags);
// 输出:<div class="example"></div>
以上是一些常用的字符转义函数,在WordPress中支持更多转义函数,可以根据具体需求选择使用。