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

written by Arief Bayu Purwanto on February 27, 2009 in Tutorial and [CR] Flexible Comment Moderation with 12 comments

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.