Top WordPress Hacks 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. 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.

view source

print?

01.function is_old_post($post_id=null){

02. $days = 15;

03. global $wp_query;

04. if(is_single() || is_page()) {

05. if(!$post_id) {

06. $post_id = $wp_query->post->ID;

07. }

08. $current_date = time();

09. $offset = $days *60*60*24;

10. $post_id = get_post($post_id);

11. $post_date = mysql2date('U',$post_id->post_date);

12. $cunning_math = $post_date + $offset;

13. $test = $current_date - $cunning_math;

14. if($test > 0){

15. $return = true;

16. }else{

17. $return = false;

18. }

19. }else{

20. $return = false;

21. }

22. return $return;

23.}

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:

view source

print?

1.<?php if(is_old_post()){ ?>

2.INSERT AD CODE HERE

3.<?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:

view source

print?

1.function wcount(){

2. ob_start();

3. the_content();

4. $content = ob_get_clean();

5. return sizeof(explode(" ", $content));

6.}

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

view source

print?

1.<?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.

view source

print?

01.<?php

02.add_filter('body_class','browser_body_class');

03.function browser_body_class($classes) {

04. global $is_lynx, $is_gecko, $is_IE, $is_opera, $is_NS4, $is_safari, $is_chrome, $is_iphone;

05.

06. if($is_lynx) $classes[] = 'lynx';

07. elseif($is_gecko) $classes[] = 'gecko';

08. elseif($is_opera) $classes[] = 'opera';

09. elseif($is_NS4) $classes[] = 'ns4';

10. elseif($is_safari) $classes[] = 'safari';

11. elseif($is_chrome) $classes[] = 'chrome';

12. elseif($is_IE) $classes[] = 'ie';

13. else $classes[] = 'unknown';

14.

15. if($is_iphone) $classes[] = 'iphone';

16. return $classes;

17.}

18.?>

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

view source

print?

1.<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:

view source

print?

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

It will output a url similar to:

view source

print?

1.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.

view source

print?

01.function catch_that_image() {

02. global $post, $posts;

03. $first_img = '';

04. ob_start();

05. ob_end_clean();

06. $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);

07. $first_img = $matches [1] [0];

08.

09. if(empty($first_img)){ //Defines a default image

10. $first_img = "/images/default.jpg";

11. }

12. return $first_img;

13.}

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

view source

print?

1.<?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:

view source

print?

1.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:

view source

print?

1.<?php

2. $title = get_the_title();

3. $keys= explode(" ",$s);

4. $title = preg_replace('/('.implode('|', $keys) .')/iu',

5. '<strong class="search-excerpt">\0</strong>',

6. $title);

7.?>

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

view source

print?

1.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.

view source

print?

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

02. $title = htmlentities($title,ENT_NOQUOTES,$encoding);

03. $keywords = htmlentities($keywords,ENT_NOQUOTES,$encoding);

04.

05. $content = array(

06. 'title'=>$title,

07. 'description'=>$body,

08. 'mt_allow_comments'=>0,  // 1 to allow comments

09. 'mt_allow_pings'=>0,  // 1 to allow trackbacks

10. 'post_type'=>'post',

11. 'mt_keywords'=>$keywords,

12. 'categories'=>array($category)

13. );

14. $params = array(0,$username,$password,$content,true);

15. $request = xmlrpc_encode_request('metaWeblog.newPost',$params);

16. $ch = curl_init();

17. curl_setopt($ch, CURLOPT_POSTFIELDS, $request);

18. curl_setopt($ch, CURLOPT_URL, $rpcurl);

19. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

20. curl_setopt($ch, CURLOPT_TIMEOUT, 1);

21. $results = curl_exec($ch);

22. curl_close($ch);

23. return $results;

24.?>

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.

view source

print?

1.<?php $author = get_post_meta($post->ID, "guest-author", true);

2.if ($author != "") {

3. echo $author;

4.} else {

5. the_author();

6.}  ?>

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.

view source

print?

1.include('mobile_device_detect.php');

2.$mobile = mobile_device_detect();

3.

4.if ($mobile==true) {

5. header( 'Location: http://your-website.com/?theme=Your_Mobile_Theme' ) ;

6.}

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

Deixe uma Resposta

Preencha os seus detalhes abaixo ou clique num ícone para iniciar sessão:

Logótipo da WordPress.com

Está a comentar usando a sua conta WordPress.com Terminar Sessão /  Alterar )

Google photo

Está a comentar usando a sua conta Google Terminar Sessão /  Alterar )

Imagem do Twitter

Está a comentar usando a sua conta Twitter Terminar Sessão /  Alterar )

Facebook photo

Está a comentar usando a sua conta Facebook Terminar Sessão /  Alterar )

Connecting to %s