2016-12-28 84 views
0
外部安全的URL

我使用的功能esc_url_raw ..产生的WordPress

WordPress function: 
esc_url_raw(string $url, array $protocols = null) 

如何打印以 “https” 网址?

Example: 
input: http://example.com 
ouput: https://example.com 

Doc Function

回答

0

可以提供在$protocols变量的可接受的协议。它可以是array()的协议,如http,https。您可以参考wp_allowed_protocols获取允许的协议。

为了您的答案,您可以提供$protocols的数组作为array('http', 'https')作为esc_url_raw的参数。

如果你实际上是寻找到一个URL从http转换为https,那么你可以定义是这样的:

function convert_to_https(url){ 
    $new_url = preg_replace("/^http:/i", "https:", $url); 
    return $new_url; 
} 


$https_url = convert_to_https('http://example.com'); 
echo $https_url; // https://example.com 

// then escape the URL 
esc_url_raw($https_url); 
+0

谢谢,功能** ** esc_url_raw什么可以让我为验证URL,逃离不属于该网址的字符。 –