wp_update_urls_to_https

函式
wp_update_urls_to_https ( No parameters )
返回值
  • (bool) True on success, false on failure.
定義位置
相關方法
wp_update_user_countswp_update_themeswp_update_custom_css_postwp_update_postwp_update_site
引入
5.7.0
棄用
-

wp_update_urls_to_https是一個更新WordPress資料庫中所有URLs的函式,以使用HTTPS而不是HTTP: 該函式用於確保資料庫中的所有URL都是安全和加密的。

更新’home’和’siteurl’選項,以使用其URL的HTTPS變體。

如果這個更新沒有導致WordPress認識到該站點現在正在使用HTTPS(例如,由於常量覆蓋了所使用的URL),這些變化將被恢復。在這種情況下,該函式將返回false。

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;
}

常見問題

FAQs
檢視更多 >