Custom DB_COLLATE (collation) value not working on fresh install?

I always knew that WordPress uses utf8_general_ci as tables collation, but recently it keeps installing utf8mb4_unicode_520_ci.

What I do is:

  1. Download WordPress from wordpress.org
  2. Rename wp-config-example.php to wp-config.php
  3. Set the defined constants as below

    /** Database Charset to use in creating database tables. */  
    define('DB_CHARSET', 'utf8');
    
    /** The Database Collate type. Don't change this if in doubt. */  
    define('DB_COLLATE', 'utf8_general_ci');  
    
  4. Set database configuration properly and create an empty database with charset and collation as above.

  5. Open mysite.dev where the WordPress is extracted and run the WordPress installation.
  6. Everything installed properly, now view my database settings AND BOOM, the tables have charset utf8mb4 and collation utf8mb4_unicode_520_ci. Side note : Some plugins that I install later will have tables collation utf8_general_ci …

Running mysql --version on my machine returns:

mysql  Ver 14.14 Distrib 5.7.19, for Linux (x86_64) using  EditLine wrapper

Note: If I use default DB_COLLATE and DB_CHARSET (omit my definitions), the tables get utf8mb4_unicode_520_ci, looks like that is the default now.

So, how do I tell WordPress to create the tables with the proper encoding ?

Thanks in advance.

1 Answer
1

You only have 1 option here, which by most standards isn’t advised. You have to edit a core WordPress file: wp-db.php

The reason is that there are no filters to change these and the code is kinda hard-coded. WordPress checks the availability of collation it selects though. If you need it just enough to setup your site and no more, modifying the file temporary would work. Note that as soon as WordPress is updated, this code will likely be replaced.

Steps:

  1. Open up the file /wp-includes/wp-db.php for editing
  2. locate the function called determine_charset(). In WordPress 4.8.1 it is on line 773
  3. Make the first line of the function return compact( 'charset', 'collate' );
  4. Save your file.

The function would look as follows:

public function determine_charset( $charset, $collate ) {
          return compact( 'charset', 'collate' );

Do not delete anything, just add that 1 line below the function definition.

  • Also, this assumes that you have your custom Charset and Collate specified in your WP Config.

Leave a Comment