Quantcast

Essential Wordpress Hacks to BOOST your Blog

Fri, Jul 10, 2009

Development, Tools, Tutorials, Wordpress

DiggThis
Delicious

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.

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.

<span><span><?php recent_comments(); ?> </span></span>

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.

Related posts

  1. Build an Awesome Community News Feature for Your Wordpress Blog In this article tripwire magazine provides a detailed tutorial...
  2. 8 Essential Wordpress Plugins Improving Management of Multi Author Blogs Most blogs is started by individuals working hard to...
  3. Most Essential Tips for Improving Response Time of Your Wordpress Blog Wordpress is one of the most popular blogging and...
  4. Howto: Wordpress 2.7+ Most Popular Posts Widget In this article tripwire magazine provides a mini-tutorial on how...
  5. Howto: Wordpress 2.7+ User Link Feed Submit Form and Widget In this article tripwire magazine provides a mini-tutorial on...

Please subscribe to tripwire magazines rss feed

Please remember to share this post!

DiggThis
Delicious

3 Comments For This Post

  1. favSHARE Says:

    This article has been shared on favSHARE.net.

  2. Hezi Says:

    thank you!

  3. Thomas Craig Consulting Says:

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

16 Trackbacks For This Post

  1. » Essential Wordpress Hacks to BOOST your Blog | tripwire magazine Wordpress Plugins: Just another WordPress weblog Says:

    [...] See the original post here: Essential Wordpress Hacks to BOOST your Blog | tripwire magazine [...]

  2. Essential Wordpress Hacks to BOOST your Blog | tripwire magazine | bllogger Says:

    [...] more here: Essential Wordpress Hacks to BOOST your Blog | tripwire magazine internet [...]

  3. Essential Wordpress Hacks to BOOST your Blog | tripwire magazine Says:

    [...] here: Essential Wordpress Hacks to BOOST your Blog | tripwire magazine Tags: Comments0 Leave a Reply Click here to cancel [...]

  4. MacDaddy Links of the Week - 7/11/09 | bkmacdaddy designs Says:

    [...] Design articles from which I learned new, better or alternative ways to do things 150 Worth Knowing Web Developer Tools and Techniques 27 Must-Have Starter Kits For Web Designers 100 Amazing Design Blogs To Subscribe To Free Website Style Guide Photoshop Template 12 Essential Wordpress Plugins And Why They’re Useful HTML 5 and CSS 3: The Techniques You’ll Soon Be Using Recommended Books for your User Experience and Usability Library Create letterpressed type using only CSS Four Methods to Create Equal Height Columns Essential Wordpress Hacks to BOOST your Blog [...]

  5. Essential Wordpress Hacks to BOOST your Blog | tripwire magazine | Neorack Script Says:

    [...] post:  Essential Wordpress Hacks to BOOST your Blog | tripwire magazine Share and [...]

  6. Essential Wordpress Hacks to BOOST your Blog | tripwire magazine – wpden Says:

    [...] See the rest here: Essential Wordpress Hacks to BOOST your Blog | tripwire magazine [...]

  7. Essential Wordpress Hacks to BOOST your Blog | tripwire magazine Says:

    [...] Original post: Essential Wordpress Hacks to BOOST your Blog | tripwire magazine [...]

  8. Essential Wordpress Hacks to BOOST your Blog - Programming Blog Says:

    [...] DIRECT LINK » [...]

  9. The week in links 14/08/09 - Craig Baldwin's Blog Says:

    [...] Essential WordPress Hacks To Boost Your Blog (tripwiremagazine.com) [...]

  10. Twitter Trackbacks for Essential Wordpress Hacks to BOOST your Blog | tripwire magazine [tripwiremagazine.com] on Topsy.com Says:

    [...] link is being shared on Twitter right now. @bkmacdaddy, an influential author, said Essential #Wordpress [...]

  11. uberVU - social comments Says:

    Social comments and analytics for this post…

    This post was mentioned on Twitter by mrfidalgo: http://a2a.me/ZE3 via @AddToAny Essential Wordpress Hacks to BOOST your Blog…

  12. links for 2009-11-25 « Free Open Source Directory Says:

    [...] Essential WordPress Hacks to BOOST your Blog | tripwire magazine 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. (tags: Essential WordPress Hacks to BOOST your Blog | tripwire magazine) [...]

  13. Essential Wordpress Hacks to BOOST your Blog | tripwire magazine Says:

    [...] Essential Wordpress Hacks to BOOST your Blog | tripwire magazine December 8th, 2009 Twitter Trackbacks for Essential Wordpress Hacks to BOOST your Blog | tripwire magazine [tripwiremagazine.com] on Topsy.com Says: August 28th, 2009 at 1:10 am. [...] link is being shared on Twitter right now. @bkmacdaddy, an influential author, … Latest Posts. 90+ Top Notch Free Wordpress Themes · 60+ Fresh News for Web Designers and Developers · Build an Awesome Community News Feature for Your Wordpress Blog · 40+ Awesome News for Web Designers and Developers … [...] [...]

  14. Essential Wordpress Hacks to BOOST your Blog | tripwire magazine Says:

    [...] Help Files and Cheats. Very large compilation of tricks and hacks that you cant miss. … [...] Uni Ego / Essential Wordpress Hacks to BOOST your Blog | tripwire [...]

  15. Tweets that mention Essential Wordpress Hacks to BOOST your Blog | tripwire magazine -- Topsy.com Says:

    [...] This post was mentioned on Twitter by Ruhani Rabin and mlomb, Kirsten Mitchell. Kirsten Mitchell said: RT @ruhanirabin: Essential #Wordpress Hacks to BOOST your Blog http://j.mp/8ixFMn [...]

  16. uberVU - social comments Says:

    Social comments and analytics for this post…

    This post was mentioned on Twitter by ruhanirabin: Essential #Wordpress Hacks to BOOST your Blog http://j.mp/8ixFMn...

Leave a Reply