maybe_convert_table_to_utf8mb4

The timeline below displays how wordpress function maybe_convert_table_to_utf8mb4 has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 5.5

/**
 * If a table only contains utf8 or utf8mb4 columns, convert it to utf8mb4.
 *
 * @since 4.2.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string $table The table to convert.
 * @return bool True if the table was converted, false if it wasn't.
 */
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");
}

WordPress Version: 5.3

/**
 * If a table only contains utf8 or utf8mb4 columns, convert it to utf8mb4.
 *
 * @since 4.2.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string $table The table to convert.
 * @return bool true if the table was converted, false if it wasn't.
 */
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");
}

WordPress Version: 4.3

/**
 * If a table only contains utf8 or utf8mb4 columns, convert it to utf8mb4.
 *
 * @since 4.2.0
 *
 * @global wpdb  $wpdb
 *
 * @param string $table The table to convert.
 * @return bool true if the table was converted, false if it wasn't.
 */
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");
}

WordPress Version: .20

/**
 * If a table only contains utf8 or utf8mb4 columns, convert it to utf8mb4.
 *
 * @since 4.2.0
 *
 * @param string $table The table to convert.
 * @return bool true if the table was converted, false if it wasn't.
 */
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");
}

WordPress Version: 2.2

/**
 * If a table only contains utf8 or utf8mb4 columns, convert it to utf8mb4.
 *
 * @since 4.2.0
 *
 * @param string $table The table to convert.
 * @return bool true if the table was converted, false if it wasn't.
 */
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;
            }
        }
    }
    return $wpdb->query("ALTER TABLE {$table} CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
}

WordPress Version: .10

/**
 * If a table only contains utf8 or utf8mb4 columns, convert it to utf8mb4.
 *
 * @since 4.2.0
 *
 * @param string $table The table to convert.
 * @return bool true if the table was converted, false if it wasn't.
 */
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");
}

WordPress Version: 4.2

/**
 * If a table only contains utf8 or utf8mb4 columns, convert it to utf8mb4.
 *
 * @since 4.2.0
 *
 * @param string $table The table to convert.
 * @return bool true if the table was converted, false if it wasn't.
 */
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;
            }
        }
    }
    return $wpdb->query("ALTER TABLE {$table} CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
}