WordPress 增加 迅雷超链接 thunder,magnet,ed2k等协议支持!

迅雷超链接 在wordpress中无法使用的解决办法

协议在wordpress中无法使用,除了http:和https:外,当你输入thunder:后,就会被系统自动过滤掉。网上有一些资源是超链接形式的,有时候务必会使用到thunder协议,如何在wp中启用 thunder,等协议呢?下面分析一下怎样启用这些的协议支持。

WordPress 增加 迅雷超链接 thunder,magnet,ed2k等协议支持!-1

WordPress的超链接中增加 迅雷超链接 thunder、magnet、ed2k等新协议

WordPress为防范SQL注入、XSS等攻击,会在保存文章内容时候,对内容进行自动过滤,导致会对超链接中不支持的协议protocol头(例如:thunder://,magnet:?xt=urn:btih: ),自动删除掉。

最简单粗暴的解决办法,就是关闭掉过滤功能。

修改 wp-includes/post.php文件,将 $postarr = sanitize_post($postarr, 'db'); 注释掉。

//$postarr = sanitize_post($postarr, 'db');

这个方法很简单实用,但是这样一来,就等于放弃了 WordPress 的防范功能,在安全方面造成了隐患,所以不建议采用这种做法。

如何有效的解决 thunder、magnet、ed2k 等新协议支持

方法1:修改 wp-includes/functions.php,增加新协议支持

修改 wp-includes/functions.php 的 function wp_allowed_protocols()

if ( empty( $protocols ) ) {
$protocols = array( 'http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed',
'telnet', 'mms', 'rtsp', 'sms', 'svn', 'tel', 'fax', 'xmpp', 'webcal', 'urn' );
}

直接增加需要添加的新协议,例如:

if ( empty( $protocols ) ) {
$protocols = array( 'http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed',
'telnet', 'mms', 'rtsp', 'sms', 'svn', 'tel', 'fax', 'xmpp', 'webcal', 'urn' , 'thunder' , 'magnet' ,'ed2k');
}

这个方法存在一个问题:由于修改的是全局变量wp-includes/functions.php文件,Wordpress的每一次新版本升级,都会覆盖 functions.php 文件,必须手工再次修改此文件。如果你的wp版本更新不是很频繁的话,可以使用这种方法。

方法2:在主题中修改functions.php,增加新协议支持

修改 wp-content/themes/主题名称/functions.php ,增加如下内容:

function ss_allow_thunder_protocol( $protocols ){
$protocols[] = 'thunder';
return $protocols;
}
function ss_allow_magnet_protocol( $protocols ){
$protocols[] = 'magnet';
return $protocols;
}
function ss_allow_ed2k_protocol( $protocols ){
$protocols[] = 'ed2k';
return $protocols;
}
add_filter( 'kses_allowed_protocols' , 'ss_allow_thunder_protocol' );
add_filter( 'kses_allowed_protocols' , 'ss_allow_magnet_protocol' );
add_filter( 'kses_allowed_protocols' , 'ss_allow_ed2k_protocol' );

将内容粘贴在functions.php文件的最下端,如果你想增加其他协议的话,参考上面代码修改即可。

或者你安装有代码插入功能的插件,在插件中,添加以上代码到插件中就可以了。

推荐代码管理插件:强大灵活的代码管理插件 WPCode 可以按条件插入任何JS、CSS、PHP代码

评分

留下评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注