2009 has been a very prolific year for WordPress hacks. In this article, I'll show you the most useful hacks I came across during the whole year. Enjoy!

Monetizing your old blog posts

Let’s start this post with a nice hack dedicated to help you make more money online, initially published on my other blog Cats Who Blog.
If you don’t want to bore your loyal readers but still want to earn some bucks, what about monetizing only your old blog posts instead? This code will add some advertisements only if the post is more than 15 days old.

The following function has to be pasted in your functions.php. If you are using the Thesis theme this file is named custom_functions.php.

function is_old_post($post_id=null){
   $days = 15;
   global $wp_query;
   if(is_single() || is_page()) {
      if(!$post_id) {
         $post_id = $wp_query->post->ID;
      }
      $current_date = time();
      $offset = $days *60*60*24;
      $post_id = get_post($post_id);
      $post_date = mysql2date('U',$post_id->post_date);
      $cunning_math = $post_date + $offset;
      $test = $current_date - $cunning_math;
      if($test > 0){
         $return = true;
      }else{
         $return = false;
      }
   }else{
      $return = false;
   }
   return $return;
}

Once you’ve successfully inserted the code in your function.php file, you are now ready to call the functions in your single.php template as shown below:

<?php if(is_old_post()){ ?>
INSERT AD CODE HERE
<?php } ?>

Source : http://www.catswhoblog.com/how-to-monetize-your-old-blog-posts

Display your posts word count

Many people asked me about being able to get the post word count and display it. It is definitely easier to do than you may think at first.
Simply open your functions.php file and paste this function in it:

function wcount(){
    ob_start();
    the_content();
    $content = ob_get_clean();
    return sizeof(explode(" ", $content));
}

Once finished, you can call the function within the loop to get the number of words for the current post:

<?php echo wcount(); ?>

Source : http://www.wprecipes.com/wordpress-function-to-display-your-posts-words-count

Detect the visitor browser within WordPress

One of my favorite WordPress hacks of the year is definitely this one, which is incredibly useful. While conditional comments are a great way to target specific browsers, WordPress has one detection function that you can use to make your web developer life easier.

<?php
add_filter('body_class','browser_body_class');
function browser_body_class($classes) {
	global $is_lynx, $is_gecko, $is_IE, $is_opera, $is_NS4, $is_safari, $is_chrome, $is_iphone;

	if($is_lynx) $classes[] = 'lynx';
	elseif($is_gecko) $classes[] = 'gecko';
	elseif($is_opera) $classes[] = 'opera';
	elseif($is_NS4) $classes[] = 'ns4';
	elseif($is_safari) $classes[] = 'safari';
	elseif($is_chrome) $classes[] = 'chrome';
	elseif($is_IE) $classes[] = 'ie';
	else $classes[] = 'unknown';

	if($is_iphone) $classes[] = 'iphone';
	return $classes;
}
?>

The final result will look something like this, if you view the source code of your page:

<body class="home blog logged-in safari">

Source : http://www.nathanrice.net/blog/browser-detection-and-the-body_class-function/

Get short urls for social bookmarking

With the rise of Twitter and its 140 characters limit, bloggers have to use short urls to fully take advantage of this new social media phenomenon.
Lots of quality url shorteners are available, but this trick will create a shorter version of your urls automatically, making you save time and hassle.
Paste the following code on your single.php file:

<?php echo get_bloginfo('url')."/?p=".$post->ID; ?>

It will output a url similar to:

http://www.catswhocode.com/?p=54

Source : http://www.wprecipes.com/how-to-get-short-urls-for-social-bookmarking

Get the first image from the post and display it

This hack has been a favorite of WpRecipes during the year 2009. And I understand that because this hack is very useful, especially for “magazine” themes: It allows you to automatically get the first image from the current post, and display it.

The first thing to do is to paste the function below on your functions.php file.

function catch_that_image() {
  global $post, $posts;
  $first_img = '';
  ob_start();
  ob_end_clean();
  $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;
}

Once finished, you can simply call the function within the loop to display the first image from the post:

<?php echo catch_that_image() ?>

Source : http://www.wprecipes.com/how-to-get-the-first-image-from-the-post-and-display-it

Use SSL on wp-admin directory

On the internet, security is always a concern. If your hosting provider supports it (Wp WebHost and HostGator does) you should definitely enable SSL support.
SSL is a cryptographic protocol that provide security and data integrity for communications over TCP/IP networks such as the Internet. TLS and SSL encrypt the segments of network connections at the Transport Layer end-to-end.
Open the wp-config.php file and paste the following:

define('FORCE_SSL_ADMIN', true);

Next, save the file, and you’re done!
Source : http://www.wprecipes.com/how-to-force-using-ssl-on-wp-admin-directory

Enhancing the search function

WordPress has a built-in “search” function which isn’t bad, but should have been better. For example, one of the things that could enhance it is to highlight the search results.
To do so, open your search.php file and insert this code:

<?php
	$title = get_the_title();
	$keys= explode(" ",$s);
	$title = preg_replace('/('.implode('|', $keys) .')/iu',
		'<strong class="search-excerpt">\0</strong>',
		$title);
?>

Then, you’ll have to define a style for the search-excerpt CSS class. Just open style.css and paste:

strong.search-excerpt { background: yellow; }

Source : http://yoast.com/wordpress-search/

Post on your WordPress blog using PHP

Many of you have enjoyed my “Awesome things to do with cURL” article, published in June. One of the most interesting snippets from that article is showing how to post articles on your WordPress blog, using PHP and cURL.

Here is the function. This code is not made for being used within WordPress, so don’t paste it on your functions.php file (or any other).

Please note that you must activate the XMLRPC posting option in your WordPress blog. If this option isn’t activated, the code will not be able to insert anything into your blog database. Another thing, make sure the XMLRPC functions are activated on your php.ini file.

function wpPostXMLRPC($title, $body, $rpcurl, $username, $password, $category, $keywords='', $encoding='UTF-8') {
    $title = htmlentities($title,ENT_NOQUOTES,$encoding);
    $keywords = htmlentities($keywords,ENT_NOQUOTES,$encoding);

    $content = array(
        'title'=>$title,
        'description'=>$body,
        'mt_allow_comments'=>0,  // 1 to allow comments
        'mt_allow_pings'=>0,  // 1 to allow trackbacks
        'post_type'=>'post',
        'mt_keywords'=>$keywords,
        'categories'=>array($category)
    );
    $params = array(0,$username,$password,$content,true);
    $request = xmlrpc_encode_request('metaWeblog.newPost',$params);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
    curl_setopt($ch, CURLOPT_URL, $rpcurl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 1);
    $results = curl_exec($ch);
    curl_close($ch);
    return $results;
?>

Source : http://www.wprecipes.com/post-on-your-wordpress-blog-using-php

Rewrite author name with custom field

If you often invite other bloggers to post on your blog, this tip is a must have. It simply allows you to create a custom field with the name of the guest author, and it will overwrite the the_author(); functions.
Simply paste the following code on your single.php and page.php, where you want the author name to be displayed.

<?php $author = get_post_meta($post->ID, "guest-author", true);
if ($author != "") {
    echo $author;
} else {
    the_author();
}  ?>

Source : http://www.wprecipes.com/rewrite-author-name-with-custom-field

Detect mobile visitors on your WordPress blog

Mobile devices as such the Blackberry or iPhone are more and more popular everyday, and this is why you definitely should take those readers in consideration by offering them a mobile version of your blog.
This hack is definitely easy to implement, thanks to Jeff Starr and Chris Coyier, the author of the excellent “Digging into WordPress” book.

To achieve this recipe, you first have to get the code from detectmobilebrowsers.mobi and upload it to your theme directory.

Then, simply open your header.php file and place the following at the top of the file. Don’t forget to edit line 5 according to the page where you’d like to redirect mobile users.

include('mobile_device_detect.php');
$mobile = mobile_device_detect();

if ($mobile==true) {
  header( 'Location: http://your-website.com/?theme=Your_Mobile_Theme' ) ;
}

Source : http://digwp.com/2009/12/redirect-mobile-users-to-mobile-theme/

By the way, if you’d like to win a premium WordPress theme, check out the contest I’m currently running on Cats Who Blog.

 

51 Comments

  1. Posted December 22, 2009 at 5:05 pm | Permalink

    nice collection – keep up the great work and have a wondeful time!

  2. Posted December 22, 2009 at 8:06 pm | Permalink

    Nice list.
    I would add another one for monetizing – display ads only to visitors from search engines. Not sure where I got the code from, perhaps from here :D

  3. Posted December 22, 2009 at 8:30 pm | Permalink

    nice hacks , by the way i have written a tutorial on how to add Twitter ID in your comment box. it for eg: you have seen in colourburned blog. can you add here :D

  4. Posted December 22, 2009 at 8:51 pm | Permalink

    @Denis : I guess you had it from WpRecipes or CatsWhoBlog.com :)

  5. Posted December 22, 2009 at 9:42 pm | Permalink

    The monetizing old posts is something that I could do with implementing on my blog. I ghet a few complaints about ads, hopefully this can help. :)

  6. Posted December 22, 2009 at 9:59 pm | Permalink

    Great list. Had read quite a few of them. Nice to freshen my memory.
    Thanks

  7. Chakib
    Posted December 22, 2009 at 10:59 pm | Permalink

    nice collection , but … hum ! is that all for 2009 ????

  8. Posted December 22, 2009 at 11:46 pm | Permalink

    These look great thanks!! Thanks for the rundown!

  9. Posted December 23, 2009 at 12:54 am | Permalink

    great collection! i’ve been looking for the word count hack… Thanks!

  10. Posted December 23, 2009 at 1:07 am | Permalink

    Great list, thanks for sharing!

  11. Posted December 23, 2009 at 2:50 am | Permalink

    Very Nice, do you hay more of the hacks? They are very interesting!

  12. Posted December 23, 2009 at 4:24 am | Permalink

    Thanks for this great list!

  13. Posted December 23, 2009 at 4:35 am | Permalink

    Awesome collection of hacks! Thank you for collecting them all into one convenient post. The browser-detection/body-class filter is by far my favorite :)

  14. Jancok
    Posted December 23, 2009 at 8:52 am | Permalink

    Cool…

  15. Posted December 23, 2009 at 9:06 am | Permalink

    These hacks are just stunning. I am really impressed by WordPress. I will surely play around a lot with WP.

    Thanks for this lovely post.

  16. Posted December 23, 2009 at 9:10 am | Permalink

    @Jeff Starr : I have to agree that the browser detection class is one of my favorites…The “Get 1st image from the post and display it” has been a real life saver for me during the whole year!

  17. Posted December 23, 2009 at 12:10 pm | Permalink

    A small addition to “Post on your WordPress blog using PHP” (or any other cURL related snippet):
    When cURL isn’t enabled at your host you can use an alternative method which uses the PHP function fopen()

    function send($url, $data) {
    $connection = @curl_init($url);
    if($connection) { // check if there is a cURL connection
    $data = http_build_query($data);
    curl_setopt($connection, CURLOPT_POST, true);
    curl_setopt($connection, CURLOPT_POSTFIELDS, $data);
    curl_setopt($connection, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($connection);
    $status = curl_getinfo($connection, CURLINFO_HTTP_CODE);
    curl_close($connection);
    } else {
    $params = array(
    ‘http’ => array(
    ‘method’ => ‘POST’,
    ‘content’ => $data
    )
    );
    $ctx = stream_context_create($params);
    $connection = @fopen($url, ‘rb’, false, $ctx);
    if(!$connection) $status = ‘400′;
    else {
    $result = @stream_get_contents($connection);
    if($result === false) $status = ‘400′;
    else $status = ‘200′;
    }
    }
    return array(’result’ => $result, ’status’ => $status);
    }

    This function requires PHP 4.3 and no additional libraries.

  18. Hirsute
    Posted December 23, 2009 at 12:27 pm | Permalink

    About the “Display your posts word count” hack. What about the “str_word_count()” function ? much easier than exploding a string !

  19. Posted December 23, 2009 at 12:53 pm | Permalink

    @Johan de Jong : Thanks a lot for this code! Very useful indeed!

  20. Posted December 23, 2009 at 1:17 pm | Permalink

    Nice list here. Surely through getting a word count via exploding your entire post is slow.

  21. Posted December 23, 2009 at 1:33 pm | Permalink

    thanks for sharing the great lists..

  22. Posted December 23, 2009 at 2:03 pm | Permalink

    Great hack’s thanks!

  23. Posted December 23, 2009 at 2:03 pm | Permalink

    Great List Jean. I might end up using the first image hack somewhere.

  24. Posted December 23, 2009 at 2:21 pm | Permalink

    Thanks very much for this list. Definitely going to be using a few of these

  25. Posted December 23, 2009 at 2:23 pm | Permalink

    Fantastic hacks!! Going to use the search one, it’s something I’ve always wanted my blog to do!

  26. Posted December 23, 2009 at 2:32 pm | Permalink

    PHP has a word count function str_word_count, this might give better and faster results that exploding a sting.

    php.net/manual/en/function.str-word-count.php

  27. Posted December 23, 2009 at 2:52 pm | Permalink

    Great job !! I impressed to see. this is a nice post it worth to learn some more thing abut programing…really Many people may be able to count post words and display it. you made It 100% easier ..keep it up celeb christmas

  28. Posted December 23, 2009 at 3:08 pm | Permalink

    Thanks for this great compilation! Helped me a lot!

  29. Posted December 23, 2009 at 3:34 pm | Permalink

    Great hacks, thank you!

  30. Posted December 24, 2009 at 5:17 pm | Permalink

    Excellent article, a few interesting tips that I am going to employ on my blog very shortly. I wanted to thank you guys for delivering quality content, definitely a refreshing way to blog( Fewer posts, but very high quality posts). Keep up the great work, Cats :)

  31. Posted December 24, 2009 at 7:36 pm | Permalink

    The “Rewrite author name with custom field” is a nice one. Easy and clean and very welcome.

    Keep it up!

  32. Posted December 24, 2009 at 7:37 pm | Permalink

    nice collection.. thanks.. can u tell me how to get defualt first picture in arras theme of wordpress..

  33. Posted December 26, 2009 at 12:06 am | Permalink

    Wow, I didn’t even know Wordpress had so many hacks =) I think I may try a few of those out, and go look for some more. There are probably a lot of things I could do to my blog without knowing it.

    Kris Roxas

  34. Posted December 26, 2009 at 9:13 pm | Permalink

    i’m impressed very great share you have
    thanks a lot

  35. Posted December 27, 2009 at 6:01 pm | Permalink

    Awesome compilation of tips!!!

    Thanks

  36. Posted December 28, 2009 at 3:25 am | Permalink

    Very nice list. I don’t see the value of showing the word count in your posts. Can you give an example site that uses that feature?

  37. Posted December 28, 2009 at 11:47 am | Permalink

    nice collection and beautiful round up.. thanks for the collection..

  38. Posted December 28, 2009 at 12:50 pm | Permalink

    I really like the snippet which highlights the keyword in the search result and the one for replacing the author field with the value in the_author! Thanks :)

  39. Posted December 30, 2009 at 1:05 am | Permalink

    Great post and great hacks thanks for sharing.

  40. Posted December 30, 2009 at 5:38 pm | Permalink

    Excellent hacks, I like everyone.

  41. Posted December 31, 2009 at 8:23 am | Permalink

    nice list. thanks!

  42. Posted January 5, 2010 at 4:28 am | Permalink

    Awesome list, thanks.. I will implement mobile visitors and enhanced search on my blog.. Thanks Jean…

  43. Posted January 5, 2010 at 12:12 pm | Permalink

    I like the collection. Thanks for sharing this kind of different stuff.

  44. Posted January 6, 2010 at 11:59 pm | Permalink

    Great hacks, thx for posting :-)

  45. Philippe Dionne
    Posted January 7, 2010 at 2:10 am | Permalink

    Great post!

    I think a web designer should always try to use hacks instead of plugins, so the theme comes packaged.

  46. Posted January 18, 2010 at 9:07 am | Permalink

    Nice collection and beautiful round up.. thanks for the collection..

  47. Posted January 18, 2010 at 9:08 am | Permalink

    Nice collection and beautiful round up…Thanks for sharing this kind of different stuff.

  48. Posted February 1, 2010 at 9:11 pm | Permalink

    These are pretty useful! Thanks for the list!

  49. Posted February 6, 2010 at 10:46 am | Permalink

    Excellent collection of all the wordpress hacks. love too see more hacks in future.

  50. Posted February 12, 2010 at 9:29 pm | Permalink

    Awesome tips as usual. I had a question as to your detecting browsers tip, why exactly? With things like Google Analytics and Statpress, why would this be necessary?

  51. Posted March 4, 2010 at 7:03 am | Permalink

    Hi Jean-Baptiste,

    The hack ‘Get the first image from the post and display it’ doesn’t always have to work on all off the wordpress-themes. As it didn’t work on the gonzoblog.nl …

    But I figured out what the problem is. In some themes this hack just shows the URL of your 1st image and not the actual picture! Change the code for your (home)page to:
    <img src="<?php echo catch_that_image() ?>" alt="" class="th" />

    Now you’ve got a picture there! In the gonzoblog my first picture is a divider/spacer for the first paragraph, so I had another problem. The picture I wanted to show is always the second picture, I sorted that one out too!

    Replace this line, and your second picture will turn up on your homepage:
    $first_img = $matches [1] [1]; //Setting to show 2nd picture, first picture: [1] [0]

    So, for all you people who also saw just the URL in your home-page, … this is it!
    LOL ;-P

14 Trackbacks

  1. By uberVU - social comments on December 22, 2009 at 5:12 pm

    Social comments and analytics for this post…

    This post was mentioned on Twitter by catswhocode: RT @tweetmeme Top WordPress hacks of 2009 http://bit.ly/7XoTPv…

  2. By Top WordPress hacks of 2009 | WordPress News on December 22, 2009 at 5:59 pm

    [...] Continue reading here: Top WordPress hacks of 2009 [...]

  3. By Top WordPress hacks of 2009 | Webs Developer on December 22, 2009 at 6:00 pm

    [...] Top WordPress hacks of 2009 Share and Enjoy: [...]

  4. By === popurls.com === popular today on December 22, 2009 at 11:00 pm

    [...] 26 Geeks Before They Were StarsdeliciousRuby & WebSockets: TCP for the Browser – igvita.comTop WordPress hacks of 2009Free Technology for Teachers: 12 TED Talks for Teachers to Watch Before 2010Design Something Every [...]

  5. [...] the CatsWhoCode.com blog there’s their list of top “hacks” for WordPress that can add that little extra boost your site might be needing. 2009 has been a [...]

  6. By Top WordPress hacks of 2009 | Lively Web Tuts on December 23, 2009 at 10:41 am

    [...] Direct Link [...]

  7. By Daily Digest for December 23rd « Alex Bilbie on December 24, 2009 at 1:18 am

    [...] Top WordPress hacks of 2009. — 3h ago via [...]

  8. By L’hebdo WordPress | WordPress Francophone on December 29, 2009 at 8:57 am

    [...] Un hack WordPress est une modification directe du code du thème ou du coeur de WordPress directement sans passer par une extension. Jean-Baptiste Jung, auteur de Cats Who Code propose un top 10 des hacks 2009. [...]

  9. By Best Of The Best In 2009 | Creative Magazine on December 31, 2009 at 12:27 am

    [...] Top Wordpress Hack Of 2009 [...]

  10. [...] Top WordPress hacks of 2009  Aucun tag pour cet article. 0 commentaire Classé dans Delicious   Écrit par Gilles [...]

  11. [...] Top WordPress hacks of 2009 [...]

  12. [...] Cats Who Code [...]

  13. By Serious Monday Roundup #23 « TOURISM MARKET on February 17, 2010 at 2:09 pm

    [...] Top WordPress hacks of 2009 – 2009 has been a very prolific year for WordPress hacks. In this article, I’ll show you the most useful hacks I came across during the whole year. [...]

  14. [...] / Tutorials WEB : Top WordPress hacks of 2009 – Cats Who Code Most Popular Firefox Extensions and Themes of 2009 – Lifehacker Most [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe without commenting

  • Smashing Network
  • Hosted by VPS.net and Akamai CDN
WordPress Appliance - Powered by TurnKey Linux