15 Killer Hacks for WordPress that Are Extremely Useful

1. Link to External Sources from Your Post Title

Often it is the case where blogger wants to just link to an external resource because he/she finds it useful to their readers. The main problem they face is that they have to make a new post in order to just tell the readers to go to another site. In this Custom Field Hack we will show you how you can link to an external link from your post title.

First thing you need to do is open your functions.php which is found in your template folder where other files like single.php and page.php is located. Paste the following code:

function print_post_title() {
global $post;
$thePostID = $post->ID;
$post_id = get_post($thePostID);
$title = $post_id->post_title;
$perm = get_permalink($post_id);
$post_keys = array(); $post_val = array();
$post_keys = get_post_custom_keys($thePostID);

if (!empty($post_keys)) {
foreach ($post_keys as $pkey) {
if ($pkey==’url1′ || $pkey==’title_url’ || $pkey==’url_title’) {
$post_val = get_post_custom_values($pkey);
if (empty($post_val)) {
$link = $perm;
} else {
$link = $post_val[0];
} else {
$link = $perm;
echo ‘<h2><a href=”’.$link.’” rel=”bookmark” title=”’.$title.’”>’.$title.’</a></h2>’;

Now you would need to open your index.php and find the following code or something similar:

<h2><a href=”<?php the_permalink() ?>” rel=”bookmark”><?php the_title(); ?></a></h2>

Change it to:

<?php print_post_title(); ?>

Once you have done that, upload both files to your webhost.

Now when you are writing a post, scroll down to where it says Custom Fields. Find the name: url1, title_url, or url_title and add the url to the external resource. Add a short description if you so desire, and hit publish.

Don’t be afraid, this function does not take away your normal post title links, all it does is add an extra query which checks for custom field for external links. If the external link is not included, it refers to the default code and link to the normal post page

There is also a plugin that does this job. It is called Page Links To

2. Change the Default Gravatar for Comments

Good Bye Mystery Man

The default mystery man is really annoying for most users. Plus if you have one more chance of branding your blog, then why not do it. Changing your default gravatar lets you brand your blog more. With this snippet below you can change your default gravatar.

First you need to open your functions.php which is located in your template folder. If you don’t have one then create one and insert the following code:

add_filter( ‘avatar_defaults’, ‘newgravatar’ );

function newgravatar ($avatar_defaults) {
$myavatar = get_bloginfo(‘template_directory’) . ‘/images/gravataricon.gif’;
$avatar_defaults[$myavatar] = “WPBeginner”;
return $avatar_defaults;

In the code the image is being extracted from the theme directory and it is called gravataricon.gif obviously you will change it to your image name. Where it says WPBeginner, that is the name of the avatar of how it will show in your admin panel options area.

Gravatar Settings

Head over to your admin panel and click Settings > Discussion and change the icon, and now you have a branded comment area with your logo.

3. Display a Retweet Button with your Brand

Add a Retweet button in WordPress

With Twitter getting so much exposure, as a blogger you should already be using it to your advantage. Power of twitter is like no other because it is word of mouth advertising. To make this easier on your readers, what you can do is place a prominent retweet button, so they can retweet the article with one click. Not only just that, but you should make it the way so you can track the retweets as well. That is where tweetmeme widget comes in.

In this tutorial we will have you create a button that will link to the text in the following format:

RT @yoursitename Title of the Post – Link

Add the following code in the template file of your choosing most likely single.php

For the Large Button:

<script type=”text/javascript”>
tweetmeme_source = ‘wpbeginner’;
<script type=”text/javascript” src=”http://tweetmeme.com/i/scripts/button.js”> </script>

For the Compact Button:

<script type=’text/javascript’>
tweetmeme_style = “compact”;
tweetmeme_source = ‘wpbeginner’;

Remember to change the source to your twitter account name, this way you will not only promote your account to get more followers, but your article will be promoted as well.

4. Display Random Header Images on Your Blog

Random Header Images

Most blog designs get boring if they have a huge header picture and it is static. This is when this tutorial comes in to make your header images dynamic because it rotates on each visit. You can select as many images as you want to rotate randomly. It brings life to a blog.

First you need to name your images in this format:

  • headerimage_1.gif
  • headerimage_2.gif
  • headerimage_3.gif

You must separate the name with an underscore. You can change the headerimage text to himage or anything you like.

Once you have done that paste the following code in your header.php where you would like the images to be displayed or in any other file.

<img src=”http://path_to_images/headerimage_<?php echo(rand(1,3)); ?>.gif”
width=”image_width” height=”image_height” alt=”image_alt_text” />

Make sure that you change the number 3 if you decide to do more than 3 images. This code is not exclusive for WordPress, it will work with any php based platform.

5. Control When Your Posts are Available Via RSS

Control When Your Posts are Available Via RSS

There are times when you publish a post and suddenly find an error. You can go back in the admin panel and change it, but it is already published in the feeds. With this hack, you can put a delay of as many minutes as you like, so you can double check the post live.

Open your functions.php and add this code:

function publish_later_on_feed($where) {
global $wpdb;

if ( is_feed() ) {
// timestamp in WP-format
$now = gmdate(‘Y-m-d H:i:s’);

// value for wait; + device
$wait = ‘10′; // integer

// http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_timestampdiff

// add SQL-sytax to default $where
$where .= ” AND TIMESTAMPDIFF($device, $wpdb->posts.post_date_gmt, ‘$now’) > $wait “;
return $where;

add_filter(‘posts_where’, ‘publish_later_on_feed’);

This code is adding a 10 minute delay on your post being shown on the RSS Feeds, you can change it by changing the number 10 to as many minutes as you like.

6. Display Certain Categories in a Menu

Display Certain Categories in a Menu

In many cases, users only want to display certain categories in their navigation menu at the top of the page. There are limited spots, that can only be filled by top categories, but if you use the default wp_list_categories code, it will show all categories. This is why this hack below comes in very handy when you want to create a navigation menu and only display certain categories.

<ul style=”float:left; width:730px;”>
<?php wp_list_categories(‘orderby=name&include=7,9,19,16,1,5,17,23’); ?>

Note, you can also change the ‘include’ text to ‘exclude’ and show all categories and exclude those that you don’t want displayed. The numbers displayed in the code are the category IDs. Remember since WordPress shows categories in a list format, you will need to edit the CSS in order to make it work.

7. Separate TrackBacks from Comments

Separate TrackBacks from Comments

When you write a great post on your blog, it likely to be linked from all around the blogosphere. At the same time most great posts start a good discussion in the comments. If you don’t separate your trackbacks from comments, it is very hard for your users to follow the comments and the discussion. In this hack we will show you how you can separate trackbacks from comments.

First you need to open comments.php and find a loop that looks something like this:

foreach ($comments as $comment) : ?>
// Comments are displayed here

Replace it with:

<?php //Displays comments only
foreach ($comments as $comment) : ?>
<?php $comment_type = get_comment_type(); ?>
<?php if($comment_type == ‘comment’) { ?>
<li>//Comment code goes here</li>
<?php }

<?php //Displays trackbacks only
foreach ($comments as $comment) : ?>
<?php $comment_type = get_comment_type(); ?>
<?php if($comment_type != ‘comment’) { ?>
<li><?php comment_author_link() ?></li>
<?php }


Basically this list is telling WordPress to display the comments in two list. If it is a Trackback display it separately and if it is a normal comment display it separately.

8. How to List Future “Upcoming” Posts

List Future Scheduled Posts

Everyone wants more users to subscribe to their feeds. The way to get more users excited and interested in your blog is to show them the future blog posts that you have scheduled.

First you need to schedule your posts for the future dates. Then open your sidebar.php or wherever you like to display the list of future posts, and paste the following code:

<?php query_posts(‘showposts=10&post_status=future’); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<span><?php the_time(‘j. F Y’); ?></span></p>
<?php endwhile;
else: ?><p>No future events scheduled.</p>
<?php endif; ?>

The code above is being sorted by the parameter post_status which in this case is “future”, but it can be draft, published and so on. There is also another parameter in this post that limits the number of posts being displayed, showposts=10. You can change the number to however many schedule posts you want to show your users.

9. Display Thumbnails Next to Each Post

Display Thumbnails Next to Each Post

Picture speaks thousand words. We have heard that saying before, and it is true with blogs also. You can’t possibly describe the post enough in a short excerpt, but adding an image brings life to the post and make the user want to click even more. With this custom field hack, we will show you how you can do just that.

First you need to make a default image of a size 210 x 210px which is what we are using at WPBeginner. You can name this default image defaultimage.gif or in this example we are using wpbeginner.gif and make sure you upload it to your theme directory folder.

Then you need to open your index.php and paste the following code where you want the image to be displayed.

<?php $postimageurl = get_post_meta($post->ID, ‘post-img’, true);
if ($postimageurl) {
<a href=”<?php the_permalink(); ?>” rel=”bookmark”><img src=”<?php echo $postimageurl; ?>” alt=”<?php the_title(); ?>” width=”210″ height=”210″ /></a>
<?php } else { ?>
<a href=”<?php the_permalink(); ?>” rel=”bookmark”><img src=”<?php bloginfo(‘template_url’); ?>/images/wpbeginner.gif” alt=”<?php the_title(); ?>” width=”210″ height=”210″ /></a>
<?php } ?>

10. Set an Expiration Date for Your Posts

Set an Expiration Date for Your Posts

This hack comes becomes very useful when you are running a contest because you might be posting information such as clues or hints that you don’t want to stay up for ever. Instead of manually removing the article, you can just make it expire automatically. It also works if you have a product that you are offering a discount on. You posted it on your blog, but you don’t want that discount to stay on your blog after its over. So you can remove it automatically with this code.

All you need to do is replace your WordPress Loop with this code:

if (have_posts()) :
while (have_posts()) : the_post(); ?>
$expirationtime = get_post_custom_values(‘expiration’);
if (is_array($expirationtime)) {
$expirestring = implode($expirationtime);

$secondsbetween = strtotime($expirestring)-time();
if ( $secondsbetween > 0 ) {
// For example…

Once you have done that, you can use custom fields when writing a post to set an expiration date. Make sure you select the key “expiration” and use the the following date format: mm/dd/yyyy 00:00:00

Now this hack does not remove or unpublish the article instead it just excludes the article from being displayed in the loop.

11. Delete Batches of Post Revisions

Delete Batches of Post Revisions

WordPress has a lot of good features and one of them is Post Revisions. This was included in WordPress 2.6, even though this is a good feature, it can cause some problems. One of them is increase the size of your database. Depending on how long it takes you to write a post, you might have as many as fifty post revisions. Now you can manually delete them, or you can run a simple query which we will show you in this post and get rid of all these useless revisions.

First thing you need to do is login to your phpMyAdmin and select your WordPress Database.

Click on the SQL Button and enter the following query:

DELETE FROM wp_posts WHERE post_type = “revision”;

In this code basically we looked up a table wp_posts and removed every post that had a post_type revision associated with it. Now depending on the size of your database, this may save you a lot of space.

12. Display Any RSS Feed on Your Blog

Display Any RSS Feed on Your Blog

There are often times when bloggers want to display RSS Feeds of other sites on their blog. Maybe it is from another blog of theirs that is related. This is when this hack comes handy because it makes your job much easier. There are several scripts and plugins that will do it for you, but you don’t need those because WordPress has this feature built in. And you should know that because you see the feeds in your Admin Panel Dashboard.

All you have to do is paste the following code where you want the feeds to be displayed. Most commonly placed in sidebar.php:

<?php include_once(ABSPATH.WPINC.’/rss.php’);
wp_rss(‘http://feeds2.feedburner.com/wpbeginner’, 5); ?>

Save the file and Upload it and you are done.

The function we used in this hack wp_rss is built in WordPress for future reference.

13. Display “Digg This” Button on Specific Posts with One Click

Display Digg This Button in Specific Posts

Digg Button in the article is very helpful specially when you article is submitted on digg. But you should not put the digg this button in every post because not every post is meant for digg. For example, if you post an announcement that you will be changing your theme, it does not need to be submitted on digg. This is when this hack comes in where you can just use the custom fields to display the “Digg This” Button in articles that you want.

Add this code in single.php where you want it displayed:

<?php $cf = get_post_meta($post->ID, ‘digg’, true);
if (!emptyempty($cf)) {
echo ‘http://digg.com/tools/diggthis.js” type=”text/javascript”>’} ?>

Now when you create a post use the custom field “Digg” and set any value and it will show the digg field. And if you don’t add that custom field, it will not show. Simple and easy.

14. Display Sticky Posts in One Area

Display Sticky Posts in One Area

This feature was wanted by many users therefore it was included in WordPress. Now in this hack we will show you how you can list all your sticky posts as a featured post in your home page or any other page.

$sticky = get_option(‘sticky_posts’);
rsort( $sticky );
$sticky = array_slice( $sticky, 0, 5);
query_posts( array( ‘post__in’ => $sticky, ‘caller_get_posts’ => 1 ) );

if (have_posts()) :
while (have_posts()) : the_post();


You can change the number 5 the amount of posts you want to show in your page. You can also display full posts by changing ‘the_excerpt’ value to ‘the_content’.

15. Display Ads after the First Post

Adding an ad after the post can get you really good money because advertisers really like that spot. It is one of the hot advertising spots. You can even place adsense there. But if you place as a normal code, an ad will be shown after each post which gets annoying for your users. Therefore using this hack, you can display ads after the first post.

Replace your current loop with this hacked version of the loop in your index.php

<?php if (have_posts()) : ?>
<?php $count = 0; ?>
<?php while (have_posts()) : the_post(); ?>
<?php $count ; ?>
<?php if ($count == 2) : ?>
//Paste your ad code here
<h2><a href=”<?php the_permalink(); ?>”><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>
<?php else : ?>
<h2><a href=”<?php the_permalink(); ?>”><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>

Now this code will show the ad after your second post. Make sure you insert your ad codes there.

Leave a Reply

Your email address will not be published.