WordPress 超链接增加magnet、ed2k 新协议支持-解决火车头发布磁力被替换问题
<p style="box-sizing: inherit; margin-top: 0px !important; margin-right: 0px; margin-bottom: 20px; margin-left: 0px; line-height: 1.8; word-wrap: break-word; color: rgb(85, 85, 85); font-family: Lato, sans-serif; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">为防范SQL注入、XSS等攻击,Wordpress会在保存博客内容时候,对内容自动过滤,导致会对超链接中不支持的协议protocol头(例如:magnet:?xt=urn:btih: ),自动删除掉。</p><p style="box-sizing: inherit; margin: 0px 0px 20px; line-height: 1.8; word-wrap: break-word; color: rgb(85, 85, 85); font-family: Lato, sans-serif; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">网上搜索结果,大部分都是简单粗暴的方案:</p><p style="box-sizing: inherit; margin: 0px 0px 20px; line-height: 1.8; word-wrap: break-word; color: rgb(85, 85, 85); font-family: Lato, sans-serif; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">修改 wp-includes/post.php,将相关内容<span class="Apple-converted-space"> </span><code style="box-sizing: inherit; font-family: monospace, monospace; font-size: 1em;">$postarr = sanitize_post($postarr, 'db');</code>注释掉。</p><p style="box-sizing: inherit; margin: 0px 0px 20px; line-height: 1.8; word-wrap: break-word; color: rgb(85, 85, 85); font-family: Lato, sans-serif; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">但这样放弃了Wordpress原本的防范措施,不建议采用,且新版本这样直接注释会导致<a href="https://yglsr.com/tag/%e7%81%ab%e8%bd%a6%e5%a4%b4%e5%8f%91%e5%b8%83/" title="View all posts in 火车头发布" target="_blank" class="tag_link" style="box-sizing: inherit; background-color: transparent; color: rgb(52, 73, 94); text-decoration: underline; transition: all 0.3s ease; -webkit-transition: all 0.3s ease; font-weight: 700;">火车头发布</a>不上去。</p><p style="box-sizing: inherit; margin: 0px 0px 20px; line-height: 1.8; word-wrap: break-word; color: rgb(85, 85, 85); font-family: Lato, sans-serif; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">推荐的解决办法:</p><h3 style="box-sizing: inherit; color: rgb(85, 85, 85); font-weight: 700; line-height: 1.2; margin: 30px 0px 20px; font-size: 18px; padding-left: 10px; border-left-width: 4px; border-left-style: solid; border-left-color: rgb(15, 129, 251); font-family: Lato, sans-serif; font-style: normal; font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">方案一:在现有主题theme,增加新协议支持,主题如果更新需要再重新写入。</h3><p style="box-sizing: inherit; margin: 0px 0px 20px; line-height: 1.8; word-wrap: break-word; color: rgb(85, 85, 85); font-family: Lato, sans-serif; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">修改 wp-content/themes/对应的theme名称/functions.php ,增加如下内容:</p><pre style="box-sizing: inherit; font-family: 'courier new'; font-size: 14px; overflow-y: scroll; margin: 20px 0px; padding: 30px; border: 0px; background-color: rgb(43, 48, 59); color: rgb(111, 187, 114); font-style: normal; font-variant: normal; font-weight: normal; line-height: 20px; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><?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' );
</pre><h3 style="box-sizing: inherit; color: rgb(85, 85, 85); font-weight: 700; line-height: 1.2; margin: 30px 0px 20px; font-size: 18px; padding-left: 10px; border-left-width: 4px; border-left-style: solid; border-left-color: rgb(15, 129, 251); font-family: Lato, sans-serif; font-style: normal; font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">方案二:修改 wp-includes/functions.php,增加新协议支持,此方案缺点是升级wp版本需要再重新设置。</h3><p style="box-sizing: inherit; margin: 0px 0px 20px; line-height: 1.8; word-wrap: break-word; color: rgb(85, 85, 85); font-family: Lato, sans-serif; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">修改 wp-includes/functions.php 的function wp_allowed_protocols()</p><pre style="box-sizing: inherit; font-family: 'courier new'; font-size: 14px; overflow-y: scroll; margin: 20px 0px; padding: 30px; border: 0px; background-color: rgb(43, 48, 59); color: rgb(111, 187, 114); font-style: normal; font-variant: normal; font-weight: normal; line-height: 20px; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">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' );
}</pre><p style="box-sizing: inherit; margin: 0px 0px 20px; line-height: 1.8; word-wrap: break-word; color: rgb(85, 85, 85); font-family: Lato, sans-serif; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">直接增加需要添加的新协议,例如:</p><pre style="box-sizing: inherit; font-family: 'courier new'; font-size: 14px; overflow-y: scroll; margin: 20px 0px; padding: 30px; border: 0px; background-color: rgb(43, 48, 59); color: rgb(111, 187, 114); font-style: normal; font-variant: normal; font-weight: normal; line-height: 20px; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"> 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');
}
</pre><p style="box-sizing: inherit; margin-top: 0px; margin-right: 0px; margin-bottom: 0px !important; margin-left: 0px; line-height: 1.8; word-wrap: break-word; color: rgb(85, 85, 85); font-family: Lato, sans-serif; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">此种方案存在一个问题:由于是全局性修改,Wordpress每一次版本升级,都会覆盖修改functions.php 文件,必须手工再次修改此文件。</p>
页:
[1]