Sailing the seas of online marketing and plundering the riches as an affiliate pirate.

Wednesday, February 9, 2011

How to Disable HTML Links in Wordpress Comments

One of the most annoying things that a blogger (Wordpress or otherwise) has to deal with is comment spam. Both spam bots and human spammers leave comments with links to various sites in hopes to improve their Google ranking or even get some direct visitors. Take away their ability to post active links in your comments, and you've won most of the battle. Regretfully, for some reason Wordpress both turns inactive text URLs into active hyperlinks, and it allows a href HTML code in the comments by default. To fix this we'll need to implement two modifications in the Wordpress code:

1. Prevent Wordpress from turning text URL addresses into clickable links. This can be achieved easily by modifying the functions.php file of your theme using the editor in Wordpress admin area. All you have to do is add the following filter somewhere in there between the opening and closing php tags:
 remove_filter('comment_text', 'make_clickable', 9);
And if your theme doesn't have a functions.php file, you can simply create one, add the opening php tag, and paste in the filter afterwards.

2. Disable HTML links in Wordpress comments. This part is a tad harder, because you actually have to edit a Wordpress core file. That's right, believe me or not, as of 3.0.5 version of Wordpress, there's still no other way to disable certain tags in comments but to edit the kses.php core file (which gets overwritten with every update, so you have to add your hack again). Hopefully they will add a setting for this in your admin area in the future.

So, find the kses.php file in your wp-includes folder. Locate the part which says * @global array $allowedtags and comment out (e.g. add a double slash before) the tags we want to disallow in the comments (in our case we want to disable active links, so you'll want to comment out lines like 'a' and 'href'). Here's an example of how that part of a modified kses.php file looks:
     * Kses allowed HTML elements.
     * @global array $allowedtags
     * @since 1.0.0
    $allowedtags = array(
        //'a' => array(
        //    'href' => array (),
        //    'title' => array ()),
Of course, you could simply delete the lines and it would have the same effect, but commenting out is preferred in case you will want to turn them back on sometime.

As I mentioned before, this hack will disappear every time you upgrade your Wordpress, so keep a note handy somewhere so you will know what to edit.


Calgary Web Design said...

Thanks for this valuable plugin. Did you have trouble with the current version?

abdussamad said...

^^ LOL your first comment on a post about preventing comment spam is spam!

Andrew said...

Haha, yep. This blog runs on Blogger though, not Wordpress.

Rakesh Daila said...

hello i want to disable the link in comments box.
like when a person post the coomments and anyone click that person's name a link will open

Post a Comment