Title: wpdb::determine_charset
Published: August 16, 2016
Last modified: May 20, 2026

---

# wpdb::determine_charset( string $charset, string $collate ): array

## In this article

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

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

Determines the best charset and collation to use given a charset and collation.

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

For example, when able, utf8mb4 should be used instead of utf8.

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

 `$charset`stringrequired

The character set to check.

`$collate`stringrequired

The collation to check.

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

 array The most appropriate character set and collation to use.

 * `charset` string
 * Character set.
 * `collate` string
 * Collation.

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

    ```php
    public function determine_charset( $charset, $collate ) {
    	if ( ( ! ( $this->dbh instanceof mysqli ) ) || empty( $this->dbh ) ) {
    		return compact( 'charset', 'collate' );
    	}

    	if ( 'utf8' === $charset ) {
    		$charset = 'utf8mb4';
    	}

    	if ( 'utf8mb4' === $charset ) {
    		// _general_ is outdated, so we can upgrade it to _unicode_, instead.
    		if ( ! $collate || 'utf8_general_ci' === $collate ) {
    			$collate = 'utf8mb4_unicode_ci';
    		} else {
    			$collate = str_replace( 'utf8_', 'utf8mb4_', $collate );
    		}
    	}

    	// _unicode_520_ is a better collation, we should use that when it's available.
    	if ( $this->has_cap( 'utf8mb4_520' ) && 'utf8mb4_unicode_ci' === $collate ) {
    		$collate = 'utf8mb4_unicode_520_ci';
    	}

    	return compact( 'charset', 'collate' );
    }
    ```

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

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

| Uses | Description | 
| [wpdb::has_cap()](https://developer.wordpress.org/reference/classes/wpdb/has_cap/)`wp-includes/class-wpdb.php` |

Determines whether the database or WPDB supports a particular feature.

  |

| Used by | Description | 
| [wpdb::init_charset()](https://developer.wordpress.org/reference/classes/wpdb/init_charset/)`wp-includes/class-wpdb.php` |

Sets $this->charset and $this->collate.

  |

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

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

## User Contributed Notes

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