3

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.

2
  • I have exactly the same question DB_CHARSET. any opinions? Commented Sep 19, 2018 at 8:57
  • I didn't dig too much further, so I didn't find anything ideal. But in the end I stopped because "Why do I need it utf8 ?", the only reason for me is that it was annoying for importing into an already existing database of utf8_general_ci encoding (which was the old wordpress database). So, since Wordpress changed the default, I don't see an important reason to keep the old one, if you want to import just delete the database and create it again with the new encoding. Commented Sep 28, 2018 at 10:40

1 Answer 1

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.
4
  • Quoting from here. There usually should be no reason to change the default value of DB_COLLATE. Leaving the value blank (null) will insure the collation is automatically assigned by MySQL when the database tables are created. This means that WordPress is (or used to) take note of the collations upon installation, so I am not very sure if editing core files is the only way. Commented Aug 30, 2017 at 13:32
  • This talks about leaving the values blank, and you're inferring that to specify them in your config will mean WordPress doesn't touch them. It does. It's very explicit in the source code. In the function I pointed you towards, if you read it, you'll see that it's changing them if they meet certain values.
    – Paul G.
    Commented Sep 1, 2017 at 14:10
  • Alright, thanks, I'll check the core files and let you know on update. Commented Sep 1, 2017 at 23:31
  • This has been a problem for us in many years when moving development installs to the live server of our clients. Wordpress installs and choose the best choice available, but when clients live server does not have the same best choice, the migration tools and applications will be in trouble. We need to force Wordpress to always use our own choice on first local base install, and the only way to do that as of May 2020 is to edit the core as mentioned here in this topic. Commented May 26, 2020 at 18:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.