How To Put House Rule Breaking Commenters In The Naughty Corner

Comments are a double-edged sword. They allow for engagement with and between readers but can also quickly disintegrate into public slanging matches, which does little to enhance a site’s reputation.

Out of the box, WordPress only offers two options for offending comments: leave them up or trash them. Wouldn’t it better to follow sites like the BBC and the Guardian and be able to tag comments as having broken the house rules and just replace the text with a suitable message?

In this article, we’ll walk through the updates needed to add this functionality to a WordPress site and leave commenters in no doubt as to when they’ve broken the rules.

Photo of Lego soccer scene with player receiving a red card from the referee
Leave commenters in no doubt when they’ve broken the rules

Even if you don’t have a site about soccer or climate change or some other topic likely to incite nasty words, if you allow comments on your site then you’ll probably have the need to take action to prevent or halt comment flame wars or to simply remove an offensive comment.

Trashing the comment is one option but this doesn’t tell the offending commenter or other users of the site that they’ve broken the house rules and that’s why the comment has been disabled.

What is required is the ability to:

  1. Tag a comment as broken house rules in the Comments page
  2. Replace the text with a “broke rules” message whenever the comment is displayed
  3. Update the “broke rules” message via the Discussion Settings page

Let’s get building.

Step 1 – Tag a Comment as Broken House Rules

WordPress might make it practically impossible to introduce a new comment status (they are hard-coded in places) but it does provide the ability to add metadata to comments and that’s what we’ll use to tag those rule-breaking comments.

Actually, letting the comments get approved but tagging them makes for an easier implementation.

What we want is to add an option to the menu that appears under each entry on the Comments page:

Screen grab showing the admin options for a single comment
Adding “Broke House Rules” to the comment options

It’s quite a hack to achieve this and a big hat tip is due to Andrew Nacin and his Comment Probation plugin for showing the way.

function comment_row_actions( $actions, $comment ) {
if ( ! isset( $actions['approve'] ) )
return $actions;

$houserules = str_replace( 'action=approvecomment', 'action=approvecomment&houserules=1', $actions['approve'] );
preg_match( '/^(.*?>)/', $houserules, $matches );
$houserules = str_replace( array( ':new=approved', ' vim-a' ), array( ':new=approved&houserules=1', '' ), $matches[1] );
$houserules .= __( 'Broke House Rules', 'comment-houserules' ) . '';

$actions['approve'] .= '<span class="comment-houserules"> | ' . $houserules . '</span>';

return $actions;
}

This function, attached to the comment_row_actions filter, checks that “approve” is an available option and then tacks the “Broke House Rules” action onto the approve action by adding houserules=1 to the querystring.

Incidentally, the “Broke House Rules” option will only appear on comments that are yet to be approved. To tag an approved comment, just unapprove it, then click on Broke House Rules.

Next we need to hook into the wp_set_comment_status action to tag the comment as a rule breaker if necessary.

function wp_set_comment_status( $comment_id, $status ) {

if ( $status != 'approved' )
delete_comment_meta( $comment_id, self::meta_key );

if ( !empty($_GET['houserules']) || !empty($_POST['houserules']) )
update_comment_meta( $comment_id, self::meta_key, '1' );
}

This untags the comment if the comment is not in the process of being approved. It then checks to see if houserules has been set and if it has it tags the comment.

That handles the updates to the Comments page. What about swapping the text?

Step 2 – Adding the “Broke Rules” Message to the Settings > Discussion Page

To enable the easy updating of the message that replaces the text of tagged comments, we’ll add an option to the Settings > Discussion page.

Screen grab showing the new opition to update the broke rules message
Easy updating of the broke rules message via the Settings
// register new House Rules message option on discussion page
function hr_register_setting(){

register_setting(
'discussion',
'hr_msg',
'trim'
);

add_settings_field(
'houserules',
'Broke House Rules message',
array ( &$this , 'hr_show_settings' ),
'discussion',
'default',
array ( 'label_for' => 'hr_msg' )
);
}

// description for new HR option
function hr_description(){

echo '
<p class="description">Enter message to be displayed in place of comment text</p>
';
}

// get the option value
function hr_show_settings( $args ){

$data = esc_attr( get_option( 'hr_msg', '' ) );

printf(
'<textarea id="hr_msg" class="large-text" cols="50" name="hr_msg">'.$data.'</textarea>' ,
$data,
$args['label_for']
);
}

This is standard Settings API code and gets added to the admin_init action. It adds the new house rules message option to the bottom of the default options on the Settings > Discussion page. The option is stored as a global option with the name hr_msg. It is this message that we’ll retrieve and use as replacement text for the tagged comments.

Step 3 – Replacing the Text of Tagged Comments With the “Broke Rules”

This is the easiest step and entails hooking a small function onto the get_comment_text filter that checks to see if the comment is tagged and if it is then returns the hr_msg global option, effectively replacing the original text.

// if rule breaker comment then replace text
function get_comment_text( $text , $comment ){

if (get_comment_meta($comment->comment_ID , self::meta_key , true)) {

return get_option ( 'hr_msg', '' );

} else {

return $text;
}

}

Now when a message is tagged as a rule-breaker it will look like this on the public side

Screen grab of a comment whose text has been replaced by the broke rules message.
Now Chris Knowles is in no doubt he broke the rules

Comments can be a powerful and effective means of building and engaging with an audience but clearly defined boundaries need to be set.

By using the “broke the house rules” approach you can make it plain to commenters when they step over the line and maintain your site as place for robust conversation.

Do you have house rules for comments? Do you think letting commenters know they have broken the rules is the right approach? Tell us in the comments below.

Download the full plugin code.

Photo credit: Daniel Novta