Created
September 25, 2015 14:27
-
-
Save alessandro-fazzi/8a3dc7da62017b87e9aa to your computer and use it in GitHub Desktop.
Force
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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