WordPress Plugin : Flexible Comment Moderation – A Step by Step Guide

Finally, how to handle submitted comments

This is the last step. Now that the administration area finished, we can move to handling the submitted comments. This is the one that triggered comment_post action hook. As seen above, comment_post action will throw a comment ID parameter.
We will use this parameter to get current comment object using built in function get_comment and get it’s status. Wether it’s approved, awaiting moderation, or marked as SPAM.

<?php
	$comment = get_comment($comment_id);
	$status = $comment->comment_approved;
?>

We will continue the process based on this status. If it’s marked as SPAM by wordpress (be it from akismet or another comment spam plugin or maybe wordpress blacklist) we will leave it intact. If not, we will get the post ID from where current comment is submitted and check wether it override the comment system or not. If it override it then we can continue the process.
Our code will look something like this:

<?php
function cr_flexible_comment_moderation_comment_post( $comment_id ) {
	$comment = get_comment($comment_id);
	$status = $comment->comment_approved;
	if($status !== "spam" ) // approved
	{
		$post_id =  $comment->comment_post_ID;

		$msm = get_post_meta( $post_id, '_cr_flexible_comment_moderation_system_mode', true);
		$mom = get_post_meta( $post_id, '_cr_flexible_comment_moderation_overide_mode', true);
		
		if("overide" == $msm)
		{
			$cstatus = "";
			if("approve" == $mom)
			{
				$cstatus = "approve";
			}
			else if("moderate" == $mom)
			{
				$cstatus = "hold";
			}
			wp_set_comment_status($comment_id, $cstatus);
		}
	}
	return $comment_id;
}
?>

After Word

That’s it, now we have our own flexible comment moderation plugin. I hope you enjoy this tutorial. I’m not a wordpress guru, so if there’s something wrong or could be implemented in smarter way, you can always leave a comment here so I and all reader here can learn it.


Author: Arief Bayu Purwanto

Hello, my name is Arief Bayu Purwanto, a 24 years old father of a beautiful daughter. Interested in online programming, linux, games, and reading. Currently working on kapanlagi.com as junior developer. I live in a relatively quite and cool place called Malang. I'm available for some freelance stuff as well as some consulting job. You can see my portofolio for some previous task I've finished and some other information related to my capability. Btw, I'm plurking here.

  • gaziantep nakliyat

    thank you:D

    • Blognmprodcut

      NM Infotech has been serving worldwide customers ranging from India to USA, since 2008.

  • gaziantep nakliyat

    thank you:D

  • I run several WP-blogs. Most of them are in german. The german blogging communitz is a kind of strange so I try to avoid them.

    I am glad to find sites like this and have in general the feeling that the WP-Community in english is much more helpful than the german one and of course offers a bigger veriety in themes and plugins.

    Therefore I am happy to spend some hours to adjust the themes and plugins (most of them are ok) to german.

    to all out there who are sharing their knowlegde and Ideas.. Thank you a lot

  • I run several WP-blogs. Most of them are in german. The german blogging communitz is a kind of strange so I try to avoid them.

    I am glad to find sites like this and have in general the feeling that the WP-Community in english is much more helpful than the german one and of course offers a bigger veriety in themes and plugins.

    Therefore I am happy to spend some hours to adjust the themes and plugins (most of them are ok) to german.

    to all out there who are sharing their knowlegde and Ideas.. Thank you a lot

  • I search for this.
    Very useful if you write a post about some recent news and you cant approve manual all the comments!

  • I search for this.
    Very useful if you write a post about some recent news and you cant approve manual all the comments!

  • Jake

    Works Perfectly. Thank You so much!

  • Jake

    Works Perfectly. Thank You so much!

  • thanks… i got much info in here, makasih ya mas

  • hmm i like this , thanks dude

  • thanks for posting bro