WordPress 插件:去除评论中的超链接
因为访客很少,我把Akismet也停用了,没想到最近几乎每天都会收一条英文的SPAM(这个…难道本少侠的名声都传到海外去了),为此特想了个法子去除评论中的超链接,做成2个插件的形式,其实代码就几句,也有很详细的注释,就不多说了,直接上代码。
hu_no_comment_links.php:
使评论不包含任何超链接(管理员不受影响),并且在新窗口打开评论者的网站
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | <?php /* Plugin Name: Hu No Comment Links Plugin URI: http://www.hu-yang.com/ Description: 使评论不包含任何超链接(管理员不受影响),并且在新窗口打开评论者的网站。 Version: 0.1 Author: Anthony Hu Author URI: http://www.hu-yang.com/ */ // wp-includes/formatting.php中的make_clickable相关函数会把URL、email、ftp地址自动转为HTML形式的超链接。 // 以下语句去除这种自动转换 remove_filter('comment_text', 'make_clickable', 9); // 在新窗口打开评论者的网站 add_filter('get_comment_author_link', 'hu_popuplinks', 6); function hu_popuplinks($text) { $text = preg_replace('/<a (.+?)>/i', "<a $1 target='_blank'>", $text); return $text; } /* 评论中允许的HTML标记是在wp-includes/kses.php中定义的, 要添加或去除哪些HTML标记只要覆盖kses.php中的$allowedtags变量就可以了。 去除评论对HTML标记<a>的支持。 */ $allowedtags = array( // 'a' => array( // 'href' => array (), // 'title' => array ()), 'abbr' => array( 'title' => array ()), 'acronym' => array( 'title' => array ()), 'b' => array(), 'blockquote' => array( 'cite' => array ()), // 'br' => array(), 'cite' => array (), 'code' => array(), 'del' => array( 'datetime' => array ()), // 'dd' => array(), // 'dl' => array(), // 'dt' => array(), 'em' => array (), 'i' => array (), // 'ins' => array('datetime' => array(), 'cite' => array()), // 'li' => array(), // 'ol' => array(), // 'p' => array(), 'q' => array( 'cite' => array ()), 'strike' => array(), 'strong' => array(), // 'sub' => array(), // 'sup' => array(), // 'u' => array(), // 'ul' => array(), ); ?> |
hu_remove_comment_links.php:
使评论不包含任何超链接(但保留本站引用或回复的超链接),并且在新窗口打开评论者的网站。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | <?php /* Plugin Name: Hu Remove Comment Links Plugin URI: http://www.hu-yang.com/ Description: 使评论不包含任何超链接(但保留本站引用或回复的超链接),并且在新窗口打开评论者的网站。 Version: 0.1 Author: Anthony Hu Author URI: http://www.hu-yang.com/ */ // wp-includes/formatting.php中的make_clickable相关函数会把URL、email、ftp地址自动转为HTML形式的超链接。 // 以下语句去除这种自动转换 remove_filter('comment_text', 'make_clickable', 9); // 在新窗口打开评论者的网站 function hu_popuplinks($text) { $text = preg_replace('/<a (.+?)>/i', "<a $1 target='_blank'>", $text); return $text; } add_filter('get_comment_author_link', 'hu_popuplinks', 6); // 去除非本站引用或回复的超链接 // 本站引用或回复的超链接一般形式:<a href="#comment-33">comment-33</a> function hu_remove_links($text) { $pattern = "/<a [^>]*?href=(?!\"#)[^>]*?>(.*?)<\/a>/i"; $replacement = "$1"; $text = preg_replace($pattern, $replacement, $text); return $text; } add_filter('comment_text', 'hu_remove_links', 66); ?> |
如果讨厌插件,也可以把相关代码放到主题的functions.php文件中。
源代码下载:hu_no_comment_links.zip。
第一个在functions里出错
第二个不能保留本站的链接
咋整啊 ?
我自己用的是第一个,好像没什么问题。
第2个的话,看说明.
22 // 去除非本站引用或回复的超链接
23 // 本站引用或回复的超链接一般形式:<a href=”#comment-33″ rel=”nofollow”>comment-33</a>
就是说只会保留以#开头超链接,并没有对应到网站的网址。
如果你是想防span的话,可以看看这个,评论超链接跳转。
http://fairyfish.net/2009/01/22/comments-link-redirect/
你都没有说这个文件放到哪里,或者代码写到那个文件下面, 不负责任的作者
试一下