Hack: only registered users can post comments
-
My friend and I threw together a little hack in the wp-comments.php file that allows you to control the public commenting on your blog, in that only registered and logged in users are able to post to your blog. It’s by no means bullet-proof like I’d want, but it works.
It uses the function get_currentuserinfo(); to test the user’s session and to see if the variables user_email and user_nickname are assigned values. I’d probably try to use more sophisticated session checking, but this works out of the box and with a few small hacks.
Open up the wp-comments.php file and add these few lines of code after the opening <?php tag
######### comments hack by Jamison & Michael
get_currentuserinfo();
$comment_author = $user_nickname;
$comment_author_email = $user_email;
$comment_author_url = $user_url;
if ((empty($user_nickname))&&(empty($user_email))) {
$message = "<font color=red>You must be a registered user and LOGGED IN to post comments.</font>
";
$message .= 'Please login or register
';
$post_disabled = " disabled";
}
Now scroll down further in the file where the form tags are for the commenting. Add these few lines of code right before the opening <form> tag.
<?
######### comments hack by Jamison & Michael
echo $message; // display message if they're not logged in
?>
Now this next part could be tricky if you don’t know HTML very well… so if you don’t, do your best to follow along…
You’ll be adding the following tag in the author, email, url, comment, and submit fields:
<?=$post_disabled?>
Those tags will look something like this:
<input type="text" name="author" id="author" class="textarea" value="<?php echo $comment_author; ?>" size="28" tabindex="1" <?=$post_disabled?> />
<input type="text" name="email" id="email" value="<?php echo $comment_author_email; ?>" size="28" tabindex="2" <?=$post_disabled?> />
<input type="text" name="url" id="url" value="<?php echo $comment_author_url; ?>" size="28" tabindex="3" <?=$post_disabled?>/>
<textarea name="comment" id="comment" cols="70" rows="4" tabindex="4"<?=$post_disabled?>></textarea>
<input name="submit" type="submit" tabindex="5" value="<?php _e("Say It!"); ?>" <?=$post_disabled?> />
What those $post_disabled tags do is fill in a value “disabled” in the form fields if the user is not logged in and cause the form to become unusuable/disabled to the end user.
Like I said, it’s not the most secure way of keeping out undesireable posts, but this combined with WP-Blacklist 1.2.1, you can relatively control who is able to post comments on your blog.
– Michael
– https://www.designbymichael.com
- The topic ‘Hack: only registered users can post comments’ is closed to new replies.