Skip to content

Instantly share code, notes, and snippets.

@vslala
Last active June 24, 2018 18:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vslala/42e98c8344d78d071e13587fbc81fd5e to your computer and use it in GitHub Desktop.
Save vslala/42e98c8344d78d071e13587fbc81fd5e to your computer and use it in GitHub Desktop.
You might have seen weird characters in your WordPress post recently. This is because of bad character encoding. These SQL commands will update all the bad encoded texts with the correct one.
-- BeMyAficionado.com
-- Backup Database to a .sql file
mysqldump --opt --default-character-set=latin1 --skip-extended-insert --user root --password my-database-name -r exp-my-database-name-latin1.sql --log-error=log-mysqldump-my-database.txt
-- Clean up post_content
UPDATE wp_posts SET post_content = REPLACE(post_content, ' </p>', '</p>');
UPDATE wp_posts SET post_content = REPLACE(post_content, '“', '“');
UPDATE wp_posts SET post_content = REPLACE(post_content, '”', '”');
UPDATE wp_posts SET post_content = REPLACE(post_content, '’', '’');
UPDATE wp_posts SET post_content = REPLACE(post_content, '‘', '‘');
UPDATE wp_posts SET post_content = REPLACE(post_content, '—', '–');
UPDATE wp_posts SET post_content = REPLACE(post_content, '–', '—');
UPDATE wp_posts SET post_content = REPLACE(post_content, '•', '-');
UPDATE wp_posts SET post_content = REPLACE(post_content, '…', '…');
UPDATE wp_posts SET post_content = REPLACE(post_content, 'Â', '');
UPDATE wp_posts SET post_content = REPLACE(post_content, '–', '-');
 
-- Clean up comment_content
UPDATE wp_comments SET comment_content = REPLACE(comment_content, '“', '“');
UPDATE wp_comments SET comment_content = REPLACE(comment_content, '”', '”');
UPDATE wp_comments SET comment_content = REPLACE(comment_content, '’', '’');
UPDATE wp_comments SET comment_content = REPLACE(comment_content, '‘', '‘');
UPDATE wp_comments SET comment_content = REPLACE(comment_content, '—', '–');
UPDATE wp_comments SET comment_content = REPLACE(comment_content, '–', '—');
UPDATE wp_comments SET comment_content = REPLACE(comment_content, '•', '-');
UPDATE wp_comments SET comment_content = REPLACE(comment_content, '…', '…');
-- Replace latin charset with utf8 charset and create new .sql file with utf8 encoding
replace "CHARSET=latin1" "CHARSET=utf8" "SET NAMES latin1" "SET NAMES utf8" < exp-my-database-name-latin1.sql > exp-my-database-name-utf8.sql
-- create new database that will hold data with utf8 encoding
create database my-database-name_utf8 character set UTF8 collate utf8_bin;
-- Export utf8 encoded data to your new database.
mysql --default-character-set=utf8 --user=root --password='my-database-password' my-database-name_utf8 < exp-my-database-name-utf8.sql;
/*
* Once you have fired the above commands
* Optn wp-config.php located at the WordPress root
* and find `define(’DB_CHARSET’, ‘utf8′);`
* change the 'utf8' to 'utf-8`, so it becomes
* define(’DB_CHARSET’, ‘utf-8′);
* and save the file
* head back to your WordPress website and now you should not see those weird characters anymore.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment