Free Wordpress Blog Setup

15 Killer Hacks for WordPress that Are Extremely Useful

By Editorial Staff in Tutorials
15 Killer Hacks for WordPress that Are Extremely Useful

WordPress community is growing fast, and we are coming up with new hacks every day. In this article we will be sharing with you some of the killer, most wanted, and extremely useful WordPress Hacks that you can use to unleash the power of this blogging software that we all love. We will try to explain how each hack works, if you don’t understand something feel free to ask the question in the comments.

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>
<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';
</script>

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
$device = ‘MINUTE’; //MINUTE, HOUR, DAY, WEEK, MONTH, YEAR

// 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 class="navmenubar" style="float:left; width:730px;">
<?php wp_list_categories('orderby=name&include=7,9,19,16,1,5,17,23'); ?>
</ul>

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
endforeach;

Replace it with:

<ul class="commentlist">
<?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 }
endforeach;
</ul>

<ul>
<?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 }
endforeach;

</ul>

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 class="datetime"><?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:

<?php
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…
the_title();
the_excerpt();
}
endwhile;
endif;
?>

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.

<?php
$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();
the_title();
the_excerpt();
endwhile;
endif;

?>

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.

Free Wordpress Blog Setup

Comments

42 Responses to “15 Killer Hacks for WordPress that Are Extremely Useful”
  1. Aen says:

    Something better for no. 9. Automatically use the first image uploaded in a post w/o the need for custom fields.

    http://www.wp-fun.co.uk/2009/01/08/post-image-the-easy-peasy-way/

  2. Danillo says:

    Thanks a lot! Great list! =)

  3. ArmNo says:

    Great hacks collection!

    Thanks for sharing.

  4. joseorestes says:

    Wow cool I will try the 15, thanks!

  5. Louis Dizon says:

    Thanks for these hack tips. I’ll make use of some of these especially the Digg button. I love it. Now off to my site. :)

  6. Anyone know if these hacks will work in the custom_functions.php file in Thesis? I can’t think of any reason why they shouldn’t, and that would be ideal for upgrading, etc.

    Thanks for a great set of hacks!

  7. Great hacks. Now I’m confused from which I should start :-)

  8. Cocktaille says:

    Great list!

    But the script for the Digg-button is incorrect. For instance !emptyempty should be !empty.

    There is missing some javascript code near the echo-statement. This should be:

    And before the last brace there should be a semicolon.

    I tried the script, works great.

    Thx

  9. Nick says:

    Great list – thanks!

    Lots to add to my “To-Do” list!

  10. wow, brilliant. wordpress is amazing. and these little things make it such a wonderful experience.

  11. Wish I had time to do all these great things. THANKS so much for taking the time to share this knowledge. I already use a few of your great tips on some of my blogs, but you inspired me to try some new ones. The first one I will try out is to change the Gravatar image. Thanks again. Diane from Montreal

  12. Amal Roy says:

    This is just excellent. All the hacks are of great use. Really great. Keep going.

  13. Keke says:

    In number 4, the .gif (in text) and .jpg (in code) got mixed up.

  14. maggot says:

    Great Hacks! Thanks for sharing!

  15. nazcar says:

    great list. thank you for sharing..

  16. Useful hacks, thanks for write..

  17. Alex says:

    For No. 6 you can also do this for pages, not just categories.
    And here is how I have it formatted in order to keep it in a default list:

    include=1,2,3&title_li=

    I thought this might be helpful for some since I had to do a bit of tweaking in order to make it work.

    Great list though WPB! Thanks!

  18. Susie Cheng says:

    Thanks so much for sharing these valuable information with us. What a great lists! I will use them form my blog now. I would like to know where I should find the Template Folder. How come I couldn’t find it?

    Thanks once again and I look forward to your next article!

    Best regards,

    Susie Cheng
    http://susiecheng.wordpress.com/

  19. Canyon says:

    Sorry for my english.
    Pour your function print_post_title()

    I do a thing like this: you put

    ID, “link”,true) ? print( get_post_meta($post->ID, “link”, true)) : the_permalink(); ?> instead of at the loop in your template.
    When you want activate this, in your post, go to the customs fiels and create a custom field like ‘link’ and put your url.

    Regards.

  20. Dirk Loehn says:

    Great tips, really! With regard to 3, the code above does not show the title as in

    RT @yoursitename Title of the Post – Link

    The following code will do:

    tweetmeme_source = ‘wpbeginner ‘ + ”;

    • In the code for hack 3, it is actually the JavaScript doing the job. As you see in the code, there is nothing related to the title.

      We are using the same script on WPBeginner and you can see that it is working.

      The reason why it is not working is probably due to your server having some sort of error with this script. This script is actually provided by tweetmeme themselves. We suggest you talk to your host about this issue. It can also verywell be a caching issue if you are using WP Super Cache.

  21. Jef says:

    Nice post, love the hack about DiggThis button…

  22. Huw Waters says:

    Good article, however one small error.

    In Hack no. 1, you’re missing a semi-colon for the text to be inserted into index.php

    print_post_title() —> print_post_title();

  23. wayne says:

    Wow. GREAT stuff! Thanks so much!

  24. I guess I don’t get the point of these hacks. Why wouldn’t you just write this type of functionality into a plug in and not risk your site being broken every time Wordpress has an upgrade. Am I missing something?

  25. Great hacks collection! Powerfull!

    Thanks for sharing.

  26. Tocki says:

    Realy helpful. Thanks for sharing

  27. Detoam says:

    I am sorry for sounding dumb, but I can’t figure out how to do number 15.

Share Your Opinions

Tell us what you're thinking...
and if you want a pic to show with your comment, then get gravatar!

Due to high volume of request from our readers, we are adding this feature that allows you to stay updated with this post's comments without having to participate in the discussion even though we would love your input as always. Don't worry we hate SPAM just as much as you do, so you will never receive any SPAM messages from our site and that's our promise to you.

Subscribe without commenting