Title: _canonical_charset
Published: April 25, 2014
Last modified: May 20, 2026

---

# _canonical_charset( string $charset ): string

## In this article

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

[ Back to top](https://developer.wordpress.org/reference/functions/_canonical_charset/?output_format=md#wp--skip-link--target)

This function’s access is marked private. This means it is not intended for use 
by plugin or theme developers, only by core. It is listed here for completeness.

Retrieves a canonical form of the provided charset appropriate for passing to PHP
functions such as htmlspecialchars() and charset HTML attributes.

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

### 󠀁[See also](https://developer.wordpress.org/reference/functions/_canonical_charset/?output_format=md#see-also)󠁿

 * [https://core.trac.wordpress.org/ticket/23688](https://core.trac.wordpress.org/ticket/23688/)

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

 `$charset`stringrequired

A charset name, e.g. "UTF-8", "Windows-1252", "SJIS".

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

 string The canonical form of the charset.

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

    ```php
    function _canonical_charset( $charset ) {
    	if ( is_utf8_charset( $charset ) ) {
    		return 'UTF-8';
    	}

    	/*
    	 * Normalize the ISO-8859-1 family of languages.
    	 *
    	 * This is not required for htmlspecialchars(), as it properly recognizes all of
    	 * the input character sets that here are transformed into "ISO-8859-1".
    	 *
    	 * @todo Should this entire check be removed since it's not required for the stated purpose?
    	 * @todo Should WordPress transform other potential charset equivalents, such as "latin1"?
    	 */
    	if (
    		( 0 === strcasecmp( 'iso-8859-1', $charset ) ) ||
    		( 0 === strcasecmp( 'iso8859-1', $charset ) )
    	) {
    		return 'ISO-8859-1';
    	}

    	return $charset;
    }
    ```

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

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

| Uses | Description | 
| [is_utf8_charset()](https://developer.wordpress.org/reference/functions/is_utf8_charset/)`wp-includes/functions.php` |

Indicates if a given slug for a character set represents the UTF-8 text encoding. If not provided, examines the current blog’s charset.

  |

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

Processes arguments passed to [wp_die()](https://developer.wordpress.org/reference/functions/wp_die/) consistently for its handlers.

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

Converts a number of special characters into their HTML entities.

  |

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

| Version | Description | 
| [3.6.0](https://developer.wordpress.org/reference/since/3.6.0/) | Introduced. |

## User Contributed Notes

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