函数文档

clean_url()

💡 云策文档标注

概述

clean_url() 函数用于检查和清理 URL,但自 WordPress 3.0.0 起已弃用,建议使用 esc_url() 替代。该函数会移除 URL 中的特定字符,并根据上下文(如显示或数据库)处理转义。

关键要点

  • clean_url() 已弃用,自 3.0.0 版本起应使用 esc_url() 或 sanitize_url()(针对数据库上下文)。
  • 函数接受三个参数:$url(必需,要清理的 URL)、$protocols(可选,可接受的协议数组)、$context(可选,URL 使用上下文,默认为 'display')。
  • 返回经过 'clean_url' 过滤器处理的清理后 URL 字符串。
  • 相关函数包括 esc_url() 和 _deprecated_function()。

代码示例

function clean_url( $url, $protocols = null, $context = 'display' ) {
    if ( $context == 'db' )
        _deprecated_function( 'clean_url( $context = 'db' )', '3.0.0', 'sanitize_url()' );
    else
        _deprecated_function( __FUNCTION__, '3.0.0', 'esc_url()' );
    return esc_url( $url, $protocols, $context );
}

注意事项

  • 在 WordPress 3.0.0 及以上版本中,避免直接使用 clean_url(),以保持代码兼容性和安全性。
  • 根据 $context 参数选择替代函数:'display' 时用 esc_url(),'db' 时用 sanitize_url()。

📄 原文内容

Checks and cleans a URL.

Description

A number of characters are removed from the URL. If the URL is for displaying (the default behavior) ampersands are also replaced. The ‘clean_url’ filter is applied to the returned cleaned URL.

See also

Parameters

$urlstringrequired
The URL to be cleaned.
$protocolsarrayoptional
An array of acceptable protocols.

Default:null

$contextstringoptional
How the URL will be used. Default is 'display'.

Return

string The cleaned $url after the ‘clean_url’ filter is applied.

Source

function clean_url( $url, $protocols = null, $context = 'display' ) {
	if ( $context == 'db' )
		_deprecated_function( 'clean_url( $context = 'db' )', '3.0.0', 'sanitize_url()' );
	else
		_deprecated_function( __FUNCTION__, '3.0.0', 'esc_url()' );
	return esc_url( $url, $protocols, $context );
}

Changelog

Version Description
3.0.0 Deprecated. Use esc_url()
1.2.0 Introduced.