Extend allowed HTML tags and attributes in WordPress wp_kses_post()

/**
 * Get allowed HTML for `wp_kses()`, i.e., `echo wp_kses( $output, wpcodebook_get_allowed_html() );`
 *
 * @return array Allowed HTML tags and attributes.
 *
 * @see https://wpcodebook.com/extend-allowed-html-tags-and-attributes-in-wordpress-wp_kses_post/
 * @see https://developer.wordpress.org/reference/functions/wp_kses/
 * @see https://developer.wordpress.org/reference/functions/wp_kses_post/
 * @see https://developer.wordpress.org/reference/functions/wp_kses_allowed_html/
 */
function wpcodebook_get_allowed_html() {
	$allowed_html = wp_kses_allowed_html( 'post' ); // Allowed in the `wp_kses_post()`.

	$allowed_html['option'] = array(
		'value'    => true,
		'selected' => true,
	);

	$allowed_html['select'] = array(
		'multiple' => true,
		'class'    => true,
		'style'    => true,
		'id'       => true,
		'name'     => true,
		'disabled' => true,
	);

	$allowed_html['input'] = array(
		'class'    => true,
		'style'    => true,
		'type'     => true,
		'id'       => true,
		'name'     => true,
		'value'    => true,
		'min'      => true,
		'max'      => true,
		'disabled' => true,
		'onclick'  => true,
	);

	return $allowed_html;
}

Leave a Comment