How to Fix Character Encoding in WordPress

Months ago, i moved my WordPress blog from InterServer to JustHost and the process was successful. After successfully installing my blog on the new server, it run well and everything was working just fine except i saw some funny characters on my blog posts and had no idea on how to remove them.

Character Encoding

Being the first encounter, i had to browse the internet for all sorts of answers but majority of them didn’t help. Not even was i able to view the content of the posts with character encoding problems on the WordPress post editor to edit them. All i saw was a blank page but the front end showed my posts pretty well. I will explain further below.

What you should know is sometimes the problem is not with WordPress but MySQL though in most cases simple tweaks on your WordPress installation can solve the problem for you. If you are running MySQL version 4.4.1 you are most likely to encounter this problem.

WordPress stores your data in a UTF8 format in a database and expects it to be that way. If you create a database with a different encoding and import content from a UTF8 database then you are likely to encounter such problems.

How do we Fix Character Encoding?

Most people will recommend exporting your database in a UTF8 format, sanitizing it to make sure all characters are in the right format and then importing it again to a new UTF8 database. This will work in some cases but not all cases. Am going to show you two methods you can adopt to solve the character encoding problem in WordPress.

Method 1 – Edit wp-config.php

This method works and has solved most of the character encoding problems in WordPress. All you have to do is edit your wp-config.php file and comment out the character encoding lines. Open the wp-config file using any text editor such as notepad++ and comment out the lines below:

//before commenting

define('DB_CHARSET', 'utf8');
define('DB_COLLATE', '');

After commenting out those two lines, the file should look like this.

//After Commenting

//define('DB_CHARSET', 'utf8');
//define('DB_COLLATE', '');

If you find only one of the lines above in your wp-config file then comment it out. In some cases you might find two lines and still have to comment both of them out. This is usually the easiest way to fix encoding problems on your WordPress site but if it doesn’t solve anything, then the problem is most likely to be with MySQL. The second method should solve it.

Method 2 – Edit wp-db.php

The first method didn’t the problem but this method did. The problem was rather a bug on MySQL 4.1. All i had to do was edit the wp-db.php file on my WordPress installation. This file is found in the wp-includes directory. Open it using a text editor like Notepad++, navigate to line 1522 and add the code below right after it.


mysql_query("SET NAMES 'utf8'");

The code should be added right after the line that makes the connection to the database which is after line 1522. This line of code fixes posting and retrieving posts with accented characters by converting the characters to UTF8 character encoding before saving or retrieving them. Adding that single line of code to the wp-db.php file completely solved my problem. All the characters were now back to normal.

The WordPress character encoding problem can turn out to be a nightmare for you if you are white about the exact steps to take to overcome it. Some people lose data in the process because they try to fix character encoding in WordPress by attempting to convert their database to utf 8 character set. Don’t try doing this especially if you know less about databases. The two methods above will solve your character encoding problems. I suggest you first try out the first method and if it fails to work, the second should work.

The WordPress content encoding error is very common and a big percentage of people who migrate their WordPress sites to new servers face it. Have you ever encountered the WordPress utf 8 problem on your blog? Feel free to share your experience with us below.

2 Comments

  1. Hello Kolo,

    Thank you for posting this fix. It didn’t work for me. Would you the lin 1522 be the same today?

Leave a Reply

Your email address will not be published.

*