5 Quick WordPress Hacks for Better Blog Posts
A well-made plugin is a beautiful thing. But sometimes when you know what you want, it’s better to just fix the code rather than add yet another plugin to an already long list of them. Here are some plugin-free hacks to spruce up the appearance of your blog posts.
Display a snapshot of an external website
If you showcase websites on your blog or simply want to add the ability to put a snapshot of one anywhere you please, here’s an easy hack that started as a plugin from BinaryMoon.
First, add the following code to your functions.php file:
<?php
functionbm_sc_mshot ($attributes, $content = '', $code = '') {
extract(shortcode_atts(array(
'url' => '',
'width' => 250,
), $attributes));
$imageUrl = bm_mshot ($url, $width);
if ($imageUrl == '') {
return '';
} else {
$image = '<imgsrc="' . $imageUrl . '" alt="' . $url . '" width="' . $width . '"/>';
return '<div class="browsershotmshot"><a href="' . $url . '">' . $image . '</a></div>';
}
}
functionbm_mshot ($url = '', $width = 250) {
if ($url != '') {
return 'http://s.wordpress.com/mshots/v1/' . urlencode(clean_url($url)) . '?w=' . $width;
} else {
return '';
}
}
add_shortcode('browsershot', 'bm_sc_mshot');
?>
Don’t forget to save.
Now, wherever you’d like to add an external website’s screenshot, simply add the [browsershot] shortcode while you’re composing WordPress’s text editor, inserting the URL and width:
[browsershoturl="http://link-to-website" width="foo-value"]
Automatically notify authors when their posts have been published
If you have a blog with multiple authors, it can be a nice touch to send them a quick email letting them know when their posts have gone live on your site. This hack, courtesy of WP recipes, is about as easy as they come.
Simply post the following code into the functions.php file:
function wpr_authorNotification($post_id) {
$post = get_post($post_id);
$author = get_userdata($post->post_author);
$message = "
Hi ".$author->display_name.",
Your post, ".$post->post_title." has just been published. Well done!
";
wp_mail($author->user_email, "Your article is online", $message);
}
add_action('publish_post', 'wpr_authorNotification');
Save it, and you’re done.
Display the Google search terms a visitor used to find your site
You can use this hack from WP-Snippets for the benefit of the user or simply for your own chronicling and analytics purposes. Just add the following code to any the files in your WP theme where you’d like the user’s search terms to appear:
<?php
$refer = $_SERVER["HTTP_REFERER"];
if (strpos($refer, "google")) {
$refer_string = parse_url($refer, PHP_URL_QUERY);
parse_str($refer_string, $vars);
$search_terms = $vars['q'];
echo 'Welcome Google visitor! You searched for the following terms to get here: ';
echo $search_terms;
};
?>
Save it, and you’re done.
Highlight the first post on a page
If you’d like the very first post on a main page, search page, or archive page to stand out from the rest, all you need to do is add a snippet of code to that page’s PHP file, then visit to your CSS file to specify how you’d like it to look. In the PHP file, find the following loop:
<?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> <div class="post"> ... </div> <?php endwhile; ?> <?php endif; ?>
Add the following snippets to the 3 appropriate lines of the loop so it looks like this instead:
<?php $i = 0; ?> <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> <?php $i++ ?> <div class="post<?php if ($i == 1) echo ' first'; ?>"> ... </div> <?php endwhile; ?> <?php endif; ?>
Now, you can visit your CSS file and define the style settings for the new class you’ve created, “first.” Once you’ve done that and saved it, you’re all set.
Replace “. . .” with “Read More” in a post excerpt
If you use post excerpts and rely on WordPress’s default the_excerpt() function to serve them up, you will see the ellipsis “. . .” when an excerpt is empty or too long. To remove the ellipsis and replace it with “Read More,” add the following code to your functions.php file. Thanks to WordPress Technology for this one.
function trim_excerpt($text) {
global $post;
$moreLink = ' ... <a href="' .get_permalink($post->ID) .'">Read More »</a>';
$text = str_replace('[...]', $moreLink, $text);
return $text;
}
add_filter('get_the_excerpt', 'trim_excerpt');
Note that you can put any text in place of “Read More.”
Save it, and you’re done.

