Disable Hyperlinks in WordPress Comments
Restrict commenter from posting links in WordPress comment by using a few lines of code – PHP code to disable hyperlinks in WordPress comments.
Have you came across that annoying moment, when people start posting unwanted and irrelevant links in comments on your WordPress blog that also survive Akismet spam filtering? WordPress users who experience a lot of comment spam can control it through Akismet. But sometimes, Akismet too, skips detection of such unwanted link postings in comments.
As a blogger and WordPress user, I experienced it several times. If you want to get rid of such unwanted spam postings, disabling hyperlinks in comments would be a permanent solution to that. In order to do that, you just need to fork the functions.php
file of your WordPress theme a little bit.
Add the below code in your WordPress theme’s functions.php
just before the closing PHP tag, i.e. ?>
and save the changes:
add_filter('pre_comment_content', 'strip_comment_links'); function strip_comment_links($content) { global $allowedtags; $tags = $allowedtags; unset($tags['a']); $content = addslashes(wp_kses(stripslashes($content), $tags)); return $content; }
That’s it! From now, commenters won’t be able to post links in comments and the previously posted links in comments will also be deactivated after saving the changes.