WordPress Hack: Notify Users of Moderation

October 29, 2004
12:06 am
Posted in: Tech, WordPress

One thing I don’t like about WordPress is that when a comment submitted by a reader goes into moderation for some reason, they have no way of knowing their comment was successfully submitted. They just see that it hasn’t been posted. Even with prominent warnings about the possibility of moderation, they might try to submit the comment again, becoming frustrated.

My solution give the user a JavaScript alert window telling them if their comment was put into moderation.

For WordPress 1.5.1, it can be done with a strict plugin: Notify Users of Moderation 1.0 Just install this, activate it, and you’re done.

For earlier versions, it requires hacking. First, open up wp-comments-post.php and look for this code (near the end). This is for WP 1.2.1 and 1.3… for WP 1.2, see below.

$location = get_permalink($comment_post_ID);

Replace that line with these two lines.

$comment_went_live = ($approved == 0) ? '?moderated=true' : '';
$location = get_permalink($comment_post_ID) . $comment_went_live;

If you are using WP 1.2, look for this line.

$location = (empty($_POST['redirect_to'])) ? $_SERVER['HTTP_REFERER'] : $_POST['redirect_to'];

Replace with this.

$comment_went_live = ($approved == 0) ? '?moderated=true' : '';
$location = (empty($_POST['redirect_to'])) ? $_SERVER['HTTP_REFERER'] : $_POST['redirect_to'];
$location .= $comment_went_live;

What this does is append ?moderated=true to the end of the URI the user is redirected to after submitting a moderated comment. This is for use with clean URIs… if you use the index.php?p=XXX method, you should change ?moderated=true to &moderated=true

Now, all you have to do is add code to index.php to make a pop-up alert when moderated=true is in the URI.

In your index.php file, inside the <head></head> section, add this code.

<?php if ($_GET['moderated'] === 'true') { ?>
<script type="text/javascript"><!--
window.onload= function(){
        alert ("Your comment was successfully processed!\n\nHowever, due to one of the reasons listed below, it was placed on hold and will appear on the site once the administrator verifies that it is not spam.\n\n   - more than <?php
    	echo get_settings('comment_max_links') . ' link';
	echo (get_settings('comment_max_links') > 1) ? 's' : '';
     ?> in the comment\n   - contained a word sometimes associated with spam\n\nPlease be patient and do not resubmit your comment.");
}
//--></script>
<?php } ?>

Modify the text of that alert to your liking. \n means a forced line break.

This will work on both WordPress 1.2 and WordPress 1.3

Yes, it works… please don’t test it out by spamming me. :grin:

Once Again: Please don’t test it out by spamming me! If you want to see what it looks like when a comment gets moderated, put ?moderated=true at the end of the address up in your browser’s address bar.

Update: Added in a little PHP logic into the popup so that it automatically grabs your max links setting.

Update: Added a plugin version for WP 1.5.1

Mark Jaquith

Hi. I’m Mark Jaquith (JAKE-with). I make WordPress, a free and open source publishing platform and I work as a freelance WordPress consultant. This is my personal blog. You can subscribe to my feed or follow me on Twitter and Google+.

60 Responses

Comments temporarily hidden. Will unhide once I get the spam under control.