wpdb::determine_charset() WordPress Method

The wpdb::determine_charset() method is used to set the character set for the WordPress database. By default, this is set to latin1.

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

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


Description

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


Top ↑

Parameters

$charset

(string)(Required)The character set to check.

$collate

(string)(Required)The collation to check.


Top ↑

Return

(array) The most appropriate character set and collation to use.

  • 'charset'
    (string) Character set.
  • 'collate'
    (string) Collation.


Top ↑

Source

File: wp-includes/wp-db.php

832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
public function determine_charset( $charset, $collate ) {
    if ( ( $this->use_mysqli && ! ( $this->dbh instanceof mysqli ) ) || empty( $this->dbh ) ) {
        return compact( 'charset', 'collate' );
    }
 
    if ( 'utf8' === $charset && $this->has_cap( 'utf8mb4' ) ) {
        $charset = 'utf8mb4';
    }
 
    if ( 'utf8mb4' === $charset && ! $this->has_cap( 'utf8mb4' ) ) {
        $charset = 'utf8';
        $collate = str_replace( 'utf8mb4_', 'utf8_', $collate );
    }
 
    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' );
}


Top ↑

Changelog

Changelog
VersionDescription
4.6.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by the Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.