Skip to content

Instantly share code, notes, and snippets.

@alessandro-fazzi
Created September 25, 2015 14:27
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 alessandro-fazzi/8a3dc7da62017b87e9aa to your computer and use it in GitHub Desktop.
Save alessandro-fazzi/8a3dc7da62017b87e9aa to your computer and use it in GitHub Desktop.
Force
<?php
/**
* @package MySQL_Collation_Fixer
* @version 1.6
*/
/*
Plugin Name: MySQL Collation fixer
Description: Change database tables collation to utf8_general__ci after DB upgrade
Version: 1.0
Author: Federico Parodi - weLaika
Author URI: http://dev.welaika.com
License: MIT
License URI: http://opensource.org/licenses/MIT
*/
// If this file is called directly, abort.
if ( !defined( 'WPINC' ) ) {
die;
}
function tables_list() {
global $wpdb;
$sqlTablesResults = $wpdb->get_results( 'SHOW TABLES' );
$outputColumnName = "Tables_in_" . DB_NAME;
$tables = array();
foreach ($sqlTablesResults as $value) {
array_push($tables, $value->$outputColumnName);
}
return $tables;
}
function fix_tables() {
global $wpdb;
$tables = tables_list();
foreach ($tables as $table) {
$wpdb->get_results( "alter table $table CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci" );
}
}
add_action( 'after_db_upgrade', 'fix_tables' );
//or calling directly fix_tables(); if you already have an upgraded database
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment