esc_url_raw()

URL过滤

描述

esc_url_raw() 函数类似与 esc_url() (实际上 esc_url_raw 函数中就使用了 esc_url ),但是不同于 esc_url(),它不会将字符转换成 HTML 实体用于显示,它的结果适用于在数据库查询等操作,重定向,或者 HTTP 请求中。

用法

<?php esc_url_raw( $url, $protocols ); ?>

参数

$url

(string) (必填) 将要被清理过滤的 URL

默认值: None

$protocols

(array) (可选) 可以接受协议的数组,如果没有设置,默认是:’http’, ‘https’, ‘ftp’, ‘ftps’, ‘mailto’, ‘news’, ‘irc’, ‘gopher’, ‘nntp’, ‘feed’, ‘telnet’。

默认值: null

返回值

(string) 

The cleaned $url after the ‘clean_url‘ filter is applied. An empty string is returned if $url specifies a protocol other than those in $protocols, or if $url contains an empty string.

已经清理过滤的 URL

示例

<!– Right –>
<?php
    $url = ‘http://wordpress.org’;
    $response = wp_remote_get( esc_url_raw( $url ) ); // no need to escape entities
    if ( !is_wp_error( $response ) ) {
        echo wp_remote_retrieve_body( $response );
    }
?>

<!– Wrong! Use esc_url instead! –>
<img src=’<?php echo esc_url_raw( $url ); ?>‘ />
<a href=’<?php echo esc_url_raw( $url ); ?>‘>WordPress</a>

注意

开发者可以通过 cleaned_url 这个 filter 接口队返回 $url 进行再次过滤。

源文件

esc_url_raw() 函数的代码位于 wp-includes/formatting.php.

/**
* Performs esc_url() for database usage.
*
* @since 2.8.0
*
* @param string $url       The URL to be cleaned.
* @param array  $protocols An array of acceptable protocols.
* @return string The cleaned URL.
*/

function esc_url_raw( $url, $protocols = null ) {

return esc_url( $url, $protocols, ‘db’ );

}
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索