WordPress is undoubtedly one of the most popular tools on the Internet because it makes it so easy to get a functional web site up and running in such a short time.
And then there are the plugins. If you want your WordPress site to do something then odds are there is a plugin that you can install to make your site do what you want it to do.
But plugins pose a bit of a problem at times. Since they are created by third-party developers you never know what quality control practices are used and what security testing is done to make sure that the plugin doesn’t leave your site vulnerable.
So instead of blindly installing a plugin, you can easily apply these hacks to your WordPress code to enhance the functionality of your site without giving up control to a plugin developer who you don’t know.
Remove the trackbacks from your comment count
Like any exercise, let’s get warmed up first.
Trackbacks are one of the communication tools that make blogging much more community driven than a static web site. However because they are listed under the comments, people don’t always approve them. This quick little hack will remove the trackbacks your blog receives from the total number of comments.
This can be done by adding the following code to the functions.php file:
add_filter('get_comments_number', 'comment_count', 0);
function comment_count( $count ) {
if ( ! is_admin() ) {
global $id;
$comments_by_type = &separate_comments(get_comments('status=approve&post_id=' . $id));
return count($comments_by_type['comment']);
} else {
return $count;
}
}
Keep readers on your site with related posts
Most blog owners know that best way to keep visitors is to provide them with more useful content. There are many plugins out there that can be used to provide readers with related posts, but it is also something that can be done with a little bit of custom code that needs to be placed within the loop:
<?php
$tags = wp_get_post_tags($post->ID);
if ($tags) {
echo 'Related Posts';
$first_tag = $tags[0]->term_id;
$args=array(
'tag__in' => array($first_tag),
'post__not_in' => array($post->ID),
'showposts'=>5,
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><a href="<?php the_permalink() ?>" rel="bookmark" title="
Permanent Link to <?php the_title_attribute(); ?>">
<?php the_title(); ?></a></p>
<?php
endwhile;
}
}
?>
What this code will do is take the first tag from your post and identify related posts based on that tag and display links to them.
Create your own comment spam filter.
Anyone running a blog should be using the Aksimet plugin to help filter out comment spam. However you can give Akismet a bit of help using this bit of code that will automatically filter out comments that contain words you define in the $bad_comment_content array.
Add the following lines of code to your functions.php file:
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',
'hair loss',
'[url=http',
'[link=http',
‘other words or phrases you don’t like’
);
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');
Update your list to include the words, phrases and URLs that you find most offensive.
Make the author’s comments stand out.
The comment section is a great place for readers to interact with the author. To let other readers know that a specific comment is a response from the author you can hack up the code of the comments.php template file to change the background color for any comment written by the author of the post.
In your comments.php file, locate the following line of code (it may be exact, if not find the line that is similar):
<li class="<?php echo $oddcomment; ?>" id="comment...
and replace it with:
<li class="<?php
/* Only use the authcomment class from style.css if the user_id is 1 (admin) */
if (1 == $comment->user_id)
$oddcomment = "authcomment";
echo $oddcomment;
?>" id="comment...
Of course you will want to change the background-color to one of your choosing. You may also need to change the color of the font as well.
Edit user contact info.
When users register for your blog they have the option to include their AIM, Yahoo and Jabber accounts in their profile. But really, who uses those anymore? Adding this bit of code to the functions.php file will remove those and allow you to add more relevant contact details:
function extra_contact_info($contactmethods) {
unset($contactmethods['aim']);
unset($contactmethods['yim']);
unset($contactmethods['jabber']);
$contactmethods['facebook'] = 'Facebook';
$contactmethods['twitter'] = 'Twitter';
$contactmethods['linkedin'] = 'LinkedIn';
$contactmethods[‘googleplus’] = ‘Google+’;
return $contactmethods;
}
add_filter('user_contactmethods', 'extra_contact_info');