23
2025
04
15:31:23

Wordpress 超链接增加 magnet、ed2k、tg 新协议支持

需要在Wordpress的超链接中增加对magnet、ed2k等新协议的支持,效果类似:

肖申克的救赎

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

google结果,大部分都是简单粗暴的方案:

修改 wp-includes/post.php,将相关内容注释掉。

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

但这样放弃了Wordpress原本的防范措施,不建议采用。

如果想让网页实现跳转到telegram,使用tg开头的链接,虽然使用此方法可行但也不推荐。

推荐的解决办法:

方案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' ,'magnet','ed2k','tg');
        }

此种方案存在一个问题:由于是全局性修改,Wordpress每一次版本升级,都会覆盖修改functions.php 文件,必须手工再次修改此文件。

如果想让Wordpress 支持 tg:// 实现跳转到 Telegram,使用tg开头的链接,则使用此方法可行。

vi /var/www/html/wp-includes/functions.php

方案2:在现有主题theme,增加新协议支持

修改 wp-content/themes/对应的theme名称/functions.php ,增加如下内容:

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_magnet_protocol' );
add_filter( 'kses_allowed_protocols' , 'ss_allow_ed2k_protocol' );
function ss_allow_tg_protocol( $protocols ){
$protocols[] = 'tg';
return $protocols;
}
add_filter( 'kses_allowed_protocols' , 'ss_allow_tg_protocol' );

设置清除网页缓存,刷新即发现链接格式已经生效。




推荐本站淘宝优惠价购买喜欢的宝贝:

image.png

本文链接:https://zblog.hqyman.cn/post/10748.html 非本站原创文章欢迎转载,原创文章需保留本站地址!

分享到:
打赏





休息一下~~


« 上一篇 下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

请先 登录 再评论,若不是会员请先 注册

您的IP地址是: