WordPressでHTTPからHTTPSへ移行する方法 (version5.7以降)

ワンクリックでいけるようになるらしい。。

サイトをHTTPからHTTPSへ移行するのは、SSL証明書の取得くらいが面倒なところですし、最近のレンタルサーバー屋さんでは安価で手続きも簡単なので、サーバー側での作業はそれほど手間では無いように思えます。

上記のサーバー作業に加え、WordPressで構築したサイトの場合は以下の2点を実施する必要があります。

  1. 「一般設定 > WordPress アドレス、サイトアドレス」をhttpsに変更する
  2. 既に投稿された記事のpost_contentのhttpになっているところをhttpsに修正する

以前(~5.6)までは2の作業が面倒で、post_contentの中身を正規表現で抜き出してきて洗い替えをするなどをやっていました。

5.7ベータのリリースノートによると、この作業が5.7からは不要になるかもしれないというお話です。

※ 以降は5.7ベータに更新されている前提で進みます。

サイトヘルスの見た目差分

5.2くらいから導入されたサイトヘルスチェック機能ですが、HTTPS接続の有無をテストしています。
5.7ではサイトがHTTPS対応していない場合にUpdateボタンが表示されるようになります。

does not use https

尚、wp-config.php などに WP_HOMEを記載している場合は以下のような画面になります。

when wp_home setting is enabled

Update機能を使いたい場合は定数を削除してくれということみたいです。

ボタンクリック時の処理

実際にボタンを押下した挙動については、紹介している良記事があったので↓こちらを参考に。

WordPress 5.7 Will Make It Easier to Migrate From HTTP to HTTPS
The next major release of WordPress will make it much easier for users to migrate their sites from HTTP to HTTPS. It int...

ボタン押下時はupdate_httpsというアクションを放り込んでいる様子が site-health.phpから伺えます。

if ( 'update_https' === $action ) {
	check_admin_referer( 'wp_update_https' );

	if ( ! current_user_can( 'update_https' ) ) {
		wp_die( __( 'Sorry, you are not allowed to update this site to HTTPS.' ), 403 );
	}

	if ( ! wp_is_https_supported() ) {
		wp_die( __( 'It looks like HTTPS is not supported for your website at this point.' ) );
	}

	$result = wp_update_urls_to_https();

	wp_redirect( add_query_arg( 'https_updated', (int) $result, wp_get_referer() ) );
	exit;
}

wp_update_urls_to_https自体はhomeとsiteurlのスキーマを変更するのみのようです。

/**
 * Update the 'home' and 'siteurl' option to use the HTTPS variant of their URL.
 *
 * If this update does not result in WordPress recognizing that the site is now using HTTPS (e.g. due to constants
 * overriding the URLs used), the changes will be reverted. In such a case the function will return false.
 *
 * @since 5.7.0
 *
 * @return bool True on success, false on failure.
 */
function wp_update_urls_to_https() {
	// Get current URL options.
	$orig_home    = get_option( 'home' );
	$orig_siteurl = get_option( 'siteurl' );

	// Get current URL options, replacing HTTP with HTTPS.
	$home    = str_replace( 'http://', 'https://', $orig_home );
	$siteurl = str_replace( 'http://', 'https://', $orig_siteurl );

	// Update the options.
	update_option( 'home', $home );
	update_option( 'siteurl', $siteurl );

	if ( ! wp_is_using_https() ) {
		// If this did not result in the site recognizing HTTPS as being used,
		// revert the change and return false.
		update_option( 'home', $orig_home );
		update_option( 'siteurl', $orig_siteurl );
		return false;
	}

	// Otherwise the URLs were successfully changed to use HTTPS.
	return true;
}

コンテンツ表示時の処理

コンテンツに埋め込まれたHTTPのURLをどこで修正しているかというと、コンテンツの表示時でした。
the_content に見覚えのないcallbackが追加されています。

add_filter( 'the_content', 'wp_replace_insecure_home_url' );

callbackは↓こちら。

/**
 * Replaces insecure HTTP URLs to the site in the given content, if configured to do so.
 *
 * This function replaces all occurrences of the HTTP version of the site's URL with its HTTPS counterpart, if
 * determined via {@see wp_should_replace_insecure_home_url()}.
 *
 * @since 5.7.0
 *
 * @param string $content Content to replace URLs in.
 * @return string Filtered content.
 */
function wp_replace_insecure_home_url( $content ) {
	if ( ! wp_should_replace_insecure_home_url() ) {
		return $content;
	}

	$https_url = home_url( '', 'https' );
	$http_url  = str_replace( 'https://', 'http://', $https_url );

	// Also replace potentially escaped URL.
	$escaped_https_url = str_replace( '/', '\/', $https_url );
	$escaped_http_url  = str_replace( '/', '\/', $http_url );

	return str_replace(
		array(
			$http_url,
			$escaped_http_url,
		),
		array(
			$https_url,
			$escaped_https_url,
		),
		$content
	);
}

要は、

  1. the_contentのタイミングで
  2. post_contentの中身を確認して
  3. スキーマがHTTPになっているサイトアドレスを見つけたら
  4. str_replaceする

ということになってるんですね。

あ、update_postはしないんだ、という安心感。

タイトルとURLをコピーしました