WordPress 6.9 开始 URL 转义函数可以支持 HTTPS作为默认协议

💡 原文中文,约900字,阅读约需3分钟。
📝

内容提要

在 WordPress 6.9 之前,esc_url() 函数仅支持 http:// 协议。自 6.9 起,如果 $protocols 的第一个元素为 'https',则会添加 https://,推荐默认使用 HTTPS。

🎯

关键要点

  • 在 WordPress 6.9 之前,esc_url() 函数只能添加 http:// 协议。

  • WordPress 6.9 开始支持 HTTPS 作为默认协议。

  • 当 esc_url() 的 $protocols 参数第一个元素为 'https' 时,会在 URL 前添加 https://。

  • 如果 $protocols 参数中只有 'https',则 esc_url() 返回空字符串。

  • 推荐在保持向后兼容性的前提下,默认使用 'https'。

延伸问答

WordPress 6.9 之前的 esc_url() 函数有什么限制?

在 WordPress 6.9 之前,esc_url() 函数只能添加 http:// 协议,无法支持其他协议。

WordPress 6.9 如何处理 HTTPS 协议?

自 WordPress 6.9 起,如果 esc_url() 的 $protocols 参数第一个元素为 'https',则会在 URL 前添加 https://。

如果 $protocols 参数中只有 'https',esc_url() 会返回什么?

如果 $protocols 参数中只有 'https',则 esc_url() 返回空字符串。

在使用 esc_url() 时,推荐的默认协议是什么?

推荐在保持向后兼容性的前提下,默认使用 'https'。

WordPress 6.9 和旧版本在处理不完整 URL 时有什么不同?

WordPress 6.9 会在不完整的 URL 前添加 https://,而旧版本则继续添加 http://。

如何在 esc_url() 中优先使用 HTTPS?

在 esc_url() 中,可以将 $protocols 参数设置为 array('https', 'http'),这样会优先使用 https://。

➡️

继续阅读