WordPress, DreamHost, UTF-8 encoding and international characters

11 comments


If you work on any WordPress blogs/sites that are written in any language that has accented characters and happen to be on DreamHost, you have probably noticed character encoding problems. For instance, the new auto-save feature in WordPress or the Ajax categories add field in the category list, can display improperly encoded characters.

If you are seeing things like Actualités, read on brave shared hoster…

Your database should be set as UTF-8 (the WordPress install doesn’t specify a UTF-8 collation for the tables and fields and DreamHost uses the MySQL default of latin1_swedish_ci) for its character set and connection encoding. WordPress is set to use UTF-8 on the WordPress > Options > Reading page. Unfortunately, there is a problem with DreamHost’s setup. The MySQL client that is running on DreamHost’s servers has been set (or left at ) latin-1 encoding. This means that what you type into WordPress goes from UTF-8 text, to latin-1 text as it goes through the MySQL client and then into a UTF-8 database. This can cause a whole host of problems, especially for the Ajax components of WordPress 2.1 since the Ajax connector uses UTF-8 even if your blog is set to latin-1.

For all old content, you will need to convert it into the correct UTF-8 encoding, but for all new content, you can prevent this problem by forcing the MySQL client to use UTF-8 character encoding. Open the file wp-include/wp-db.php in your WordPress directory. Locate the following line:

$this->dbh = @mysql_connect($dbhost, $dbuser, $dbpassword);

On the next line paste this text:

$this->query("SET NAMES UTF8");

This instructs the MySQL client to use UTF-8 for data going to and from the database. Everything should now be working correctly as your UTF-8 posts go through a UTF-8 MySQL client into a UTF-8 database. It would be nice if DreamHost offered a control panel option to somehow manage the character encoding of new databases and the MySQL client.

Querying the database with “SET NAMES UTF8″ immediately after opening your DB connection is also useful if you are developing your own PHP applications on DreamHost servers or using one-click installed software like phpBB, Gallery, Joomla/Mambo, MediaWiki or activeCollab.

{ 10 comments… read them below or add one }

1 friend21 June 10, 2011 at 9:21 pm

This didn’t work for me in Wordpress 3.1… I am having the same issue on Dreamhost (with apostrophes and special characters in titles) and adding that code to similar place in the wp-db.php had no effect. Any ideas?

Reply

2 thelostseoul March 6, 2011 at 6:26 pm

Thank you for this post. Even though I do not use DreamHost, the issue is the same at other hosts. Thank you again.

PS The only thing that I would add is that there is sometimes login error language which may (or may not) make your directions a little unclear. That said, if you are careful, then it is no problem.

Reply

3 Pete November 16, 2010 at 12:14 pm

I tried by changing the wp-config file, as I read somewhere else, but it didn’t help.
I’ll try your suggestions now. tnx

Reply

4 Prashant January 15, 2010 at 8:57 am

Is there a plugin to do the same? Changing the character set in mysql may seem tricky for non programmers and would be much easier to do it from within wordpress admin.

Reply

5 Laura Conrad June 8, 2009 at 8:17 pm

Thanks. This was driving me nuts. In my case, it wasn’t the wordpress that wouldn’t take the accented character (that seems to have been fixed in the last two years), but another database I was using. I could put in the accented character and see it in phpmyadmin, I could put an accented character into a post in my wordpress database, I could display the data in a wordpress blog on another machine, but I could not see the accent in my dreamhost wordpress page.

Reply

6 Moliv December 12, 2008 at 2:55 am

Thanks for the post, I have been having the same problems.

Reply

7 wadeball December 11, 2008 at 10:49 pm

Thanks for your interesting article

Reply

8 Paulo October 23, 2008 at 6:35 pm

Thank you so much! I have wasted long hours trying to fix this problem! Thank you!

Reply

9 diamondTearz April 21, 2007 at 12:44 am

Thank you for this. I just updated my blog and for some reason started to have problems with this. Googled all over before I found your post.

Reply

10 fields marshall March 18, 2007 at 4:47 pm

Thanks very helpful information explained well. I had to dig around the wordpress site a while ago ..

I originally had my site on dreamhost and now moved it over to textdrive and noticed the same thing.

It would be nice if wordpress would put this into their codebase somehow as thefantastico installer and other automated updaters will overwrite the wp-db.php file . .

Reply

Leave a Comment

{ 1 trackback }

Previous post:

Next post: