钩子文档

wp_kses_allowed_html

💡 云策文档标注

概述

wp_kses_allowed_html 是一个 WordPress 过滤器,用于控制特定上下文中允许的 HTML 标签和属性。它允许开发者自定义 KSES 安全过滤器的允许列表,确保内容安全。

关键要点

  • wp_kses_allowed_html 过滤器用于过滤允许的 HTML 标签和属性,基于给定的上下文。
  • HTML 标签和属性名在 HTML 中不区分大小写,但在 KSES 允许列表中必须以小写形式添加,否则不会被识别为允许。
  • 过滤器接受两个参数:$html(允许的 HTML 标签数组)和 $context(上下文名称)。
  • 相关函数 wp_kses_allowed_html() 返回给定上下文的允许 HTML 标签和属性数组。
  • 此过滤器自 WordPress 3.5.0 版本引入。

代码示例

add_filter( 'wp_kses_allowed_html', 'acf_add_allowed_iframe_tag', 10, 2 );
function acf_add_allowed_iframe_tag( $tags, $context ) {
  if ( $context === 'acf' ) {
    $tags['iframe'] = array(
      'src' => true,
      'height' => true,
      'width' => true,
      'frameborder' => true,
      'allowfullscreen' => true,
    );
  }
  return $tags;
}

注意事项

在添加标签到允许列表时,必须使用小写形式,以确保 KSES 能正确识别。


📄 原文内容

Filters the HTML tags that are allowed for a given context.

Description

HTML tags and attribute names are case-insensitive in HTML but must be added to the KSES allow list in lowercase. An item added to the allow list in upper or mixed case will not recognized as permitted by KSES.

Parameters

$htmlarray[]
Allowed HTML tags.
$contextstring
Context name.

Source

return apply_filters( 'wp_kses_allowed_html', $html, $context );

Changelog

Version Description
3.5.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    If you need to allow ACF to use iFrames you can use this:

    add_filter( ‘wp_kses_allowed_html’, ‘acf_add_allowed_iframe_tag’, 10, 2 );
    function acf_add_allowed_iframe_tag( $tags, $context ) {
    if ( $context === ‘acf’ ) {
    $tags[‘iframe’] = array(
    ‘src’ => true,
    ‘height’ => true,
    ‘width’ => true,
    ‘frameborder’ => true,
    ‘allowfullscreen’ => true,
    );
    }
    return $tags;
    }