函数文档

wp_update_https_migration_required()

💡 云策文档标注

概述

wp_update_https_migration_required() 函数用于在 URL 从 HTTP 更新为 HTTPS 时,根据需要更新 'https_migration_required' 选项。它通过钩子 'update_option_home' 触发,确保迁移状态正确设置。

关键要点

  • 函数在 WordPress 安装过程中不执行任何操作,通过 wp_installing() 检查。
  • 如果新 URL 不是旧 URL 的 HTTPS 版本,则删除 'https_migration_required' 选项。
  • 对于新站点(fresh_site 为 true),迁移不需要,选项设为 false;否则设为 true。
  • 使用 untrailingslashit() 处理 URL 比较,避免尾部斜杠影响。
  • 相关函数包括 delete_option(), update_option(), get_option() 等。

代码示例

function wp_update_https_migration_required( $old_url, $new_url ) {
    // Do nothing if WordPress is being installed.
    if ( wp_installing() ) {
        return;
    }

    // Delete/reset the option if the new URL is not the HTTPS version of the old URL.
    if ( untrailingslashit( (string) $old_url ) !== str_replace( 'https://', 'http://', untrailingslashit( (string) $new_url ) ) ) {
        delete_option( 'https_migration_required' );
        return;
    }

    // If this is a fresh site, there is no content to migrate, so do not require migration.
    $https_migration_required = get_option( 'fresh_site' ) ? false : true;

    update_option( 'https_migration_required', $https_migration_required );
}

📄 原文内容

Updates the ‘https_migration_required’ option if needed when the given URL has been updated from HTTP to HTTPS.

Description

If this is a fresh site, a migration will not be required, so the option will be set as false.

This is hooked into the ‘update_option_home’ action.

Parameters

$old_urlmixedrequired
Previous value of the URL option.
$new_urlmixedrequired
New value of the URL option.

Source

function wp_update_https_migration_required( $old_url, $new_url ) {
	// Do nothing if WordPress is being installed.
	if ( wp_installing() ) {
		return;
	}

	// Delete/reset the option if the new URL is not the HTTPS version of the old URL.
	if ( untrailingslashit( (string) $old_url ) !== str_replace( 'https://', 'http://', untrailingslashit( (string) $new_url ) ) ) {
		delete_option( 'https_migration_required' );
		return;
	}

	// If this is a fresh site, there is no content to migrate, so do not require migration.
	$https_migration_required = get_option( 'fresh_site' ) ? false : true;

	update_option( 'https_migration_required', $https_migration_required );
}

Changelog

Version Description
5.7.0 Introduced.