Title: wp_kses_normalize_entities
Published: April 25, 2014
Last modified: August 20, 2026

---

# wp_kses_normalize_entities( string $content, string $context = ‘html’ ): string

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/wp_kses_normalize_entities/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/wp_kses_normalize_entities/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/wp_kses_normalize_entities/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/wp_kses_normalize_entities/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/wp_kses_normalize_entities/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_kses_normalize_entities/?output_format=md#changelog)

Converts and fixes HTML entities.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/wp_kses_normalize_entities/?output_format=md#description)󠁿

This function normalizes HTML entities. It will convert `AT&T` to the correct `AT&
amp;T`, `&#00058;` to `&#058;`, `&#XYZZY;` to `&amp;#XYZZY;` and so on.

When `$context` is set to ‘xml’, HTML entities are converted to their code points.
For example, `AT&T&hellip;&#XYZZY;` is converted to `AT&amp;T…&amp;#XYZZY;`.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/wp_kses_normalize_entities/?output_format=md#parameters)󠁿

 `$content`stringrequired

Content to normalize entities.

`$context`stringoptional

Context for normalization. Can be either `'html'` or `'xml'`.

Default:`'html'`

## 󠀁[Return](https://developer.wordpress.org/reference/functions/wp_kses_normalize_entities/?output_format=md#return)󠁿

 string Content with normalized entities.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/wp_kses_normalize_entities/?output_format=md#source)󠁿

    ```php
    function wp_kses_normalize_entities( $content, $context = 'html' ) {
    	// Disarm all entities by converting & to &amp;
    	$content = str_replace( '&', '&amp;', $content );

    	/*
    	 * Decode any character references that are now double-encoded.
    	 *
    	 * It's important that the following normalizations happen in the correct order.
    	 *
    	 * At this point, all `&` have been transformed to `&amp;`. Double-encoded named character
    	 * references like `&amp;amp;` will be decoded back to their single-encoded form `&amp;`.
    	 *
    	 * First, numeric (decimal and hexadecimal) character references must be handled so that
    	 * `&amp;#09;` becomes `&#9;`. If the named character references were handled first, there
    	 * would be no way to know whether the double-encoded character reference had been produced
    	 * in this function or was the original input.
    	 *
    	 * Consider the two examples, first with named entity decoding followed by numeric
    	 * entity decoding. We'll use U+002E FULL STOP (.) in our example, this table follows the
    	 * string processing from left to right:
    	 *
    	 * | Input        | &-encoded        | Named ref double-decoded  | Numeric ref double-decoded |
    	 * | ------------ | ---------------- | ------------------------- | -------------------------- |
    	 * | `&#x2E;`     | `&amp;#x2E;`     | `&amp;#x2E;`              | `&#x2E;`                   |
    	 * | `&amp;#x2E;` | `&amp;amp;#x2E;` | `&amp;#x2E;`              | `&#x2E;`                   |
    	 *
    	 * Notice in the example above that different inputs result in the same result. The second case
    	 * was not normalized and produced HTML that is semantically different from the input.
    	 *
    	 * | Input        | &-encoded        |  Numeric ref double-decoded | Named ref double-decoded |
    	 * | ------------ | ---------------- | --------------------------- | ------------------------ |
    	 * | `&#x2E;`     | `&amp;#x2E;`     | `&#x2E;`                    | `&#x2E;`                 |
    	 * | `&amp;#x2E;` | `&amp;amp;#x2E;` | `&amp;amp;#x2E;`            | `&amp;#x2E;`             |
    	 *
    	 * Here, each input is normalized to an appropriate output.
    	 */
    	$content = preg_replace_callback( '/&amp;#(0*[1-9][0-9]{0,6});/', 'wp_kses_normalize_entities2', $content );
    	$content = preg_replace_callback( '/&amp;#[Xx](0*[1-9A-Fa-f][0-9A-Fa-f]{0,5});/', 'wp_kses_normalize_entities3', $content );
    	if ( 'xml' === $context ) {
    		$content = preg_replace_callback( '/&amp;([A-Za-z]{2,8}[0-9]{0,2});/', 'wp_kses_xml_named_entities', $content );
    	} else {
    		$content = preg_replace_callback( '/&amp;([A-Za-z]{2,8}[0-9]{0,2});/', 'wp_kses_named_entities', $content );
    	}

    	return $content;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/kses.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/kses.php#L2169)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/kses.php#L2169-L2214)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/wp_kses_normalize_entities/?output_format=md#related)󠁿

| Used by | Description | 
| [esc_url()](https://developer.wordpress.org/reference/functions/esc_url/)`wp-includes/formatting.php` |

Checks and cleans a URL.

  | 
| [_wp_specialchars()](https://developer.wordpress.org/reference/functions/_wp_specialchars/)`wp-includes/formatting.php` |

Converts a number of special characters into their HTML entities.

  | 
| [wp_kses()](https://developer.wordpress.org/reference/functions/wp_kses/)`wp-includes/kses.php` |

Filters text content and strips out disallowed HTML.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/wp_kses_normalize_entities/?output_format=md#changelog)󠁿

| Version | Description | 
| [5.5.0](https://developer.wordpress.org/reference/since/5.5.0/) | Added `$context` parameter. | 
| [1.0.0](https://developer.wordpress.org/reference/since/1.0.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_kses_normalize_entities%2F)
before being able to contribute a note or feedback.