Top 10 WordPress Hacks from October 2009

by Jean. 41 Comments -

I know most of your loves to discover WordPress hacks, and this is why I’m pretty sure that you’ll definitely love this article. I have compiled the most useful and awesome WordPress hacks that were released past month. Enjoy!

How to: Show parent page title regardless of what subpage you are on

Let’s start with a nice code for those using WordPress as a CMS: Just paste it anywhere on your theme files and it will display the parent page title.

<?php
if($post->post_parent) {
    $parent_title = get_the_title($post->post_parent);
    echo $parent_title;
} else {
    wp_title('');
}
?>

» Source: http://www.wprecipes.com/how-to-show-parent-page-title-regardless-of-what-subpage-you-are-on


WordPress hack: Automatically insert author bio on each post

When you’re owning a multi-writers blog, it is important to show who wrote the post. In case of guest bloggers, it is also a nice way to give credit.
Simply insert the following lines of code into your functions.php file, and that’s all. Author bio will be automatically displayed after each post.

function get_author_bio ($content=''){
    global $post;

    $post_author_name=get_the_author_meta("display_name");
    $post_author_description=get_the_author_meta("description");
    $html="<div class='clearfix' id='about_author'>\n";
    $html.="<img width='80' height='80' class='avatar' src='http://www.gravatar.com/avatar.php?gravatar_id=".md5(get_the_author_email()). "&default=".urlencode($GLOBALS['defaultgravatar'])."&size=80&r=PG' alt='PG'/>\n";
    $html.="<div class='author_text'>\n";
    $html.="<h4>Author: <span>".$post_author_name."</span></h4>\n";
    $html.= $post_author_description."\n";
    $html.="</div>\n";
    $html.="<div class='clear'></div>\n";
    $content .= $html;
    }

    return $content;
}

add_filter('the_content', 'get_author_bio');

» Source: http://aext.net/2009/10/wordpress-hack-anything-can-be-added-anywhere-in-the-post-content/


Compress WordPress output and speed your blog’s load speed

Is your host slow? Althought on WpWebHost we don’t have that kind of problems, if you’re hosted elsewhere you’ll probably enjoy this tip who can reduce your blog load speed.

After you made sure that the zlib php extension is enabled by your hosting provider, place the following code in your header (above the DOCTYPE), save the file and your blog will load faster.

<?php
ini_set('zlib.output_compression', 'On');
ini_set('zlib.output_compression_level', '1');
?>

» Source: http://www.wprecipes.com/compress-wordpress-output-and-speed-your-blogs-load-speed


Display registered users comment count on your WordPress blog

If your blog is private or have lots of registered users, it may be interesting to be able to display the number of comments posted by registered users. Just paste this code and the count will be displayed where you pasted it.

<?php
global $wpdb;
$where = 'WHERE comment_approved = 1 AND user_id <> 0';
$comment_counts = (array) $wpdb->get_results("
		SELECT user_id, COUNT( * ) AS total
		FROM {$wpdb->comments}
		{$where}
		GROUP BY user_id
	", object);
foreach ( $comment_counts as $count ) {
  $user = get_userdata($count->user_id);
  echo 'User ' . $user->display_name . ' comment count is ' . $count->total . '
';
}
?>

» Source: http://www.wprecipes.com/display-registered-users-comment-count-on-your-wordpress-blog


Automatically resize pictures on your WordPress blog

You know it, a picture is worth a thousand words. But pictures means that you have to resize it, which is alwyas boring.
Happilly, a very cool script called TimThumb can resize pictures for you. The function below create a WordPress shortcode that will make Timthumb use even easier.

function imageresizer( $atts, $content = null ) {
	return '<img src="/timthumb/timthumb.php?src='.$content.'&w=590" alt="" />';
}

add_shortcode('img', 'imageresizer');

Then, you can use the following syntax to add an automatically resized image to your blog post:

[img]http://www.yoursite.com/yourimage.jpg[/img]

» Source: http://www.wprecipes.com/automatically-resize-pictures-on-your-wordpress-blog


WordPress tip: Create a Tweetmeme “Retweeet” shortcode

Twitter is one of the best way to get quality traffic to your blog. In order to help people sharing your articles on Twitter, you should definitely implement a Tweetmeme button, which display how many time time people RT’d your blog posts.
Just paste the function below into your functions.php file.

function tweetmeme(){
	return '<div class="tweetmeme"><script type="text/javascript" src="http://tweetmeme.com/i/scripts/button.js"></script></div>';
}
add_shortcode('tweet', 'tweetmeme');

Once you saved the file, you’ll be able to display the Tweetmeme “retweet” button anywhere on your posts. In WordPress editor, make sure you are in HTML mode and insert the following:

[tweet]

When your post will be published, the shortcode will be replaced by the TweetMeme button.
For a “Live Demo” of this tip, just take a look at my new blog Cats Who Blog, dedicated to blogging/make money online tips.
» Source: http://www.wprecipes.com/wordpress-tip-create-a-tweetmeme-retweeet-shortcode


WordPress trick: function to get tags related to category

Do you ever wanted to be able to get tags related to one (or more) specific category? If yes, I’m pretty sure you’ll be delighted with this very cool tip.
First, here is the function you have to paste in your functions.php file:

function get_category_tags($args) {
	global $wpdb;
	$tags = $wpdb->get_results
	("
		SELECT DISTINCT terms2.term_id as tag_id, terms2.name as tag_name, null as tag_link
		FROM
			wp_posts as p1
			LEFT JOIN wp_term_relationships as r1 ON p1.ID = r1.object_ID
			LEFT JOIN wp_term_taxonomy as t1 ON r1.term_taxonomy_id = t1.term_taxonomy_id
			LEFT JOIN wp_terms as terms1 ON t1.term_id = terms1.term_id,

			wp_posts as p2
			LEFT JOIN wp_term_relationships as r2 ON p2.ID = r2.object_ID
			LEFT JOIN wp_term_taxonomy as t2 ON r2.term_taxonomy_id = t2.term_taxonomy_id
			LEFT JOIN wp_terms as terms2 ON t2.term_id = terms2.term_id
		WHERE
			t1.taxonomy = 'category' AND p1.post_status = 'publish' AND terms1.term_id IN (".$args['categories'].") AND
			t2.taxonomy = 'post_tag' AND p2.post_status = 'publish'
			AND p1.ID = p2.ID
		ORDER by tag_name
	");
	$count = 0;
	foreach ($tags as $tag) {
		$tags[$count]->tag_link = get_tag_link($tag->tag_id);
		$count++;
	}
	return $tags;
}

Once you have pasted the function, you can use it in your theme:

$args = array('categories' => '12,13,14');
$tags = get_category_tags($args);

» Source: http://www.wprecipes.com/wordpress-trick-function-to-get-tags-related-to-category


WordPress tip: Get all custom fields from a page or a post

Do you ever wanted to be able to get all custom fields from a specific post or page? If yes, let’s start by pasting the following code in your functions.php file:

function all_my_customs($id = 0){
    //if we want to run this function on a page of our choosing them the next section is skipped.
    //if not it grabs the ID of the current page and uses it from now on.
    if ($id == 0) :
        global $wp_query;
        $content_array = $wp_query-&gt;get_queried_object();
        $id = $content_array-&gt;ID;
    endif;   

    //knocks the first 3 elements off the array as they are WP entries and i dont want them.
    $first_array = array_slice(get_post_custom_keys($id), 3);

    //first loop puts everything into an array, but its badly composed
    foreach ($first_array as $key =&gt; $value) :
           $second_array[$value] =  get_post_meta($id, $value, FALSE);

            //so the second loop puts the data into a associative array
            foreach($second_array as $second_key =&gt; $second_value) :
                       $result[$second_key] = $second_value[0];
            endforeach;
     endforeach;

    //and returns the array.
    return $result;
}

Once done, you can use the function like this:

$result = all_my_customs();
echo $result['my_meta_key'];

» Source: http://www.wprecipes.com/wordpress-tip-get-all-custom-fields-from-a-page-or-a-post


Check if a plugin is active

If you want to check if a WordPress plugin is active, just use the is_plugin_active() function. The function takes a single parameter, which is the path to the plugin, as shown in the example below:

<?php
if (is_plugin_active('plugin-directory/plugin-file.php')) {
    //plugin is activated
}
?>

» Source: http://www.wprecipes.com/check-if-a-wordpress-plugin-is-active-the-easy-way


Create an Anti-IE6 plugin

With this amazing code created by Nathan Rice, you’ll be able to serve IE6 users the default WordPress theme. After all, those idiots don’t deserve anything better :D
Just paste the following in a new file and save it as ie6.php. Upload it to your wp-content/plugins directory and activate it on your WordPress dashboard.
By the way, if you hate IE6 just like I do, you should definitely check out this very funny article.

<?php
/*
Plugin Name: Serve Default to IE6
Plugin URI: http://www.nathanrice.net/blog/serve-ie6-visitors-the-default-wordpress-theme
Description: This plugin will serve the default theme to any visitors using IE6.
Author: Nathan Rice
Author URI: http://www.nathanrice.net/
Version: 1.0
*/

add_filter('template', 'serve_default_to_iesix');
add_filter('option_template', 'serve_default_to_iesix');
add_filter('option_stylesheet', 'serve_default_to_iesix');
function serve_default_to_iesix($theme) {
	if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6') !== false)
		$theme = 'default';

	return $theme;
}
?>

» Source: http://www.nathanrice.net/blog/serve-ie6-visitors-the-default-wordpress-theme/

  • http://www.thunderpaw.com David Lyle

    Nice tips!

    LOVE the anti IE6 ;)

  • http://www.wesbos.com Wes Bos

    Great post, Jean. Glad to see mine on the top :)

    I was playing around with the wordpress is_tree function the other day and I couldn’t figure out how to get it to work with an array of numbers. Maybe you could do a post on that?

    Keep it up!

  • http://www.joyoge.com/ joyoge designers’ bookmark

    nice hacks for wp, thanks for share..

  • http://blog.robsonb.com.br RobsonB

    Excellent tips to hack WP themes… certain I’ll use it soon! =D

  • Pingback: Top 10 WordPress Hacks from October 2009 | TopRoundups

  • http://collegedemocratsatcu.org Robby Hakim

    nice tutorial, i hope this is can increase my knowledge about wordpress

  • http://fwebde.com/ Eric B.

    Thanks for the useful tips!

  • http://www.395theblog.com Jake Easley

    Great tips. While the majority of these are built into my current theme, this is a great resource for changes or installing new themes.

  • Pingback: Top 10 WordPress Hacks from October 2009

  • http://cheapestprepaidcellphone.com Randy

    Great post with the codes for hacking. This will be awesome. I will try this one if it is really works.

  • http://www.w2point.com Web 2.0

    That’s a bookmark article :) Thanks!

  • http://largecatlitterboxes.com/ David

    Awesome. I’m sure this will be awesome.

  • http://coattreestand.com/ Rey

    Great post. I will try this.

  • http://aext.net Lam Nguyen

    Thanks for adding my post here :D

  • Pingback: 【今週のまとめ】 2009/11/2-11/6 « SPACE ☆ SPACE

  • http://www.arthritisjournal.org Arleen Anderson

    Great list of WP hacks! Webmasters will surely be interested in this discussion.

  • http://codenametomato.com Marsh

    thanks alot jean , it was really marvelous!

    Great work dude, keep it up…

  • http://www.bdo7.com/vb منتديات

    thanks alot jean

  • Pingback: Top 10 WordPress Hacks from October 2009 | Masricano.com

  • http://www.startcleaningservice.com/ Jason Barrett

    Thank you so much jean for this great tip.

  • http://australiagreyhoundracing.com Matt

    Whoa, thanks so much for pointing out the “author bio” tip… I’m just about to open up my blog to guest contributors, so this is perfect timing for me :)

  • http://bukmacher1x2.com.pl bukmacherski marco

    great post,Wp is the best ,for ever

  • http://www.noobtalk.com/ Noob Talk

    Thank you so much! I was looking a code for automatic insertation of author bio on each post! You Sir, are a life savior!

  • http://www.techhail.com Samrat P

    Nice post.. btw any idea on how to remove the anchor for dates from the comments section.

  • http://www.xbox360repairing.com Justin Bachelor

    Great WP tutorials! This can definitely help all those WP users out there to fix their specific WP problems.

  • Pingback: Recopilación de Wordpress hacks | El blog del diseñador Cookieface. Diseño, internet, actualidad y demas chucherias

  • http://vikas-gupta.in Vikas Gupta

    Great list! Will try some though I do not know CSS enough. I am soon shifting to self hosting.

  • http://www.reversephonedetectiveweb.co.cc Ramon

    Finally, a decent re-sizer. You have no idea what I have been through to find such a simple thing. Thanks a bunch!

  • Pingback: 10 meilleurs hacks Wordpress d’Octobre 2009

  • Jon

    Fantastic post, my company recently went through the process of updating the design of our WordPress blog. Quite an interesting process – but turned out great!

  • http://www.trendpointe.com tommy

    Thank you so much! I was looking a code for automatic insertation of author bio on each post! You Sir, are a life savior!

  • http://www.ghilliesuitsuperstore.com ghillie suit

    I am new to blogging and wordpress as well. This is undoubtedly the best WP hack I have found yet. i really don’t know how to thank you. Speeding up blog load time tips was great. Actually I tried few plugins but got a lot of errors. I hope thisone will do the trick.

  • http://videospescaria.blogspot.com Lucas

    Thanks for sharing. This blog is to be congratulated

  • http://www.thejanitorialsupply.com/ Duke Cullen

    thanks for this awesome overview..definitely am going to try out a couple of these tricks.

  • http://hjacob.com/ Hendrik Jacob

    Thanks for sharing – my favorite is the tagscategory snippet

  • http://munimkazia.com Munim

    Nice hacks.
    I like the Tweetmeme shortcode one.. cute! :)

  • http://aksesuarevi.com aksesuar

    thank you..nice wordpress hacks..

  • http://www.fotolusion.com Sravan

    “With this amazing code created by Nathan Rice, you’ll be able to serve IE6 users the default WordPress theme. After all, those idiots don’t deserve anything better :D”

    ROTFLMAO!

  • http://www.activr.com Hami

    What a great compilation. These are surely some of the too wordpress hacks of this year.. thought missed some of them but here you presented them again. Thanks.

  • Alan

    I tried to use the Create an Anti-IE6 plugin but got the following message: Incompatible archive PCLZIP_ERR_BAD_FORMAT (-10) : Unable to find End of Central Dir Record signature

    I used the code and uploaded a php file that I created as the file on the creators web site is a dead link. Any ideas?

  • http://www.zenerexreview.com Rob

    Ah, thanks for the load speed hack. I’ve been hearing how Google is penalizing sites that load slowly. Not sure if this is true, but a fast-loading site is a good thing no matter what.