WordPress Comments Hacks
WordPress Comments Hacks
Automatically refuse spam comments on your WordPress blog
Spam is a nuisance, and you know it. Happilly, WordPress users have Akismet, which help a lot to fight spam. But what about protecting your blog even more? This recipe might help
Paste the following code in your functions.php. Comment that contain any of the words contained within the $bad_comment_content array will be automatically rejected.
function in_comment_post_like($string, $array) {
foreach($array as $ref) { if(strstr($string, $ref)) { return true; } }
return false;
}
function drop_bad_comments() {
if (!empty($_POST[‘comment’])) {
$post_comment_content = $_POST[‘comment’];
$lower_case_comment = strtolower($_POST[‘comment’]);
$bad_comment_content = array(
‘viagra’,
‘hydrocodone’,
‘hair loss’,
‘[url=http’,
‘[link=http’,
‘xanax’,
‘tramadol’,
‘russian girls’,
‘russian brides’,
‘lorazepam’,
‘adderall’,
‘dexadrine’,
‘no prescription’,
‘oxycontin’,
‘without a prescription’,
‘sex pics’,
‘family incest’,
‘online casinos’,
‘online dating’,
‘cialis’,
‘best forex’,
‘amoxicillin’
);
if (in_comment_post_like($lower_case_comment, $bad_comment_content)) {
$comment_box_text = wordwrap(trim($post_comment_content), 80, “n “, true);
$txtdrop = fopen(‘/var/log/httpd/wp_post-logger/nullamatix.com-text-area_dropped.txt’, ‘a’);
fwrite($txtdrop, ” ————–n [COMMENT] = ” . $post_comment_content . “n ————–n”);
fwrite($txtdrop, ” [SOURCE_IP] = ” . $_SERVER[‘REMOTE_ADDR’] . ” @ ” . date(“F j, Y, g:i a”) . “n”);
fwrite($txtdrop, ” [USERAGENT] = ” . $_SERVER[‘HTTP_USER_AGENT’] . “n”);
fwrite($txtdrop, ” [REFERER ] = ” . $_SERVER[‘HTTP_REFERER’] . “n”);
fwrite($txtdrop, ” [FILE_NAME] = ” . $_SERVER[‘SCRIPT_NAME’] . ” – [REQ_URI] = ” . $_SERVER[‘REQUEST_URI’] . “n”);
fwrite($txtdrop, ‘————–******——————‘.”n”);
header(“HTTP/1.1 406 Not Acceptable”);
header(“Status: 406 Not Acceptable”);
header(“Connection: Close”);
wp_die( __(‘bang bang.’) );
}
}
}
add_action(‘init’, ‘drop_bad_comments’);
Source
Number your comments
Open comments.php and find the following line:
< ?php foreach ($comments as $comment) : ?>
Just above this line, initialize a counter variable:
< ?php $i = 0; ?>
Just after this line, increment the counter:
< ?php $i++; ?>
Now, you just have to echo the $i variable to get the number of the current comment. Paste this code anywhere on your comments loop:
< ?php echo $i; ?>
source
Your comments are now numbered!
Batch deleting spam comments on a WordPress blog
delete more than 6000 spam comments that you received on your blog.
First of it all, backup your WordPress database. Then, login to your phpmyadmin, select your WordPress blog database and click on SQL:
Then, insert the following sql command:
DELETE from wp_comments WHERE comment_approved = ‘0’;
source
All non-approved comments will be deleted. But instead of having such trouble next time, you should definitely install Akismet!
Highlight author comments in WordPress
This is useful to make the author comments pop in the comment section, so the readers find the answers to their questions quicker. It can also avoid people pretending they are the author by commenting with his name.
To style the author’s comments differently, you should open the comments.php template file and look for this piece of code (or something looking like it).” id=”comment…
Then you can style it in your stylesheet with something like this.
.authcomment {
background-color: #B3FFCC !important;
}
source
Alternating Colors For Comments
You might want to have alternating row colors for your comments, to make a clearer separation. Alternating rows make it easier to distinguish different comments. Doing this is relatively very easy.
Open your theme css file, and paste this at the bottom:
.color1 {
background-color: #DBDBDB;
}
.color2 {
background-color: #EEEEEE;
}
At the VERY top of comments.php file, put this on a line of it’s own.
< ?$i;?>
Now scroll down to find this part of the code
< ?php foreach ($comments as $comment) : ?>
Now replace the above by
< ?php foreach ($comments as $comment) : ?>