wp_kses_allowed_htmlхук-фильтрWP 3.5.0

Позволяет изменить массив допустимых HTML-тегов и атрибутов, используемый при очистке контента функциями семейства wp_kses().

Теги и атрибуты должны быть в нижнем регистре, иначе KSES их не распознает.

Использование

add_filter( 'wp_kses_allowed_html', 'wp_kama_kses_allowed_html_filter', 10, 2 );

/**
 * Function for `wp_kses_allowed_html` filter-hook.
 * 
 * @param array[] $html    Allowed HTML tags.
 * @param string  $context Context name.
 *
 * @return array[]
 */
function wp_kama_kses_allowed_html_filter( $html, $context ){

	// filter...
	return $html;
}
$html(массив)
Разрешенные HTML теги. Массив элеметов тег ⇒ атрибуты, который вы можете дополнять или сокращать через фильтр.
$context(строка)
Контекст в котором срабатывает очистка. Например: post, data, strip, entities, pre_user_description и т.д.

Примеры

0

#1 Демо пример

add_filter( 'wp_kses_allowed_html', 'add_additional_allowed_html', 10, 2 );
function add_additional_allowed_html( $allowed, $context ) {
	// Меняем правила только для контекста 'post'.
	if ( 'post' !== $context ) {
		return $allowed;
	}

	// Разрешаем SVG.
	$allowed['svg']  = array(
		'xmlns'   => true,
		'viewbox' => true,
		'width'   => true,
		'height'  => true,
		'fill'    => true,
	);

	$allowed['path'] = array(
		'd'    => true,
		'fill' => true,
	);

	return $allowed;
}
0

#2 Разрешим iframe в визуальный редактор

See: https://gist.github.com/bueltge/4511711

// allow script & iframe tag within posts
add_filter( 'wp_kses_allowed_html','allow_post_tags', 1 );
function allow_post_tags( $allowedposttags ){
	$allowedposttags['script'] = array(
		'type'   => true,
		'src'    => true,
		'height' => true,
		'width'  => true,
	);

	$allowedposttags['iframe'] = array(
		'src' => true,
		'width' => true,
		'height' => true,
		'class' => true,
		'frameborder' => true,
		'webkitAllowFullScreen' => true,
		'mozallowfullscreen' => true,
		'allowFullScreen' => true
	);

	return $allowedposttags;
}

Список изменений

С версии 3.5.0 Введена.

Где вызывается хук

wp_kses_allowed_html()
wp_kses_allowed_html
wp-includes/kses.php 869
return apply_filters( 'wp_kses_allowed_html', $html, $context );
wp-includes/kses.php 875
$tags = apply_filters( 'wp_kses_allowed_html', $allowedposttags, $context );
wp-includes/kses.php 892
$tags = apply_filters( 'wp_kses_allowed_html', $tags, $context );
wp-includes/kses.php 904
return apply_filters( 'wp_kses_allowed_html', $tags, $context );
wp-includes/kses.php 908
return apply_filters( 'wp_kses_allowed_html', array(), $context );
wp-includes/kses.php 912
return apply_filters( 'wp_kses_allowed_html', $allowedentitynames, $context );
wp-includes/kses.php 917
return apply_filters( 'wp_kses_allowed_html', $allowedtags, $context );

Где используется хук в WordPress

wp-includes/class-wp-customize-widgets.php 1798
add_filter( 'wp_kses_allowed_html', array( $this, 'filter_wp_kses_allowed_data_attributes' ) );