Essential WordPress Hacks to BOOST your Blog

January 30, 2011 · 24 comments

by Lars

wordpress-codehacks

WordPress needs no introduction. It is THE blogging platform these days and usually known as the synonym for blogging. It’s a fact that WordPress is getting more and more popular and that tons of blogs are powered by it. The community around WordPress is excellent providing free themes and plugings in nearly any form ans shape to meet your specific needs. Still there are must have stuff that we don’t have simple out of the box solutions for except for using so called WordPress Hacks. (not sure Hack is the right word always but it’s what people call it.). WordPress is not just a blogging tool, its a platform and framework allowing it’s users to jump deep into the blogging engine and use hooks and APIs to create unique designs and functionality. The WordPress Codex is by far the best place to learn about WordPress and its tweaks but it is aimed at developers knowing PHP, SQL, Apache, etc. The WordPress documentation / Codex is excellent but it is not really a place for most bloggers as they will have a hard time turning API specifications into functional solutions on their blogs. This article is an attempt to offer the majority of WordPress users an supplement to Codex, plugins by providing really nice to have WordPress Hacks that anyone can use. Finally this article provides a list of the best resources ever to find hacks for WP.


ElegantThemes
ThemeForest

Advertisement

Flexible Recent posts widget

Most hits on a blog goes directly to post pages via referring sites, search engines, rss readers etc. It is a very good idea to showcase resent and popular blog posts or articles to increase the number of pages users visit. Making this available in the sidebar in a attractive way may significantly increase internal clicks and then your overall trafic.

To create the widget illustrated to the right you need functionality to pull out the most resent articles from the database and turn this into html. It then needs to be integrated into the sidebar. This can be done in many ways fx. updating the template directly or by using plugins like PHP Code Widget. PHP Code Widget is a great tool to avoid changing the templates. It basically allows you to inject code into the sidebar from the WordPress administration tool like this. The widget system lets you move it around as you like afterwards.

Below the interesting parts will be briefly explained. I know there are other and simpler ways to have a most resent posts widget but this one give you 100% control and flexibility to make it look as you prefer. As in this example where images are loaded automatically and put in just before the post title.

This is the SQL query that pull out the most recent posts from the database. I actually could be more simple but I have decided to show you how to join the database tables needed to exclude specific categories in the list. In the case below I have excluded category id 41 taking place in the WHERE clause. (WHERE not wp_term_taxonomy.term_id IN (41) AND…)

If you want to change the number of posts returned you need to change the 4 at the very last part of the SQL query juat after the LIMIT statement. DISTINCT is used to avoid multiple entries of the same post to be returned – ex. if a post belongs to more than one category.

SELECT DISTINCT comment_count,ID,post_title
  FROM wp_posts AS wpost
  INNER JOIN wp_term_relationships ON wpost.ID = wp_term_relationships.object_id
  INNER JOIN wp_term_taxonomy ON wp_term_relationships.term_taxonomy_id =
    wp_term_taxonomy.term_taxonomy_id AND wp_term_taxonomy.taxonomy = 'category'
  WHERE not wp_term_taxonomy.term_id IN (41)  AND post_status = 'publish'
  ORDER BY `post_date` DESC LIMIT 0 , 4

The html that presents the data pulled out from the database is created by looping through the following code. The “foreach” statement have been left out here. You should be aware of the #image-url# as you need to change it to the location of your images. In order to generate the images dynamically you need to name them <post-id>.png (<post-id> being the WordPress id of the post ex. 1532.png)

<li><a href="<?php echo get_permalink($postid); ?>" title="<?php echo $title ?>">
<img src="#image-url#/<?php echo $postid; ?>.png" alt="" width="40" height="40" /></a>
<a href="<?php echo get_permalink($postid); ?>" title="<?php echo $title ?>">
<?php echo $title ?></a></li>

The full code can be found here.

Popular posts widget

The technique used with the previous hack can also be used to show a list of the most popular posts on your site. You can choose between different ways to determine popularity but in this case it is the number of comments that is used. The more comments the more popular a post is rated. The main change is in the SQL code. You can change the number of posts returned in the same way as in the previous hack. Use PHP Code Widget to get it into the sidebar.

SELECT comment_count,ID,post_title
  FROM $wpdb->posts
  ORDER BY comment_count DESC LIMIT 0 , 8");

The full code can be found here.

How to Display Related Posts

Another great way to get more hits on your blog is to have a “Related Posts” list fx. below the post. This may catch some readers attention and make them click a few more times. Adding this script requires that you can find The Loop in the PHP page generating the posts on your blog. It is normally found in the single.php file located in the root of your theme folder /wp-content/themes/<theme name>. The loop is used by WordPress to display each of your posts. In single.php there is one post only. Using The Loop, WordPress processes each of the posts to be displayed on the current page and formats them according to how they match specified criteria within The Loop tags. This is how it looks

//Loop starts here
<?php while (have_posts()) : the_post(); ?>
... //place the code in here.
//Loop ends here
<?php endwhile; ?>

Related posts can be generated in several different ways. One way to do it is to compare tags to see if posts are placed in the same tag archive. This technique is used here. If you want to show more than 4 related posts you just have to find “showposts=>4” and change the number. Be aware that it will only return 4 posts if there are 4 matching posts.

First lets get the tags for the current post and put them into an array. Then check if we found any, If we did we will print out the text Related Posts. Then we get hold on the first tag for the post and then builds up a new array called @args.

$tags = wp_get_post_tags($post->ID);
if ($tags) {
 echo 'Related Posts';
 $first_tag = $tags[0]->term_id;
 $args=array(
 'tag__in' => array($first_tag),
 'post__not_in' => array($post->ID),
 'showposts'=>4, 'caller_get_posts'=>1
 );

Having prepared all the arguments we are ready to pull the posts from the WordPress database by using WP_Query supplying the @args array just created. Having done that we simply run through all returned posts and write out the html.

 $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="Permalink to <?php the_title_attribute(); ?>">
 <?php the_title(); ?></a></p>
 <?php endwhile;
 }

The full code can be found here.

Showing upcomming posts may be a way to get visitors attention. It is not a feature that I have seen used a lot but fx. if you’re planning a contest it may be good to give regular users a heads up. Lets see how it can be created. I would suggest again that you use PHP Code Widget to inject it into the sidebar.

<?php query_posts('showposts=10&post_status=future'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
  <p><?php the_title(); ?> <?php the_time('j. F Y'); ?></p>
<?php endwhile; else: ?><p>No future posts.</p><?php endif; ?>

The full code can be found here.

Display Recent Comments

This hack will show you how to create a function that will allow you to add a list of recent comments anywhere on the site. Functions are placed in the functions.php file normally in the root of your theme folder.
The part that goes into function.php. Create it if it is not present already. First we are defining a function called recent_comments. It takes four parameters and you shoud fist of all be interested in the two first. These specify number of comments shown and lenght before they are truncated.

function recent_comments($src_count=6, $src_length=50, $pre_HTML='<ul>', $post_HTML='') {

The SQL query used to pull out the comments looks like this. It’s pulling lates comments from the “comments” table that have status approved. The truncation to – in this case 50 characters is implemented using the SUBSTRING function. To get post title out as well the posts table is joined in. Won’t go into details on that here

SELECT DISTINCT ID, post_title, post_password, comment_ID, comment_post_ID,
  comment_author, comment_date_gmt, comment_approved, comment_type,
  SUBSTRING(comment_content,1,$src_length) AS com_excerpt FROM $wpdb->comments
  LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID)
  WHERE comment_approved = '1' AND comment_type = '' AND post_password = ''
  ORDER BY comment_date_gmt DESC
  LIMIT $src_count

Finaly the SQL is executed and the result is being iterated to write the html out.

$comments = $wpdb->get_results($sql);
$output = $pre_HTML;
foreach ($comments as $comment) {
$output .= "<li><a href=\"" . get_permalink($comment->ID) . "#comment-" . $comment->comment_ID . "\" title=\"on " . $comment->post_title . "\">" . strip_tags($comment->com_excerpt) ."...</a></li>";
}

Getting the recent comments onto your blog is simple. Just add the following code fx. in a PHP Code Widget. This will call the function we created above in functions.php.

<?php recent_comments(); ?> 

The full code can be found here.

Display Word Count Of The Post

Open single.php located in your theme root folder and paste the following code where you want to display the word count.

<?php function count_words($str){
 $words = 0;
 $str = eregi_replace(" +", " ", $str);
 $array = explode(" ", $str);
 for($i=0;$i < count($array);$i++)
 {
 if (eregi("[0-9A-Za-zÀ-ÖØ-öø-ÿ]", $array[$i]))
 $words++;
 }
 return $words;
 }?>

 Word count: <?php echo count_words($post->post_content); ?>
}

Great Resources with WP Hacks

wprecipes

Great site with loads of tricks and awesome hacks for WordPress

20-wordpress-recipes-codes

This post have 20 hand picked nice to know hacks.

wphacks

This site has a huge collection of hacks for WP. They are nicely categorised to make it faster for browse.

40 most wanted wordpress tricks and hacks

This is a must have post with 40 essential hacks.

63 Essential WordPress Hacks, Tutorials, Help Files and Cheats

Very large compilation of tricks and hacks that you cant miss.

10 WordPress Hacks to Make your Life even Easier

This is a post created to make our lives easier. Does it? Well maybe! The trick and hacks in there are great.

Yearning for BCP-221 exam help? Download our 640-461 dumps and 640-461 demos to pass your real exam in a hassle free way.

{ 7 comments… read them below or add one }

Donna Wilson November 28, 2012 at 11:18 pm

I really appreciate you sharing these great tips!!! I need to update my blog…gonna try these!

Reply

Paul September 23, 2011 at 11:05 pm

Great tutorials I’ll have to try some of these.

Reply

Dave November 15, 2010 at 5:07 am

Great job on explaining how to further tweak these common widgets to make then exactly what you need them to be. Great work

Reply

juan webdesignshock August 24, 2010 at 2:35 am

Hello, thanks for these tips, related posts ones it’s gonna be implemented soon in my blog !!
have a great day !

Reply

Thomas Craig Consulting February 20, 2010 at 12:36 pm

Great resources for a Wordpress newbie like myself, best think about Wordpress is the community involvement and abundance of developer resources. Thanks again.

Reply

Hezi July 11, 2009 at 12:28 pm

thank you!

Reply

favSHARE July 11, 2009 at 11:21 am

This article has been shared on favSHARE.net.

Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

{ 17 trackbacks }

Previous post:

Next post:


Web Analytics