Preprocess Comment Content in WordPress

By  on  

I've put a great amount of effort into making sure the comment system on this blog is fast and feature-filled.  The comment system is AJAX-based so you don't need to worry about page refreshes.  You can also post links to GitHub gists, CodePen pens, and JSFiddle fiddles and see them rendered within the comment.  Those tasks I accomplish after a comment has been registered in the system.  But what if you want to modify comment content before it is processed, and subsequently marked as SPAM or scrubbed?  That's super easy with WordPress hooks!

The PHP

The preprocess_comment hook allows us to get at the comment data before it is processed.  Here is how I use this hook, wrapping `text` strings in <code> elements and encoding angle characters in <pre> elements:

// Manage comment submissions
function preprocess_new_comment($commentdata) {
	// Replace `code` with <code>code</code>
	$commentdata['comment_content'] = preg_replace("/`(.*)`/Um", "<code>$1</code>", $commentdata['comment_content']);

	// Ensure that code inside pre's is allowed
	preg_match_all("/<pre(.*?)>(.*)<\/pre>/", $commentdata['comment_content'], $pre_matches); // $2
	foreach($pre_matches as $match) {
		$immediate_match = str_replace(array('<', '>'), array('<', '>'), $match[2]);
		$commentdata['comment_content'] = str_replace($match[2], $immediate_match, $commentdata['comment_content']);
	}

	// Return
	return $commentdata;
}
add_action('preprocess_comment', 'preprocess_new_comment');

This snippet should be added to functions.php, as you would expect of a WordPress theme enhancement.

I love the WordPress hook system -- it makes the CMS incredibly powerful and customizable.  I also use this hook to prevent WordPress comment SPAM.  And since many users place HTML code in my comments, it's important I encode those angle characters properly.  In the end, you never know what your user will submit and what each site will accept -- use this WordPress hook to take control!

Recent Features

  • By
    CSS @supports

    Feature detection via JavaScript is a client side best practice and for all the right reasons, but unfortunately that same functionality hasn't been available within CSS.  What we end up doing is repeating the same properties multiple times with each browser prefix.  Yuck.  Another thing we...

  • By
    Write Simple, Elegant and Maintainable Media Queries with Sass

    I spent a few months experimenting with different approaches for writing simple, elegant and maintainable media queries with Sass. Each solution had something that I really liked, but I couldn't find one that covered everything I needed to do, so I ventured into creating my...

Incredible Demos

  • By
    Image Reflection with jQuery and MooTools

    One subtle detail that can make a big difference on any web design is the use of image reflections. Using them too often can become obnoxious but using reflections on large, "masthead" images is a classy enhancement. Unfortunately creating image reflections within your...

  • By
    MooTools 1.2 Image Protector: dwProtector

    Image protection is a hot topic on the net these days, and why shouldn't it be? If you spent two hours designing an awesome graphic, would you want it ripped of in matter of seconds? Hell no! That's why I've created an image...

Discussion

  1. Hello David,
    Thanks for the hook implementation for preprocess_comment.

    I have question on using the hook and replacing angle brackets inside in the short code. On my site http://zevolving.com/, I allow visitors to use the short code to post the ABAP code. In ABAP, we have few variables (known as Field symbols) which can be declared using the angle bracket, like <fs_wa>

    When readers submit the comment, these variables gets disappeared from the comments. I think WP considers them HTML tags and removes them.

    I think, I can use the preprocess_comment hook, but not sure how to implement the logic of replacing the angle brackets with symbols. Can you please help me?

    Thanks Much.

    Regards,
    Naimesh Patel

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!