Switch WordPress from utf8 to utf8mb4 retrospectively

The WordPress 4.2 upgrade changed the database character set from utf8 to utf8mb4 (for example necessary for full emoji support) if a few requirements were met. This article shows you how to make the switch manually if you did not meet the requirements at the time of the WordPress 4.2 upgrade.

Requirements for the utf8mb4 update

Gary Pendergast lists the following requirements in his post »The utf8mb4 Upgrade« (quoted from his post):

  • You’re currently using the utf8 character set.
  • Your MySQL server is version 5.5.3 or higher (including all 10.x versions of MariaDB).
  • Your MySQL client libraries are version 5.5.3 or higher. If you’re using mysqlnd, 5.0.9 or higher.

Because my hosting provider Uberspace currently uses CentOS 6, which cannot reach the needed MySQL version, I did not get the update automatically with 4.2. After failing with saving an Emoji today, and getting the hint with utf8mb4 from Bernhard Kau, I searched for a solution. I found it in the post »utf8mb4 auf uberspace« on crusy.net — the solution for me is switching to MariaDB.

If you now simply have a MySQL version which is high enough, you can likely change the character set via phpMyAdmin or similar tools (but I did not test that, because that would currently be no solution for me because of the above-named reason).

In any case, you need to change the tables to utf8mb4 after that — changing the database character set is not sufficient.

Changing the tables to utf8mb4

I found the solution for this problem on wordpress.stackexchange.com. There the update routine from 4.2 (respectively 4.3, since the core team of WordPress pushed the routine to 4.3, to get more sites updated) is put into a plugin or functions.php of your theme and called via the URL parameter ?update-utf8bm4=1. With that, all tables are switched to utf8mb4, where possible.

I inserted that into a small plugin, which you can find as a Gist on GitHub. On a multisite, you have to call the script on each site of the multisite. If you are done with the change, you should remove the plugin!

3 reactions on »Switch WordPress from utf8 to utf8mb4 retrospectively«

  1. But... I think the table has a setting which your plugin changes to "utf8mb4" and then each text column has a setting too, which needs to be changed too shouldn't it?

Leave a Reply

Your email address will not be published. Required fields are marked *