Useful Code Snippets for WordPress Development

Posted on March 4th, 2011

1.Automatically Resize Images
You can use this hack to automatically resize any image you’d like to whatever width and height you choose for a more organized look. To do this, copy the following script and create a folder for it anywhere on your site (i.e. scripts) and name it “timthumb.php“. Now, you can use the following syntax to add an automatically resized image to your blog post:

<img src="/scripts/timthumb.php?src=/images/whatever.jpg&amp;h=150&amp;w=150&amp;zc=1"
 alt="" />

2.Display the Most Commented Posts of 2009

As the New Year is about to role in, it would be fun and resourceful to let your readers know which of your posts were most popular in the past year. This hack will allow your visitors to view the top 10 most commented/popular posts of 2009. This is a great way to give your posts a second shot at being noticed. In order to do this, you’ll need to place the following code on your sidebar.php file, or wherever else you’d like on your theme:

<ul>
<?php
$result = $wpdb->get_results
("SELECT comment_count,ID,post_title, post_date FROM $wpdb->posts
WHERE post_date BETWEEN '2009-01-01' AND '2009-12-31'
ORDER BY comment_count DESC LIMIT 0 , 10");

foreach ($result as $topten) {
$postid = $topten->ID;
$title = $topten->post_title;
$commentcount = $topten->comment_count;
if ($commentcount != 0) {
?>
<li></li>
<?php }
}
?>
</ul>

3.Redirect Your WordPress Feed to FeedBurner
If you’ve found out how useful FeedBurner really is after you’ve set-up your WordPress site, and you have a few RSS subscribers on your default WordPress feed, then you’ll need to redirect your feed to FeedBurner. Every time a user follows a link to your default feed (i.e. http://www.yourblog.com/feed) they will be redirected to the location of your FeedBurner feed ( i.e. http://feeds.feedburner.com/yourblog). This way, even if a user manages to somehow subscribe to your old RSS feed, they will always be redirected to your new feed. Place the following code in your .htaccess file.

# temp redirect wordpress content feeds to feedburner
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} !FeedBurner    [NC]
RewriteCond %{HTTP_USER_AGENT} !FeedValidator [NC]
RewriteRule ^feed/?([_0-9a-z-]+)?/?$ http://feeds.feedburner.com/YourFeedName [R=302,NC,L]
</IfModule>

4. Custom “Read More” Links for Your Posts
This is a really useful hack if you want to better define or customize the look of your “Read More” links for posts. The first thing you have to do is to edit your posts and create custom fields. Give them custom_more as a key, and whatever text you want to be displayed as the value. Then you have to edit your index.php file (and also your category.php, search.php, etc) and find a line similar to this:
the_content(“Read more”);

Now just replace it with this code:

<?php $custommore = get_post_meta($post->ID, 'custom_more', true); ?>
<?php if (!$custommore) { $custommore = 'Read More &raquo;'; } ?>
<?php the_content($custommore); ?>

5. Post Thumbnail Images new feature of wordpress 2.9
With WordPress 2.9, theme authors can easily enable Post Thumbnail selection UI and call those image using simple template tags.
First, in the theme’s functions.php, declare that your theme supports this feature. This will enable the UI in the WP Admin.
add_theme_support( ‘post-thumbnails’ );
Next, you should specify the dimensions of your post thumbnails. You have two options here: box-resizing and hard-cropping. Box resizing shrinks an image proportionally (that is, without distorting it), until it fits inside the “box” you’ve specified with your width and height parameters. For example, a 100×50 image in a 50×50 box would be resized to 50×25. The benefit here is that the entire image shows. The downside is that the image produced isn’t always the same size. Sometimes it will be width-limited, and sometimes it will be height-limited. If you’d like to limit images to a certain width, but don’t care how tall they are, you can specify your width and then specify a height of 9999 or something ridiculously large that will never be hit.
set_post_thumbnail_size( 50, 50 ); // 50 pixels wide by 50 pixels tall, box resize mode

Now, you can make use of the template functions to display these images in the theme. These functions should be used in the loop.
has_post_thumbnail() returns true/false and indicates whether the current post has a manually-chosen Post Thumbnail (in the loop):
the_post_thumbnail() outputs the Post Thumbnail, if it exists (in the loop):
What if you want to use a small 50×50 hard-cropped image for the home page, but want to use a 150 pixel-wide and 150 height image on the single post page. You can specify additional custom sizes! Here’s the code:
add_image_size( ‘single-post-thumbnail’, 150, 150 );

then call the Function
<?php the_post_thumbnail( ‘single-post-thumbnail’ ); ?>