esc_url() 是用來過濾網址裡面不安全的內容的,看說明似乎在開發 plugin 會比較常用到。

具體目地如下:

  1. 拒絕不是下面協議的 URL (defaulting to http, https, ftp, ftps, mailto, news, irc, gopher, nntp, feed, and telnet)
  2. 消除無效字串和刪除危險字串。
  3. 將字串轉換成 HTML 實體,並且將 & 和 單引號(') 轉換成數字實體:&#038, &#039。

 

用法:

esc_url( $url, $protocols, $_context );

 

參數:

$url
(string) (required) 將要被清理過濾的 URL
Default: 無

$protocols
(array) (optional) 可以接受協議的數組,如果沒有設置,默認是:'http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet'。
Default: 無

$_context
(string) (optional) 如何返回 URL。
Default: 'display'

 

這個函式會回傳已經清理過濾後的 url。

 

參考來源:
esc_url()
WordPress 常用函数 / esc_url