How to schedule comments moderation in WordPress

Today we are going to show how easy it is to implement WordPress comments moderation mechanism which is being run automatically using native scheduling mechanism and WordPress API. Our code is going to iterate through newest comments and replace bad words that might be present in the comment text. If you are looking for a file containing bad words you can visit this site.

In this tutorial we are going to show you how to:
- Iterate through the comments
- Iterate through the text file line by line
- Modify comment text
- Schedule event
- Remove event from the scheduler
- Run this code

Our code is a plugin and looks like this:

comments-moderator.php

This code does the whole job of comments moderation and sets an event to run moderation task every day when a plugin is activated. It deactivates event when plugin is deactivated.

<?php 
/* 
Plugin Name: ITC Comments Moderator Plugin 
Plugin URI: http://www.itcuties.com/ 
Description: Cuts bad words from comments text 
Version: 1 Author: ITCUTIES Author 
URI: http://www.itcuties.com 
*/

function moderateComments () { 	
// Set the number of lates comments to get 	
$args_comments = array('number' =--> '100');

	// Get latest comments comments
	$comments = get_comments($args_comments);

	// Iterate results
	foreach($comments as $c) {
		// Get comments as na array
		$comment_array = get_comment( $c->comment_ID, ARRAY_A );

		// Iterate bad words file
		$lines = file('[BAD-WORDS-FILE-URL-GOES-HERE]', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
		foreach ($lines as $line_num => $line) {
			// Replace bad words in a comment text with some stars
			$comment_array['comment_content'] = str_replace($line, "****", $comment_array['comment_content']);
		}

		// Update comment
		wp_update_comment( $comment_array );

	}
}

// Register schedule event on plugin activation
register_activation_hook(__FILE__, 'moderate_comments_activation');
add_action('my_comment_moderation_event', 'moderateComments');

function moderate_comments_activation() {
	// Run moderation for the first time
	moderateComments();
	// Schedule moderation to run every day
	wp_schedule_event( current_time( 'timestamp' ), 'daily', 'my_comment_moderation_event');
}

// Remove event from schedule on plugin deactivation
register_deactivation_hook(__FILE__, 'moderate_comments_deactivation');

function moderate_comments_deactivation() {
	wp_clear_scheduled_hook('my_comment_moderation_event');
}

?>

Iterate throught the comments

We use get_comments function to get latest comments. While iterating through each comment we need to get its data as an array because wp_update_comment function parameter is an array.

    // Set the number of lates comments to get
    $args_comments = array(
        'number' => '100'
    );

    // Get latest comments comments
    $comments = get_comments($args_comments);

    // Iterate results
    foreach($comments as $c) {
        // Get comments as na array
        $comment_array = get_comment( $c->comment_ID, ARRAY_A );

    ...

Iterate through the text file line by line

Now we need to iterate through the bad words file where a word is placed in one line. Loops $line variable contains our bad word which we want to potentially remove from the comment text.

        // Iterate bad words file
        $lines = file([A_PATH_TO_A_FILE_CONTAINING_BAD_WORDS_HERE], FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
        foreach ($lines as $line_num => $line) {
			...

Modify comment text

Each bad word, if present in the comment text, is being replaced by the PHP str_replace function. To save comments modifications we use WordPress API wp_update_comment function.

            ...
            $comment_array['comment_content'] = str_replace($line, "****", $comment_array['comment_content']);
        }

        // Update comment
        wp_update_comment( $comment_array );
        ...

Schedule event

Scheduling moderation function to run every day is simple. Just register activation hook with register_activation_hook, add an action by calling add_action function and then schedule it with wp_schedule_event function.

    ...
	// Register schedule event on plugin activation
	register_activation_hook(__FILE__, 'moderate_comments_activation');
	add_action('my_comment_moderation_event', 'moderateComments');

	function moderate_comments_activation() {
		...
		// Schedule moderation to run every day
		wp_schedule_event( current_time( 'timestamp' ), 'daily', 'my_comment_moderation_event');
	}
    ...

Remove event from the scheduler

When you want to get rid of this task it’s as easy as calling wp_clear_scheduled_hook function.

	...
	// Remove event from schedule on plugin deactivation
	register_deactivation_hook(__FILE__, 'moderate_comments_deactivation');

	function moderate_comments_deactivation() {
		wp_clear_scheduled_hook('my_comment_moderation_event');
	}

Run this code


Get a bad words file and place it in your wordpress installation. Download our code, unpack it copy to the plugin folder of your wordpress installation. Set a path to this file in the code (line 26). Activate the plugin.

Download this sample codes here.

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>