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.




