精品軟體與實用教程
迅雷超連結在wordpress中無法使用的解決辦法
迅雷超連結 thunder協議在wordpress中無法使用,除了http:和https:外,當你輸入thunder:後,就會被系統自動過濾掉。網路上有一些資源是迅雷超連結形式的,有時候務必會使用到thunder協議,如何在wp中啟用thunder,magnet,ed2k等協議呢?下面分析一下怎樣啟用這些的協定支援。
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', 'newstelnet', 'irc', 'gopher', 'nntp', 'feed', 'telnet', 'irc', 'gopher', 'nntp', 'feed', 'telnet', 'irc', 'gopher', 'nntp', 'feed', 'telnet', ' 'fax', 'xmpp', 'webcal', 'urn' ); }
直接增加需要新增的新協議,例如:
if ( empty( $protocols ) ) { $protocols = array( 'http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'newstelnet', 'irc', 'gopher', 'nntp', 'feed', 'telnet', 'irc', 'gopher', 'nntp', 'feed', 'telnet', 'irc', 'gopher', 'nntp', 'feed', 'telnet', ' '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 ){ $protocolds; ss_allow_ed2k_protocol( $protocols ){ $protocols[] = 'ed2k'; return $protocols; } add_filter( 'kses_allowed_protocols' , 'ss_allow_thunder_protocol' ); 'ss_allow_magnet_protocol' ); add_filter( 'kses_allowed_protocols' , 'ss_allow_ed2k_protocol' );
將內容貼在functions.php檔案的最下端,如果你想增加其他協定的話,參考上面程式碼修改即可。
或者你安裝有程式碼插入功能的插件,在插件中,加入以上程式碼到插件中就可以了。