Combat Comment Spam
Most spammers aren't clever enough to populate the REFERER header. This code snippet is not only extremely easy to implement, but pretty effective, too. Open up your themes functions.php and drop in the following:
function wp_check_referrer() {
if (empty($_SERVER['HTTP_REFERER']) || (!isset($_SERVER['HTTP_REFERER'])) {
wp_die( __('Undefined HTTP_REFERER.') ); }
}
add_action('check_comment_flood', 'wp_check_referrer');
Now, unless the REFERER field is set, they won't even make it to Akismet! Probably wouldn't hurt to warn your visitors since a select few might intentionally prevent sending a REFERER string. This isn't unusual for anyone coming from a proxy or one of the many FireFox extensions that protect "privacy."
Post Word Count
As the heading says, this will count the number of words in a post. Open up functions.php again and insert the following:
function getwc() {
ob_start();
the_content();
$content = ob_get_clean();
return sizeof(explode(" ", $content));
}
To display the word count for all to see, insert the following inside the WordPress loop (if have_posts() ... ). single.php probably works best..
<p>Word Count: <?php echo getwc(); ?></p>
Log all POSTs to a Text File
This WordPress hack is a bit longer, but can provide some interesting information. By adding this block of code to your functions.php, all $_POST submissions will end up in a text file of your choice. Setup is simple: replace 172.16.20.9 and 192.168.80.20 with IPs to exclude (usually your own), and set the path to a log file. Outside of the public_html directory is probably a good idea.
function server_post_logd() {
$client_addr = $_SERVER['REMOTE_ADDR'];
$request_file = $_SERVER['SCRIPT_NAME'];
if ($client_addr != '172.16.20.9' && $client_addr != '192.168.80.20') {
if(!empty($_POST)) {
$fp = fopen('/users/nullamatix.com/logs/post_logd-file.txt', 'a');
foreach($_POST as $key => $value) {
fwrite($fp, $key.' = '.$value."\n");
}
fwrite ($fp, $client_addr . "\n");
fwrite ($fp, $request_file . "\n");
fwrite ($fp, date("F j, Y, g:i a") . "\n");
fwrite ($fp, '--------------**********------------------'."\n\n" );
fclose ($fp);
}
}
}
add_action('init', 'server_post_logd');
Just make sure /users/nullamatix.com/logs/post_logd-file.txt exists and is writable by the server. Be aware, if you have enemies, this can fill up disk space and potentially cause a DoS attack.
All Posts, 1 Page
Create a WordPress page template file called "All Posts" or whatever. Here's the code - pretty simple stuff.
<?php
$total_posts = $wpdb->get_var("SELECT COUNT(*) FROM
$wpdb->posts WHERE post_status = 'publish'");
if (0 < $total_posts) $total_posts = number_format($total_posts); ?>
<h2><?php echo $total_posts; ?> Posts Since July 10th, 1966</h2>
<ul>
<?php
$all_posts = get_posts('post_type=any&exclude=80,55&numberposts=-1&');
foreach($all_posts as $post) : ?>
<li><?php the_time('m/d/y') ?>: <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
</ul>
Live Demo: All Nullamatix.com Posts
Alright, that's all I'm able to come up with at the moment. What are some of your favorite WordPress Hacks?
Word Count: 624


