WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like to be able to filter out a commenters ability to add hyperlinks in their comment text.

I removed the "websites" field from the mix to reduce the amount of spammage already (see: Removing the "Website" Field from Comments and Replies?") which has helped a lot.

By default, they can use the '< a '> tag to do so in the comment box text, which allows spammers to embed hyperlinks to their sites.

Is there a way to filter out that capability in the wysiwyg editor for comment fields?

share|improve this question
nice idea, p.s.: codex.wordpress.org/Function_Reference/wp_filter_comment – edelwater Jan 3 '11 at 17:14
up vote 7 down vote accepted

WP runs so many prettifying filters on this stuff that it's easy to get lost.

Here is what I ended up with:

remove_filter('comment_text', 'make_clickable', 9);
add_filter('pre_comment_content', 'strip_comment_links');

function strip_comment_links($content) {

    global $allowedtags;

    $tags = $allowedtags;
    $content = addslashes(wp_kses(stripslashes($content), $tags));

    return $content;

This scrubs out clearly defined links and removes filter that turns plain text links into properly tagged ones.

share|improve this answer
This seems to have worked just fine. Added it as a plugin and tested it - life is good! Thanks! – cpuguru Jan 4 '11 at 17:54

Another solution -

The function that will remove all <a>...</a> from a text (Probably it's better to use strip_tags instead of regex here):

     function strip_links($content){
       return preg_replace('/<a[^>]*>(.*)<\/a>/iU','', $content);

Remove links from all new comments permanently, before they are insterted in the db:

     add_filter('preprocess_comment', 'new_comment_strip_links');
     function new_comment_strip_links($commentdata){
       $commentdata['comment_content'] = strip_links($commentdata['comment_content']);
       return $commentdata;

Or, remove links before we output them to the screen (Your theme should run the 'comment_text' filter somewhere in a template file):

     add_filter('comment_text', 'display_comment_strip_links');
     function display_comment_strip_links($content){
       return strip_links($content);
share|improve this answer
strip_tags() is overkill, there are quite a few harmless tags that are allowed in comments for formatting. – Rarst Jan 3 '11 at 18:09
No need for this at all, Rarst's approach using the allowedtags array is better suited here and it's exactly why the array of allowed tags is hookable/definable. Nice shot at a solution nonetheless... :) – t31os Jan 4 '11 at 15:34

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.