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

This tutorial will explain how to create a plugin that will add new functionality to wordpress add page/post form. The plugin will add another level of comment moderation by allowing author to specifically always moderate/approve comment on certain page/post.

First of all, I’m sorry that I have to break this post into several pages. Because it’s long guys!

Introduction

One day, I have a need of some new functionality on my blog. What I need is the ability to always moderate comment on some entry while on others, I let my wordpress handle it (if it allow the comment to be approved automatically, so be it). I googled it with no result. So, I create one. But, I have to google some information or look at wordpress codex then write code then google again, write code, google, write code, and so on. You should get the idea. That’s why I create this tutorial to help me remember the step to create such plugin.

Getting Started

A journey of a thousand miles begins with a single step.
   — Lao-tzu

All plugin file should goes to wp-content/plugins/ directory. For this tutorial, we’ll name our file as cr-flexible-comment-moderation.php and save it in wordpress plugin folder (wp-content/plugins/). Next, we’ll have to fill the plugin information header to this file, so that wordpress could recognize it as plugin. Standard header will look something like this:

<?php 
	/*
	Plugin Name: CR Flexible Comment Moderation
	Plugin URI: http://bayu.freelancer.web.id/
	Description: Plugin for extend wordpress comment moderation mode
	Author: Arief Bayu Purwanto
	Version: 1.0
	Author URI: http://bayu.freelancer.web.id/
	*/
?>


Because we want to add another option to add new/edit post’s page, we’ll have to use these action hooks:

  • admin_menu:Runs after the basic admin panel menu structure is in place.
  • save_post:Runs whenever a post or page is created or updated, which could be from an import, post/page edit form, xmlrpc, or post by email. Action function arguments: post ID.
  • comment_post:Runs just after a comment is saved in the database. Action function arguments: comment ID, approval status (“spam”, or 0/1 for disapproved/approved).

These are hooks we need. With admin_menu, we will be able to add new functionality in wordpress administration area. While save_post will allow us to attach code to wordpress’s save post event and comment_post will attach our function to wordpress comment post event.

<?php 
	add_action('admin_menu', 'cr_flexible_comment_moderation_add_custom_box');
	add_action('save_post', 'cr_flexible_comment_moderation_save_postdata');
	add_action('comment_post', 'cr_flexible_comment_moderation_comment_post');

	function cr_flexible_comment_moderation_add_custom_box () {}
	function cr_flexible_comment_moderation_save_postdata( $post_id ) {}
	function cr_flexible_comment_moderation_comment_post( $comment_id ) {}
?>

Now that we have this all set and saved, we can go to wordpress admin and enable our plugin.

Right now, we should see no difference in our add new post page.

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