10 Exceptional WordPress Hacks


One of the reasons people love WordPress so much is its great flexibility. You can change the software’s appearance with themes. You can enhance its functionality with plug-ins. And, last but not least, you can totally unleash WordPress’ power with hacks. Some time ago, we wrote a post showing 10 Killer WordPress Hacks1.

Today, let’s do it again with 10 new and totally killer WordPress hacks to make your blog stand out from the crowd. As usual, we won’t just list the hacks alone. In each entry, you’ll find an explanation of the code as well as the kinds of problems that the hack solves.

You may be interested in the following related posts:

1. Create TinyURLs On The Fly Link


The problem. Because Twitter has become a social media revolution, many bloggers and Twitter users enjoy sharing blog posts they have found and liked on Twitter. However, manually creating a TinyURL before tweeting can get a little tedious. As you probably know, Twitter can bring a lot of traffic to your blog, so it is in your interest to consistently provide short URLs to your readers.

The solution. To use this recipe, follow the simple steps below:

  1. Open your functions.php file.
  2. Paste the following code in the file:
    function getTinyUrl($url) {
        $tinyurl = file_get_contents("http://tinyurl.com/api-create.php?url=".$url);
        return $tinyurl;
  3. Open your single.php file and paste the following in the loop:
    $turl = getTinyUrl(get_permalink($post->ID));
    echo 'Tiny Url for this post: <a href="'.$turl.'">'.$turl.'</a>'
  4. That’s all you need. Each of your posts now has its own TinyURL, ready for tweeting!

Code explanation. The popular URL shortening service TinyURL provides a quick API that creates TinyURLs on the fly. When you pass a URL to http://tinyurl.com/api-create.php, the API immediately prints the related TinyURL on the screen.

Using the PHP function file_get_contents(), we can get it and assign it to the $tinyurl variable. The last part of the code retrieves the post’s permalink and passes it as a parameter to the getTinyUrl() function previously created.


2. List Upcoming Posts Link


The problem. If you often schedule posts to be published, how about displaying them in a list? This will make your readers look forward to what you’re going to publish in a few days and can help you reach new RSS subscribers. Implementing this functionality on your WordPress blog isn’t hard at all.

The solution. Nothing hard here. Just copy this code and paste it anywhere in your theme files.

<div id="zukunft">
  <div id="zukunft_header"><p>Future events</p></div>

  <?php query_posts('showposts=10&post_status=future'); ?>
  <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <div >
      <p class><b><?php the_title(); ?></b><?php edit_post_link('e',' (',')'); ?><br />

      <span class="datetime"><?php the_time('j. F Y'); ?></span></p>
  <?php endwhile; else: ?><p>No future events scheduled.</p><?php endif; ?>


Once you’ve saved the file, your upcoming posts will be displayed on your blog.

Code explanation. This code use the super-powerful query_posts() WordPress function, which allows you to take control of the WordPress loop.

The parameter used is post_status, which allows you to get posts according to their status (published, draft, pending or future). The showposts parameter is also used to define how many items you’d like to get. You can change the value of this parameter on line 4 to retrieve more or less than ten posts.


3. Create A “Send To Facebook” Button Link


The problem. In the first hack, we noted that Twitter can bring a lot traffic to your blog. Another website that can boost your traffic stats easily is Facebook. In this hack, let’s see how we can create a “Send to Facebook” button for your WordPress blog.

The solution.

  1. Open the single.php file in your theme.
  2. Paste the following code in the loop:
    <a href="http://www.facebook.com/sharer.php?u=<?php the_permalink();?>&t=<?php the_title(); ?>" target="blank">Share on Facebook</a>
  3. Alternatively, you could use the getTinyUrl() function to send a short URL to Facebook:
    <?php $turl = getTinyUrl(get_permalink($post->ID)); ?>
    <a href="http://www.facebook.com/sharer.php?u=<?php echo $turl;?>&t=<?php the_title(); ?>" target="blank">Share on Facebook</a>
  4. That’s all. Your readers will now be able to share your blog post on Facebook with their friends!

Code explanation. This useful hack is very easy to understand: the only thing we do here is retrieve the post’s permalink and title and send them as parameters to http://www.facebook.com/sharer.php.

In the alternative method, we used the getTinyUrl() function (created in the previous hack) to send a short URL instead of the post’s permalink.


4. Create A Maintenance Page For Your WordPress Blog Link


The problem. One thing I really like about Drupal is the option to temporarily redirect visitors to a maintenance page. Sadly, WordPress doesn’t have this feature. When you upgrade your blog, switch themes or make design changes, you may not want your visitors to see your blog as it is being tweaked, especially if it has design or code problems or, even worse, security gaps.

The solution. To solve this problem, we use the power of the .htaccess file. Just follow the steps below to get started.

  1. Create your maintenance page. A simple WordPress page is generally sufficient.
  2. Find your .htaccess file (located at the root of your WordPress installation) and create a back-up.
  3. Open your .htaccess file for editing.
  4. Paste the following code:
    RewriteEngine on
    RewriteCond %{REQUEST_URI} !/maintenance.html$
    RewriteCond %{REMOTE_ADDR} !^
    RewriteRule $ /maintenance.html [R=302,L]
  5. Replace on line 3 with your IP address (Don’t know it?9). Make sure to use the same syntax.
  6. Now, all visitors except you will be redirected to your maintenance page.
  7. Once you’re done tweaking, upgrading, theme switching or whatever, re-open your .htaccess file and remove (or comment out) the redirection code.

Code explanation. The .htaccess file, which controls the Apache Web server, is very useful for these kinds of tasks.

In this example, we state that any visitor who has an IP different from (which doesn’t request maintenance.html) should be redirected to maintenance.html.

By replacing with your own IP address, you make sure you’re still allowed to browse your blog normally, while others are redirected to maintenance.html.



The problem. One well-known way of keeping visitors on your blog longer and helping them discover news posts is to display, usually at the end of the article, a list of related content.

Many plug-ins will do this job, but why not super-charge your theme by integrating this functionality by default?

The solution.

  1. Open the single.php file in your theme.
  2. Paste the following code in the loop:
    //for use in the loop, list 5 post titles related to first tag on current post
    $tags = wp_get_post_tags($post->ID);
    if ($tags) {
      echo 'Related Posts';
      $first_tag = $tags[0]->term_id;
        'tag__in' => array($first_tag),
        'post__not_in' => array($post->ID),
      $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>
  3. Save the file, and then have a look at your blog: related posts are automatically displayed!

Code explanation. This hack uses tags to retrieve related posts. The first thing it does is get the post’s tags. If a post has tags, the first one is extracted and used in a query that retrieves posts with the same tag.

By default, this code displays up to five related posts. To change this number, simply edit line 9 of the code.


6. Automatically Retrieve The First Image From Posts On Your Home Page Link


The problem. Many WordPress users use custom fields to display a thumbnail on their blog home page. Of course, this is a nice solution, but how about automatically retrieving the first image from a post and using it as a thumbnail?

The solution. This hack is quite easy to implement:

  1. Open the functions.php file in your theme.
  2. Paste this code in. Don’t forget to specify a default image on line 10 (in case a post of yours does not have an image).
    function catch_that_image() {
      global $post, $posts;
      $first_img = '';
      $output = preg_match_all('/<img.+src=['"]([^'"]+)['"].*>/i', $post->post_content, $matches);
      $first_img = $matches [1] [0];
      if(empty($first_img)){ //Defines a default image
        $first_img = "/images/default.jpg";
      return $first_img;
  3. Save the functions.php file.
  4. On your blog home page (index.php), call the function this way to get the URL of the first image from the post:
    <?php echo catch_that_image() ?>

Code explanation. The function uses the global variable $post to parse the post’s content with a regular expression. If an image is found, its URL is returned by the function. If not, the default image URL is returned.


7. Resize Images On The Fly Link


The problem. When you use thumbnails on your blog’s home page or even images in posts, having to manually resize them is boring and wastes a lot of time. So, why not use the power of PHP to do it?

The solution. To achieve this hack, just follow these simple steps:

  1. Get this script14 and save it on your computer (I’ll assume you’ve named it timthumb.php).
  2. Use an FTP program to connect to your server and create a new directory called scripts. Upload the timthumb.php file to it.
  3. Once done, you can display images like so:
    <img src="/scripts/timthumb.php?src=/images/whatever.jpg&h=150&w=150&zc=1" alt="Screenshot" />

    In other words, you just call the timthumb.php file and pass your image as a parameter. The same goes for your desired width and height.

Code explanation. The timthumb.php script use the PHP GD library, which allows you to manipulate images dynamically with PHP. GD is installed by default on all servers running PHP5. If you’re not running PHP5, you’ll have to check if GD is installed before using this script.

The timthumb.php file gets the parameters you’ve passed to it (image URL, width and height) and uses it to create a new image with your stated dimensions. Once that’s done, the image is returned to you.



The problem. Displaying your most popular posts is a good way to make visitors stay longer on your blog, as is displaying related posts. Many great plug-ins can list your most popular posts, but again, why use a plug-in when you can simply hack your WordPress theme to do it automatically?

The solution. Just paste the following code anywhere in your theme files (for example, in sidebar.php). To change the number of displayed posts, simply change the “5” on line 3 to your desired number.

<h2>Popular Posts</h2>
<?php $result = $wpdb->get_results("SELECT comment_count,ID,post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 5");
foreach ($result as $post) {
$postid = $post->ID;
$title = $post->post_title;
$commentcount = $post->comment_count;
if ($commentcount != 0) { ?>

<li><a href="<?php echo get_permalink($postid); ?>" title="<?php echo $title ?>">
<?php echo $title ?></a> {<?php echo $commentcount ?>}</li>
<?php } } ?>


Code explanation. This code executes an SQL query to the WordPress database, using the $wpdb object, to get a list of the five posts with the most comments. The results are then wrapped in an unordered HTML list and displayed on screen.


9. Highlight Searched Text In Search Results Link


The problem. The WordPress search engine system is often criticized for not being powerful enough. One of its weakest points in my opinion is that searched text is not easily distinguishable from the rest of the text. Let’s solve that!

The solution.

  1. Open your search.php file and find the the_title() function.
  2. Replace it with the following:
    echo $title;
  3. Now, just before the modified line, add this code:
      $title  = get_the_title();
      $keys= explode(" ",$s);
      $title  = preg_replace('/('.implode('|', $keys) .')/iu',
        '<strong class="search-excerpt"></strong>',
  4. Save the search.php file and open style.css. Add the following line to it:
    strong.search-excerpt { background: yellow; }

That’s all. Better, isn’t it?

Code explanation. Once again, regular expressions are a lifesaver. The regexp parses the $s content ($s is the variable containing the searched text) and automatically adds a <strong class=”search-excerpt”> element around any occurrences of $s.

Then, you simply modify your style.css file to give searched text a special style and make it more visible to your readers.


10. Disable Widgetized Areas Without Editing Theme Files Link


The problem. Widgets are very useful, but sometimes you don’t need them on a particular page or post. Sure, you can create a page template for a particular page or even remove the widgetized zone from the code, but a much better and more elegant solution exists.

The solution. To do this, simply add the following code to your functions.php file:

add_filter( 'sidebars_widgets', 'disable_all_widgets' );

function disable_all_widgets( $sidebars_widgets ) {
  if ( is_home() )
    $sidebars_widgets = array( false );
  return $sidebars_widgets;

Code explanation. This code first adds a filter to the sidebars_widgets WordPress function. Now every time WordPress tries to execute this function, it will execute the disable_all_widgets function we just created.

The disable_all_widgets function uses WordPress conditional tags (in this example, is_home(), but you can use any conditional tag) to disable all widgets if a visitor is on a particular page or post.


You may be interested in the following related posts:


Footnotes Link

  1. 1 http://www.smashingmagazine.com/2009/01/07/10-killer-wordpress-hacks/
  2. 2 http://www.smashingmagazine.com/2009/03/04/15-useful-twitter-plugins-and-hacks-for-wordpress/
  3. 3 http://www.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/
  4. 4 http://www.smashingmagazine.com/2008/12/18/8-useful-wordpress-sql-hacks/
  5. 5 http://www.smashingmagazine.com/2008/12/02/10-useful-rss-hacks-for-wordpress/
  6. 6 http://www.wprecipes.com/how-to-automatically-provide-tinyurls-for-your-wordpress-blog-posts
  7. 7 http://www.wprecipes.com/how-to-list-future-posts
  8. 8 http://www.wprecipes.com/how-to-add-a-share-on-facebook-link-to-your-wordpress-blog
  9. 9 http://www.ip-adress.com/
  10. 10 http://www.catswhocode.com/blog/10-awesome-htaccess-hacks-for-wordpress
  11. 11 http://www.wprecipes.com/how-to-show-related-posts-without-a-plugin
  12. 12 http://www.wprecipes.com/how-to-get-the-first-image-from-the-post-and-display-it
  13. 13 http://wordpress.org/support/topic/246893
  14. 14 http://timthumb.googlecode.com/svn/trunk/timthumb.php
  15. 15 http://www.darrenhoyt.com/2008/04/02/timthumb-php-script-released/
  16. 16 http://www.wprecipes.com/how-to-resize-images-on-the-fly
  17. 17 http://www.problogdesign.com/wordpress/create-your-own-popular-posts-page/
  18. 18 http://yoast.com/wordpress-search/
  19. 19 http://www.wprecipes.com/how-to-enlight-searched-text-in-search-results
  20. 20 http://justintadlock.com/archives/2009/03/06/disable-widget-areas-without-touching-theme-templates
  21. 21 http://www.smashingmagazine.com/2009/03/04/15-useful-twitter-plugins-and-hacks-for-wordpress/
  22. 22 http://www.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/
  23. 23 http://www.smashingmagazine.com/2009/01/26/10-steps-to-protect-the-admin-area-in-wordpress/
  24. 24 http://www.smashingmagazine.com/2008/12/18/8-useful-wordpress-sql-hacks/
  25. 25 http://www.smashingmagazine.com/2008/12/02/10-useful-rss-hacks-for-wordpress/

↑ Back to top Tweet itShare on Facebook

This guest post was written by Jean-Baptiste Jung, a 28-year-old blogger from Belgium, who blogs about Web Development on Cats Who Code, about WordPress at WpRecipes and about blogging on Cats Who Blog . You can stay in touch with Jean by following him on Twitter.

  1. 1

    Great article!!

  2. 2

    Nice post but most of them are really simple and the rest have already been listed on previous posts.

  3. 3

    I am in the process of developing my personal homepage, and I am not sure if I should use WordPress or not. I have experimented with it in the past but am not sure if it is appropriate for a graphic designer/web designer/developer to use. I wonder if I should write my own PHP or use WordPress. Either way, great article, there are a lot of tools here to help with social networking.

  4. 4

    This is, as usual, pure gold, top stuff!

  5. 5

    Really helpful. I’ll use it for bulding templates, thanks!

  6. 6

    Thanks for the info.

  7. 7

    Helpfull stuff, thanks! (:

  8. 8

    Andrea Pelizzardi

    April 15, 2009 5:08 am

    Another way to do the “8. Get Your Most Popular Posts Without A Plug-In”.

    Anyway, good article… not great :)

  9. 9

    Fantastic! The upcoming posts thing sure looks handy. I like to keep my flow of posts consistent but sometimes I have a million and one ideas for posts that I keep as drafts. Having this upcoming posts list is a cracker.

  10. 10

    awesome post, back on form Smashing Magazine! Keep up the good work!

  11. 11

    David (Marketing Integrity)

    April 15, 2009 5:20 am

    These are helpful custom solutions that really benefit an intermediate WordPress user. Thanks-a-million!

  12. 12

    re: Create TinyURLs On The Fly:

    isn’t this opening a connection with tinyurl every time the page loads despite there is already a shortened url existing for the post?
    and what will happen, when tinyurl.com is not available?
    is the page loading then anyway?

    and what about a solution using the short link provided by wp itself with the …/?p=999 format like “http : / / smashingmagazine.com / ? p=123” ?
    your twitter links wouldn’t be dependant from an external source and your domain name would be instantly visible.

  13. 13

    Matheus Almeida

    April 15, 2009 5:31 am

    Hi Jean, great post! I loved it!
    I’m adding some of those hacks to my blog right now! lol

    But I found an problem at tip #9 when I was modifying my code.
    There’s a missing “$1”, so the regex keep the found word to highlight.
    So, it shold look like this:
    <strong class="search-excerpt">$1</strong>

    Thanks for the tips!!

  14. 14

    Hey Jean-Baptiste, very useful hacks, although I knew a few of them already from your Blogs. But anyway, it’s nice to read an article in my favourite magazine by my favourite blogger. ;-)

    Thanks to you, I will have to sit down for several hours and tweak my Blog.

    By the way: With so many Blogs of yours, please write an article about a good Blog-Time-Management. xD

  15. 15

    Just noticed that the RSS icon in the top right part of the site looks like it is in the WC. ROFL.

  16. 16

    Daryn St. Pierre

    April 15, 2009 5:54 am

    @Lindsay Lee – Why re-invent the wheel though? WordPress has a huge following and the vast amount of plugins, hacks and users makes it completely worthwhile. Although making your own thing is fun.

    I think this is a great article, regardless of whether or not some of them were posted elsewhere. I wasn’t aware of them and I’m sure a lot of other people weren’t either. Now they are. There are a lot of hacks here that I can actually use in everyday projects.

  17. 17

    This is a really nice post, some great tips on the best way to achieve common tasks without the plugins. Thanks!

  18. 18

    That’s a gr8 list. Smashing magazine articles are really useful.

  19. 19

    I do not think it would be wise to follow tip #1 as it would slow down your blog A LOT because whenever your blog gets a request WP must contact TinyURL in order to fetch the URL. The same URL, time and time again.

  20. 20

    Nice list…wish you would do a similar one for Drupal or Joomla.

  21. 21

    Adam Kayce : Monk at Work

    April 15, 2009 6:40 am

    Your posts consistently rock.

  22. 22

    Some interesting tips in there, thanks a lot!

  23. 23

    Very useful article. Thanks for that dedicated work.

  24. 24

    I always wondered which is the better option, using your own theme functions or using plugins. Is there any performance advantage to be gained either option?

  25. 25

    @andy about TinyUrls

    those are some valid remarks; these issues occurs with every external service.
    I’ve had my share with IntenseDebate and sometimes Google Analytics is very slow.

    But your remark about WP’s build in pagenumbering might be just what I’m looking for; why bother with TinyURL indeed!
    Why haven’t I thought about that myself!

    Will try to implement that on my upcoming fan-tas-tic project…thnx for the tip!

  26. 26

    Strange you didn’t mention supporting mobile.

    Actually with a simple theme, you can use tinysrc.mobi to do off-server image resizing (per mobile) – and you’ve got a decent mobile blog.

    Probably simple enough to warrant being called a hack.

  27. 27

    great post, but I have seen a very similar post published 5 days earlier than this one…

  28. 28

    wordpress blogger

    April 15, 2009 8:02 am

    I like the tiny URL hack..thanks for the post.

  29. 29

    Nothing brand new here, but still a good roundup.

    Especially liked #7 Resizing images on the fly. Wish WordPress had that built into the core!

  30. 30

    Heguiberto Souza

    April 15, 2009 8:06 am

    Nice post, I just need to get my butt out of this malaise and start the project of creating my own page I have been postponing for so long

  31. 31

    Great tip! But you can even highlight the searched text in the excerpt. Just change the $title to $excerpt, and change get_the_title with get_the_excerpt. echo the $excerpt variable in your search results and you’re done!

  32. 32

    Re: Display Related Posts Without A Plug-In
    This is a list of links, so it would be better to make it a <ul>.

    Anyway, I like the post. It makes hacking WP look so simple.

  33. 33

    Awesome stuff! Although, I think if there was a Plugin that could do the same thing as a Hack, I’d just use the Plugin.

  34. 34
  35. 35

    nice, it will help me with the current project I’ve got to do. by the way, anyone interested to help me with some hacks and plugins, templates etc? there is plenty to do and I can easy give away some parts to other developer which hopefully can speed up the development. more info – priv – just email me lokers(at)gmail.com

    good article! as always! thank you

  36. 36

    Great article, although, the related articles hack will not work for me. I’ve tried it on two of my many wordpress blogs. I keep getting an “Error on line __” where on the line is the ‘?php endwhile;’

    What am I doing wrong? Thanks!

  37. 37

    For #4, Create A Maintenance Page For Your WordPress Blog, I use a plug-in. This is easier than manually editing the htaccess each time and allows all the admins to view the site while in maintenance.


  38. 38

    Great stuff here, some which I’ve been looking for and had never found them until now. Make more posts like this even if it has been posted somewhere else. Sometimes we don’t have the time to search all around the web and just like to find it here.

    Even though each hack describes what they each do but it would be more helpful to show Demos of each hack.


  39. 39

    Any chance we could get a new article without a number in the headline? This is linkbait gone mad!

  40. 40

    Love it love it love it! For my own personal blog, I just need to find a good, free, flexible wordpress theme now.

  41. 41

    TX CHL Instructor

    April 15, 2009 11:18 am

    What happens to the ‘hacks’ when you install the next WP update? Do you have to re-do all of the hacks? That could get old…

  42. 42

    Excelent article. I learned a lot from it. I am a novice wordress designer and allways wanted to know how to resize images on the fly. Thanks for this great information!.

  43. 43

    this is a wonderful wonderful list! i will be using #4 Create A Maintenance Page For Your WordPress Blog!!! whoo hoo!

  44. 44

    Digital Photography

    April 15, 2009 12:31 pm

    Great information on these WordPress hacks!

  45. 45

    Anyone interested in using the built-in short/default URL’s in WordPress instead of TinyURL, like stated in posts #12 and #25, you should consider this post of mine:
    Need a short URL service? Try WordPress!

    It works for me… ;)

  46. 46

    Thanks for the awesome article. I will definitely use some of these.

  47. 47

    How could I get the tinyurl to work with tr.im’s url shortening? I cant get it to work!

  48. 48

    @Montana Flynn
    this should work with tr.im:

    function getTinyUrl($url) {
    $tinyurl = file_get_contents("http://api.tr.im/api/trim_simple?url=?url=".$url);
    return $tinyurl;

    the rest stays the same

  49. 49

    sorry, this got mixed up because of the link :)

    here again, change the hxxp to http :

    function getTinyUrl($url) {
    $tinyurl = file_get_contents("hxxp://api.tr.im/api/trim_simple?url=".$url);
    return $tinyurl;

  50. 50

    There’s another way to temporarily take down a WordPress site very easily:
    Update WordPress with Minimal Downtime


↑ Back to top