wp_kses_allowed_html( string|array $context = '' )

Returns an array of allowed HTML tags and attributes for a given context.


Parameters Parameters

$context

(string|array) (Optional) The context for which to retrieve tags. Allowed values are 'post', 'strip', 'data', 'entities', or the name of a field filter such as 'pre_user_description'.

Default value: ''


Top ↑

Return Return

(array) Array of allowed HTML tags and their allowed attributes.


Top ↑

More Information More Information

The Return value is a multidimensional array with the tag name as the key and an array of attributes as the value.


Top ↑

Source Source

File: wp-includes/kses.php

function wp_kses_allowed_html( $context = '' ) {
	global $allowedposttags, $allowedtags, $allowedentitynames;

	if ( is_array( $context ) ) {
		/**
		 * Filters the HTML that is allowed for a given context.
		 *
		 * @since 3.5.0
		 *
		 * @param array[]|string $context      Context to judge allowed tags by.
		 * @param string         $context_type Context name.
		 */
		return apply_filters( 'wp_kses_allowed_html', $context, 'explicit' );
	}

	switch ( $context ) {
		case 'post':
			/** This filter is documented in wp-includes/kses.php */
			$tags = apply_filters( 'wp_kses_allowed_html', $allowedposttags, $context );

			// 5.0.1 removed the `<form>` tag, allow it if a filter is allowing it's sub-elements `<input>` or `<select>`.
			if ( ! CUSTOM_TAGS && ! isset( $tags['form'] ) && ( isset( $tags['input'] ) || isset( $tags['select'] ) ) ) {
				$tags = $allowedposttags;

				$tags['form'] = array(
					'action'         => true,
					'accept'         => true,
					'accept-charset' => true,
					'enctype'        => true,
					'method'         => true,
					'name'           => true,
					'target'         => true,
				);

				/** This filter is documented in wp-includes/kses.php */
				$tags = apply_filters( 'wp_kses_allowed_html', $tags, $context );
			}

			return $tags;

		case 'user_description':
		case 'pre_user_description':
			$tags             = $allowedtags;
			$tags['a']['rel'] = true;
			/** This filter is documented in wp-includes/kses.php */
			return apply_filters( 'wp_kses_allowed_html', $tags, $context );

		case 'strip':
			/** This filter is documented in wp-includes/kses.php */
			return apply_filters( 'wp_kses_allowed_html', array(), $context );

		case 'entities':
			/** This filter is documented in wp-includes/kses.php */
			return apply_filters( 'wp_kses_allowed_html', $allowedentitynames, $context );

		case 'data':
		default:
			/** This filter is documented in wp-includes/kses.php */
			return apply_filters( 'wp_kses_allowed_html', $allowedtags, $context );
	}
}


Top ↑

Changelog Changelog

Changelog
Version Description
5.0.1 form removed as allowable HTML tag.
3.5.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by stevenlinx

    Example migrated from Codex:

    // strips all html (empty array)
    $allowed_html = wp_kses_allowed_html( 'strip' );
    
    // allows all most inline elements and strips all block level elements except blockquote
    $allowed_html = wp_kses_allowed_html( 'data' );
    
    // very permissive: allows pretty much all HTML to pass - same as what's normally applied to the_content by default
    $allowed_html = wp_kses_allowed_html( 'post' );
    
    // allows a list of HTML Entities such as  
    $allowed_html = wp_kses_allowed_html( 'entities' );
    

You must log in before being able to contribute a note or feedback.