How to Remove Links in WordPress Comments
There are more and more spammers in my blog, they post various comments with some links in it, few of them are useful for my article. So I want to remove these links in comments. I have no other choose as long as I am using wordpress.
Note:
- Removing links in comments will affect all the comments in your wordpress, including all the regular(good) comments.
- All the removing actions only affect the display, not the data in database, you cancelled the removing anytime.
1. Remove links in wordpress comments
Add these codes in your current theme's function.php file. Just before some other add_action or add_filter syntaxs.
function remove_links_in_comments($text = ''){
$text = preg_replace("/<a[^>]*href=[^>]*>|<\/[^a]*a[^>]*>/i","",$text);
echo $text;
}
add_filter('comment_text', 'remove_links_in_comments', 10);
2. Remove the comment author links in wordpress comments
Note: this action will remove all the author links in comments, if you want to display the author links again, just delete these codes and refresh your page.
function remove_comment_author_url($text = ''){
$text = preg_replace("/<a[^>]*href=[^>]*>|<\/[^a]*a[^>]*>/i","",$text);
echo $text;
}
add_filter('get_comment_author_link', 'remove_comment_author_url', 20);