WordPress在 检查和清理URL地址时需要用到一个参数就是$protocols,即是一些列可接受的协议,默认返回wp_allowed_protocols()。
wp_allowed_protocols(): string[]
返回允许的协议数组,允许的协议包括http、https、ftp、ftps、mailto、news、irc、irc6、ircs、gopher、nntp、feed、telnet、mms、rtsp、sms、svn、tel、fax、xmpp、webcal、urn、javascript。
如果不在协议列表中,就会返回空字符串,所以如果你想要添加一些额外的协议到允许的协议列表中,可以使用以下代码实现:
apply_filters( 'kses_allowed_protocols', string[] $protocols )
比如:
/**
* Extend list of allowed protocols.
*
* @param array $protocols List of default protocols allowed by WordPress.
*
* @return array $protocols Updated list including new protocols.
*/
function wporg_extend_allowed_protocols( $protocols ){
$protocols[] = 'skype';
$protocols[] = 'spotify';
$protocols[] = 'macappstores';
return $protocols;
}
add_filter( 'kses_allowed_protocols' , 'wporg_extend_allowed_protocols' );