Top 10 WordPress Hacks from October 2009

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.

if($post->post_parent) {
    $parent_title = get_the_title($post->post_parent);
    echo $parent_title;
} else {

» Source:

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;

    $html="<div class='clearfix' id='about_author'>\n";
    $html.="<img width='80' height='80' class='avatar' src='".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 class='clear'></div>\n";
    $content .= $html;

    return $content;

add_filter('the_content', 'get_author_bio');

» Source:

Compress WordPress output and speed your blog’s load speed

Is your host slow? Althought on HostGator 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.

ini_set('zlib.output_compression', 'On');
ini_set('zlib.output_compression_level', '1');

» Source:

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.

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}
		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:

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:


» Source:

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=""></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:


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:

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, as tag_name, null as tag_link
			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
			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);
	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:

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;

    //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];

    //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:

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:

if (is_plugin_active('plugin-directory/plugin-file.php')) {
    //plugin is activated

» Source:

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

Plugin Name: Serve Default to IE6
Plugin URI:
Description: This plugin will serve the default theme to any visitors using IE6.
Author: Nathan Rice
Author URI:
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: