if ( $wp_current_db_version < 33055 && 'utf8mb4' === $wpdb->charset ) {
$tables = $wpdb->tables( 'blog' );
$tables = $wpdb->tables( 'all' );
if ( ! wp_should_upgrade_global_tables() ) {
$global_tables = $wpdb->tables( 'global' );
$tables = array_diff_assoc( $tables, $global_tables );
foreach ( $tables as $table ) {
maybe_convert_table_to_utf8mb4( $table );
* Executes comments changes made in WordPress 4.3.0.
* @global wpdb $wpdb WordPress database abstraction object.
function upgrade_430_fix_comments() {
$content_length = $wpdb->get_col_length( $wpdb->comments, 'comment_content' );
if ( is_wp_error( $content_length ) ) {
if ( false === $content_length ) {
} elseif ( ! is_array( $content_length ) ) {
$length = (int) $content_length > 0 ? (int) $content_length : 65535;
if ( 'byte' !== $content_length['type'] || 0 === $content_length['length'] ) {
// Sites with malformed DB schemas are on their own.
$allowed_length = (int) $content_length['length'] - 10;
$comments = $wpdb->get_results(
"SELECT `comment_ID` FROM `{$wpdb->comments}`
WHERE `comment_date_gmt` > '2015-04-26'
AND LENGTH( `comment_content` ) >= {$allowed_length}
AND ( `comment_content` LIKE '%<%' OR `comment_content` LIKE '%>%' )"
foreach ( $comments as $comment ) {
wp_delete_comment( $comment->comment_ID, true );
* Executes changes made in WordPress 4.3.1.
// Fix incorrect cron entries for term splitting.
$cron_array = _get_cron_array();
if ( isset( $cron_array['wp_batch_split_terms'] ) ) {
unset( $cron_array['wp_batch_split_terms'] );
_set_cron_array( $cron_array );
* Executes changes made in WordPress 4.4.0.
* @global int $wp_current_db_version The old (current) database version.
* @global wpdb $wpdb WordPress database abstraction object.
global $wp_current_db_version, $wpdb;
if ( $wp_current_db_version < 34030 ) {
$wpdb->query( "ALTER TABLE {$wpdb->options} MODIFY option_name VARCHAR(191)" );
// Remove the unused 'add_users' role.
foreach ( $roles->role_objects as $role ) {
if ( $role->has_cap( 'add_users' ) ) {
$role->remove_cap( 'add_users' );
* Executes changes made in WordPress 4.5.0.
* @global int $wp_current_db_version The old (current) database version.
* @global wpdb $wpdb WordPress database abstraction object.
global $wp_current_db_version, $wpdb;
if ( $wp_current_db_version < 36180 ) {
wp_clear_scheduled_hook( 'wp_maybe_auto_update' );
// Remove unused email confirmation options, moved to usermeta.
if ( $wp_current_db_version < 36679 && is_multisite() ) {
$wpdb->query( "DELETE FROM $wpdb->options WHERE option_name REGEXP '^[0-9]+_new_email$'" );
// Remove unused user setting for wpLink.
delete_user_setting( 'wplink' );
* Executes changes made in WordPress 4.6.0.
* @global int $wp_current_db_version The old (current) database version.
global $wp_current_db_version;
// Remove unused post meta.
if ( $wp_current_db_version < 37854 ) {
delete_post_meta_by_key( '_post_restored_from' );
// Remove plugins with callback as an array object/method as the uninstall hook, see #13786.
if ( $wp_current_db_version < 37965 ) {
$uninstall_plugins = get_option( 'uninstall_plugins', array() );
if ( ! empty( $uninstall_plugins ) ) {
foreach ( $uninstall_plugins as $basename => $callback ) {
if ( is_array( $callback ) && is_object( $callback[0] ) ) {
unset( $uninstall_plugins[ $basename ] );
update_option( 'uninstall_plugins', $uninstall_plugins );
* Executes changes made in WordPress 5.0.0.
* Executes changes made in WordPress 5.1.0.
delete_site_option( 'upgrade_500_was_gutenberg_active' );
* Executes changes made in WordPress 5.3.0.
* The `admin_email_lifespan` option may have been set by an admin that just logged in,
* saw the verification screen, clicked on a button there, and is now upgrading the db,
* or by populate_options() that is called earlier in upgrade_all().
* In the second case `admin_email_lifespan` should be reset so the verification screen
* is shown next time an admin logs in.
if ( function_exists( 'current_user_can' ) && ! current_user_can( 'manage_options' ) ) {
update_option( 'admin_email_lifespan', 0 );
* Executes changes made in WordPress 5.5.0.
global $wp_current_db_version;
if ( $wp_current_db_version < 48121 ) {
$comment_previously_approved = get_option( 'comment_whitelist', '' );
update_option( 'comment_previously_approved', $comment_previously_approved );
delete_option( 'comment_whitelist' );
if ( $wp_current_db_version < 48575 ) {
// Use more clear and inclusive language.
$disallowed_list = get_option( 'blacklist_keys' );
* This option key was briefly renamed `blocklist_keys`.
* Account for sites that have this key present when the original key does not exist.
if ( false === $disallowed_list ) {
$disallowed_list = get_option( 'blocklist_keys' );
update_option( 'disallowed_keys', $disallowed_list );
delete_option( 'blacklist_keys' );
delete_option( 'blocklist_keys' );
if ( $wp_current_db_version < 48748 ) {
update_option( 'finished_updating_comment_type', 0 );
wp_schedule_single_event( time() + ( 1 * MINUTE_IN_SECONDS ), 'wp_update_comment_type_batch' );
* Executes changes made in WordPress 5.6.0.
global $wp_current_db_version, $wpdb;
if ( $wp_current_db_version < 49572 ) {
* Clean up the `post_category` column removed from schema in version 2.8.0.
* Its presence may conflict with `WP_Post::__get()`.
$post_category_exists = $wpdb->get_var( "SHOW COLUMNS FROM $wpdb->posts LIKE 'post_category'" );
if ( ! is_null( $post_category_exists ) ) {
$wpdb->query( "ALTER TABLE $wpdb->posts DROP COLUMN `post_category`" );
* When upgrading from WP < 5.6.0 set the core major auto-updates option to `unset` by default.
* This overrides the same option from populate_options() that is intended for new installs.
* See https://core.trac.wordpress.org/ticket/51742.
update_option( 'auto_update_core_major', 'unset' );
if ( $wp_current_db_version < 49632 ) {
* Regenerate the .htaccess file to add the `HTTP_AUTHORIZATION` rewrite rule.
* See https://core.trac.wordpress.org/ticket/51723.
save_mod_rewrite_rules();
if ( $wp_current_db_version < 49735 ) {
delete_transient( 'dirsize_cache' );
if ( $wp_current_db_version < 49752 ) {
$results = $wpdb->get_results(
"SELECT 1 FROM {$wpdb->usermeta} WHERE meta_key = %s LIMIT 1",
WP_Application_Passwords::USERMETA_KEY_APPLICATION_PASSWORDS
if ( ! empty( $results ) ) {
$network_id = get_main_network_id();
update_network_option( $network_id, WP_Application_Passwords::OPTION_KEY_IN_USE, 1 );
* Executes network-level upgrade routines.
* @global int $wp_current_db_version The old (current) database version.
* @global wpdb $wpdb WordPress database abstraction object.
function upgrade_network() {
global $wp_current_db_version, $wpdb;
// Always clear expired transients.
delete_expired_transients( true );
if ( $wp_current_db_version < 11549 ) {
$wpmu_sitewide_plugins = get_site_option( 'wpmu_sitewide_plugins' );
$active_sitewide_plugins = get_site_option( 'active_sitewide_plugins' );
if ( $wpmu_sitewide_plugins ) {
if ( ! $active_sitewide_plugins ) {
$sitewide_plugins = (array) $wpmu_sitewide_plugins;
$sitewide_plugins = array_merge( (array) $active_sitewide_plugins, (array) $wpmu_sitewide_plugins );
update_site_option( 'active_sitewide_plugins', $sitewide_plugins );
delete_site_option( 'wpmu_sitewide_plugins' );
delete_site_option( 'deactivated_sitewide_plugins' );
while ( $rows = $wpdb->get_results( "SELECT meta_key, meta_value FROM {$wpdb->sitemeta} ORDER BY meta_id LIMIT $start, 20" ) ) {
foreach ( $rows as $row ) {
$value = $row->meta_value;
if ( ! @unserialize( $value ) ) {
$value = stripslashes( $value );
if ( $value !== $row->meta_value ) {
update_site_option( $row->meta_key, $value );
if ( $wp_current_db_version < 13576 ) {
update_site_option( 'global_terms_enabled', '1' );
if ( $wp_current_db_version < 19390 ) {
update_site_option( 'initial_db_version', $wp_current_db_version );
if ( $wp_current_db_version < 19470 ) {
if ( false === get_site_option( 'active_sitewide_plugins' ) ) {
update_site_option( 'active_sitewide_plugins', array() );
if ( $wp_current_db_version < 20148 ) {
// 'allowedthemes' keys things by stylesheet. 'allowed_themes' keyed things by name.
$allowedthemes = get_site_option( 'allowedthemes' );
$allowed_themes = get_site_option( 'allowed_themes' );
if ( false === $allowedthemes && is_array( $allowed_themes ) && $allowed_themes ) {
$themes = wp_get_themes();
foreach ( $themes as $stylesheet => $theme_data ) {
if ( isset( $allowed_themes[ $theme_data->get( 'Name' ) ] ) ) {
$converted[ $stylesheet ] = true;
update_site_option( 'allowedthemes', $converted );
delete_site_option( 'allowed_themes' );
if ( $wp_current_db_version < 21823 ) {
update_site_option( 'ms_files_rewriting', '1' );
if ( $wp_current_db_version < 24448 ) {
$illegal_names = get_site_option( 'illegal_names' );
if ( is_array( $illegal_names ) && count( $illegal_names ) === 1 ) {
$illegal_name = reset( $illegal_names );
$illegal_names = explode( ' ', $illegal_name );
update_site_option( 'illegal_names', $illegal_names );
if ( $wp_current_db_version < 31351 && 'utf8mb4' === $wpdb->charset ) {
if ( wp_should_upgrade_global_tables() ) {
$wpdb->query( "ALTER TABLE $wpdb->usermeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" );
$wpdb->query( "ALTER TABLE $wpdb->site DROP INDEX domain, ADD INDEX domain(domain(140),path(51))" );
$wpdb->query( "ALTER TABLE $wpdb->sitemeta DROP INDEX meta_key, ADD INDEX meta_key(meta_key(191))" );
$wpdb->query( "ALTER TABLE $wpdb->signups DROP INDEX domain_path, ADD INDEX domain_path(domain(140),path(51))" );
$tables = $wpdb->tables( 'global' );
// sitecategories may not exist.
if ( ! $wpdb->get_var( "SHOW TABLES LIKE '{$tables['sitecategories']}'" ) ) {
unset( $tables['sitecategories'] );
foreach ( $tables as $table ) {
maybe_convert_table_to_utf8mb4( $table );
if ( $wp_current_db_version < 33055 && 'utf8mb4' === $wpdb->charset ) {
if ( wp_should_upgrade_global_tables() ) {
$indexes = $wpdb->get_results( "SHOW INDEXES FROM $wpdb->signups" );
foreach ( $indexes as $index ) {
if ( 'domain_path' === $index->Key_name && 'domain' === $index->Column_name && 140 != $index->Sub_part ) {
$wpdb->query( "ALTER TABLE $wpdb->signups DROP INDEX domain_path, ADD INDEX domain_path(domain(140),path(51))" );
$tables = $wpdb->tables( 'global' );
// sitecategories may not exist.
if ( ! $wpdb->get_var( "SHOW TABLES LIKE '{$tables['sitecategories']}'" ) ) {
unset( $tables['sitecategories'] );
foreach ( $tables as $table ) {
maybe_convert_table_to_utf8mb4( $table );
if ( $wp_current_db_version < 44467 ) {
$network_id = get_main_network_id();
delete_network_option( $network_id, 'site_meta_supported' );
is_site_meta_supported();
// General functions we use to actually do stuff.
* Creates a table in the database, if it doesn't already exist.
* This method checks for an existing database and creates a new one if it's not
* already present. It doesn't rely on MySQL's "IF NOT EXISTS" statement, but chooses
* to query all tables first and then run the SQL statement creating the table.
* @global wpdb $wpdb WordPress database abstraction object.
* @param string $table_name Database table name.
* @param string $create_ddl SQL statement to create table.
* @return bool True on success or if the table already exists. False on failure.
function maybe_create_table( $table_name, $create_ddl ) {
$query = $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $table_name ) );
if ( $wpdb->get_var( $query ) === $table_name ) {
// Didn't find it, so try to create it.
$wpdb->query( $create_ddl );
// We cannot directly tell that whether this succeeded!
if ( $wpdb->get_var( $query ) === $table_name ) {
* Drops a specified index from a table.
* @global wpdb $wpdb WordPress database abstraction object.
* @param string $table Database table name.
* @param string $index Index name to drop.
* @return true True, when finished.
function drop_index( $table, $index ) {