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

Creating new admin custom box

Next, we’ll add code to handle the creation of admin custom box. To be able to do this, we’ll use wordpress built-in function called add_meta_box. This function take the following form:

<?php
add_meta_box ($id, $title, $callback, $page, $context, $priority);
?>

Here’s the code that implement it:

<?php
	add_meta_box ( 'cr_flexible_comment_moderation_sectionid', 'Advanced Moderation',
		'cr_flexible_comment_moderation_inner_custom_box', 'post', 'side' );
	add_meta_box ( 'cr_flexible_comment_moderation_sectionid', 'Advanced Moderation',
		'cr_flexible_comment_moderation_inner_custom_box', 'page', 'side' );

	function cr_flexible_comment_moderation_inner_custom_box () {
		echo "hi, I'm box content!";
	}
?>


Notice the difference in $page parameter. The add_meta_box’s $page parameter accept 3 value:

  1. ‘post’: will shown on add/edit post
  2. ‘page’: will shown on add/edit page
  3. ‘link’: will shown on add/edit link

While $context parameter accept the following value:

  1. ‘normal’ and ‘advanced’: will laid our box in the middle page area
  2. ’side’: will laid our box in the sidebar

Here’s how it’s laid out with $context = ’side’:

while this one with $context = ‘normal’ or ‘advanced’:

Filling box with contents

Now that our shiny box is ready, we will move to filling it with useful options. This custom box accept standard HTML as it’s content.

<?php
function cr_flexible_comment_moderation_inner_custom_box () {

  echo '<input type="hidden" name="cr_flexible_comment_moderation_noncename" id="cr_flexible_comment_moderation_noncename" value="' .
    wp_create_nonce( plugin_basename(__FILE__) ) . '" />';

  echo '<label for="cr_flexible_comment_moderation_system_mode">Moderation Mode:</label><br />';
  echo '<input type="radio"'
  		.' name="cr_flexible_comment_moderation_system_mode"'
		.' value="default" /> use system default<br />';
  echo '<input type="radio"'
  		.' name="cr_flexible_comment_moderation_system_mode"'
		.' value="overide" /> override system default<br />';
  echo '<p><u><strong>Moderation mode</strong></u> will determine if we will override discussion setting.
  	If you choose default, it will use system setting (<em>discussion setting</em> ). Otherwise, it will use setting below (<em>if the comment is not marked as spam</em> )</p><br />';
	  echo '<label for="cr_flexible_comment_moderation_overide_mode">Override Mode:</label><br />';
	  echo '<input type="radio"'
	  		.' name="cr_flexible_comment_moderation_overide_mode"'
			.' value="approve" /> always approved<br />';
	  echo '<input type="radio"'
	  		.' name="cr_flexible_comment_moderation_overide_mode"'
			.' value="moderate" /> always moderated<br />';
  echo '<p><u><strong>Override mode</strong></u> will help system to determine if new comment to this page/post will be marked as approved/hold moderation.</p>';
}
?>

One think to note here is the use of wp_create_nonce. This little function will create a one time, randomly generated token that will be used as form post verification. Because form submit can be initiated from practically anywhere. We’ll see it’s uses in next step.


The box will look something like this:

Handle Page Save

Next step, we’ll create code to handle save_post hook.
First, we will verify our randomly generated nonce with wp_verify_nonce:

<?php
	if ( !wp_verify_nonce( $_POST['cr_flexible_comment_moderation_noncename'], plugin_basename(__FILE__) )) {
		return $post_id;
	}
?>

This is to check that current POST really came from our own form. Next, we’ll verify if current user is allowed to edit this post with current_user_can:

<?php
	if ( 'page' == $_POST['post_type'] ) {
		if ( !current_user_can( 'edit_page', $post_id ))
			return $post_id;
	} else {
		if ( !current_user_can( 'edit_post', $post_id ))
			return $post_id;
	}
?>

After all this check, we can move forward to save the options with add_post_meta and update_post_meta.

<?php
	$msm = $_POST['cr_flexible_comment_moderation_system_mode'];
	if(!add_post_meta($post_id, "_cr_flexible_comment_moderation_system_mode", $msm, true))
		update_post_meta($post_id, "_cr_flexible_comment_moderation_system_mode", $msm);

	$mom = $_POST['cr_flexible_comment_moderation_overide_mode'];
	if(!add_post_meta($post_id, "_cr_flexible_comment_moderation_overide_mode", $mom, true))
		update_post_meta($post_id, "_cr_flexible_comment_moderation_overide_mode", $mom);
?>

Another thing to remember here is the use of the 4th parameter in add_post_meta. If we set this parameter (called $unique) to false, everytime we call add_post_meta with the same $post_id and $meta_key (the 2nd parameter), this little function will never return false. Instead, it will add new post meta entry, resulting an overwhelmed post meta entry on database.
Please note also, that I use a trailing ‘_’ (underscore) character as post meta key. This technique called as hidden post meta. When you do this, your post meta will never shown in add new post’s Custom Fields box.

After we put it all together, it will loke something like this:

<?php
function cr_flexible_comment_moderation_save_postdata( $post_id ) {
	// verify this came from the our screen and with proper authorization,
	// because save_post can be triggered at other times

	if ( !wp_verify_nonce( $_POST['cr_flexible_comment_moderation_noncename'], plugin_basename(__FILE__) )) {
		return $post_id;
	}

	if ( 'page' == $_POST['post_type'] ) {
		if ( !current_user_can( 'edit_page', $post_id ))
			return $post_id;
	} else {
		if ( !current_user_can( 'edit_post', $post_id ))
			return $post_id;
	}

	// OK, we're authenticated: we need to find and save the data

	$msm = $_POST['cr_flexible_comment_moderation_system_mode'];
	if(!add_post_meta($post_id, "_cr_flexible_comment_moderation_system_mode", $msm, true))
		update_post_meta($post_id, "_cr_flexible_comment_moderation_system_mode", $msm);

	$mom = $_POST['cr_flexible_comment_moderation_overide_mode'];
	if(!add_post_meta($post_id, "_cr_flexible_comment_moderation_overide_mode", $mom, true))
		update_post_meta($post_id, "_cr_flexible_comment_moderation_overide_mode", $mom);

	return $post_id;
}
?>

Get the options for use in custom box

Now that we can save the options, we’ll need a way to show to author when he edit his post entry so that he know current moderation mode.
We will modify our cr_flexible_comment_moderation_inner_custom_box function. We will use built in function get_post_meta. First, we have to retrieve current post’s post ID. After that, we can use this $post_id variable to use with get_post_meta. Next, with the post meta’s value retrieved, we can use it to automatically check currently selected mode.
Here’s the full cr_flexible_comment_moderation_inner_custom_box function body.

<?php
function cr_flexible_comment_moderation_inner_custom_box () {
  $post_id = mysql_escape_string($_GET['post']);

  // The actual fields for data entry
  $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($msm == "" ) { $msm = 'default'; } //so we get default value when creating new entry for post/page
  if($mom == "" ) { $mom = 'moderate'; } //so we get default value when creating new entry for post/page

  echo '<input type="hidden" name="cr_flexible_comment_moderation_noncename id="cr_flexible_comment_moderation_noncename" value="' .
	wp_create_nonce( plugin_basename(__FILE__) ) . '" />';

  echo '<label for="cr_flexible_comment_moderation_system_mode">Moderation Mode:</label><br />';
  echo '<input type="radio"'
  		.' name="cr_flexible_comment_moderation_system_mode"'
  		. ($msm == 'default' ? ' checked="checked"' : '')
		.' value="default" /> use system default<br />';
  echo '<input type="radio"'
  		.' name="cr_flexible_comment_moderation_system_mode"'
  		. ($msm == 'overide' ? ' checked="checked"' : '')
		.' value="overide" /> override system default<br />';
  echo '<p><u><strong>Moderation mode</strong></u> will determine if we will override discussion setting.
  	If you choose default, it will use system setting (<em>discussion setting</em> ).
  	Otherwise, it will use setting below (<em>if the comment is not marked as spam</em> )</p><br />';
	  echo '<label for="cr_flexible_comment_moderation_overide_mode">Override Mode:</label><br />';
	  echo '<input type="radio"'
	  		.' name="cr_flexible_comment_moderation_overide_mode"'
	  		. ($mom == 'approve' ? ' checked="checked"' : '')
			.' value="approve" /> always approved<br />';
	  echo '<input type="radio"'
	  		.' name="cr_flexible_comment_moderation_overide_mode"'
	  		. ($mom == 'moderate' ? ' checked="checked"' : '')
			.' value="moderate" /> always moderated<br />';
  echo '<p><u><strong>Override mode</strong></u> will help system
  	to determine if new comment to this page/post will be marked as approved/hold moderation.</p>';
}
?>

Our custom box will look something like this:

Pages: 1 2 3

  • Jake
    Works Perfectly. Thank You so much!
  • I search for this.
    Very useful if you write a post about some recent news and you cant approve manual all the comments!
  • 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
  • gaziantep nakliyat
    thank you:D
blog comments powered by Disqus