maybe_convert_table_to_utf8mb4() WordPress Function
The maybe_convert_table_to_utf8mb4() function is used to convert a table to the UTF-8 character set and collation. This function is only needed if you are using a version of MySQL earlier than 5.5.3, or if you are using a version of WordPress earlier than 4.2.0.
maybe_convert_table_to_utf8mb4( string $table ) #
If a table only contains utf8 or utf8mb4 columns, convert it to utf8mb4.
Parameters
- $table
(string)(Required)The table to convert.
Return
(bool) True if the table was converted, false if it wasn't.
Source
File: wp-admin/includes/upgrade.php
2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 | function maybe_convert_table_to_utf8mb4( $table ) { global $wpdb ; $results = $wpdb ->get_results( "SHOW FULL COLUMNS FROM `$table`" ); if ( ! $results ) { return false; } foreach ( $results as $column ) { if ( $column ->Collation ) { list( $charset ) = explode ( '_' , $column ->Collation ); $charset = strtolower ( $charset ); if ( 'utf8' !== $charset && 'utf8mb4' !== $charset ) { // Don't upgrade tables that have non-utf8 columns. return false; } } } $table_details = $wpdb ->get_row( "SHOW TABLE STATUS LIKE '$table'" ); if ( ! $table_details ) { return false; } list( $table_charset ) = explode ( '_' , $table_details ->Collation ); $table_charset = strtolower ( $table_charset ); if ( 'utf8mb4' === $table_charset ) { return true; } return $wpdb ->query( "ALTER TABLE $table CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci" ); } |
Expand full source codeCollapse full source codeView on TracView on GitHub
Changelog
Version | Description |
---|---|
4.2.0 | Introduced. |