Database Encoding Fix
‘á’, ‘é’ => ‘é’, ‘í’ => ‘í’, ‘ó’ => ‘ó’, ‘ú’ => ‘ú’, ‘ü’ => ‘ü’,
‘Á’ => ‘Á’, ‘É’ => ‘É’, ‘Í’ => ‘Í’, ‘Ó’ => ‘Ó’, ‘Ú’ => ‘Ú’,
‘ñ’ => ‘ñ’, ‘Ã’ => ‘Ñ’,
‘Â’ => »,
];
foreach ($replacements as $bad => $good) {
$text = str_replace($bad, $good, $text);
}
return $text;
}
global $wpdb;
$fixed = 0;
echo «
Database Encoding Fix
«;
// Fix Yoast indexables
$table = $wpdb->prefix . ‘yoast_indexable’;
if ($wpdb->get_var(«SHOW TABLES LIKE ‘$table'»)) {
$fields = [‘description’, ‘title’, ‘open_graph_description’, ‘open_graph_title’, ‘twitter_description’, ‘twitter_title’];
foreach ($fields as $field) {
$count = $wpdb->get_var(«SELECT COUNT(*) FROM $table WHERE $field LIKE ‘%Ã%'»);
if ($count > 0) {
$wpdb->query(«UPDATE $table SET $field = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE($field, ‘ó’, ‘ó’), ‘á’, ‘á’), ‘é’, ‘é’), ‘í’, ‘í’), ‘ú’, ‘ú’), ‘ñ’, ‘ñ’) WHERE $field LIKE ‘%Ã%'»);
echo «
Fixed $count rows in $field
«;
$fixed += $count;
}
}
}
// Fix postmeta
$count = $wpdb->get_var(«SELECT COUNT(*) FROM {$wpdb->postmeta} WHERE meta_value LIKE ‘%Ã%'»);
if ($count > 0) {
$wpdb->query(«UPDATE {$wpdb->postmeta} SET meta_value = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(meta_value, ‘ó’, ‘ó’), ‘á’, ‘á’), ‘é’, ‘é’), ‘í’, ‘í’), ‘ú’, ‘ú’), ‘ñ’, ‘ñ’) WHERE meta_value LIKE ‘%Ã%'»);
echo «
Fixed $count rows in postmeta
«;
$fixed += $count;
}
// Fix posts
$count = $wpdb->get_var(«SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type=’page’ AND (post_content LIKE ‘%Ã%’ OR post_title LIKE ‘%Ã%’)»);
if ($count > 0) {
$wpdb->query(«UPDATE {$wpdb->posts} SET post_content = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(post_content, ‘ó’, ‘ó’), ‘á’, ‘á’), ‘é’, ‘é’), ‘í’, ‘í’), ‘ú’, ‘ú’), ‘ñ’, ‘ñ’) WHERE post_type=’page’ AND post_content LIKE ‘%Ã%'»);
$wpdb->query(«UPDATE {$wpdb->posts} SET post_title = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(post_title, ‘ó’, ‘ó’), ‘á’, ‘á’), ‘é’, ‘é’), ‘í’, ‘í’), ‘ú’, ‘ú’), ‘ñ’, ‘ñ’) WHERE post_type=’page’ AND post_title LIKE ‘%Ã%'»);
echo «
Fixed $count rows in posts
«;
$fixed += $count;
}
echo «
Total fixed: $fixed rows
«;
exit;
}
