Top 8 WordPress Tricks And Hacks

by Salman Ahsan · View Comments

in WordPress Tricks

Imagine if you knew how to code wordpress themes and designs then you wouldn’t have to spend massive amount of money on purchasing it. Usually, I work on the themes on most of my blogs. I enjoy tweaking all the time because it is very essential for your blog.

I am sure most of you would love to discover some cool wordpress hacks and I hope you enjoy this article. If you are not a pro then I would suggest that you try these on a new wordpress installation.

Wordpress Tricks and Hacks

How to display Adsense when you Only want to

To use this trick, you need a self hosted wordpress and an adsense account of course.
Insert the following code into your function.php file. All or majority of the themes come with a function.php file.

function showads() {

return ‘<script type=”text/javascript”><!–

google_ad_client = “pub-xxxxxxxxxxxxxxx”;

google_ad_slot = “4668915978″;

google_ad_width = 468;

google_ad_height = 60;

//–>

</script>

<script type=”text/javascript” src=”http://pagead2.googlesyndication.com/pagead/show_ads.js”>

</script>’;

}

Once you are done, you will be able to insert adsense whenever and wherever you want it in your post. To do that, simply paste the following code in your post (make sure you are in the HTML mode)

[adsense]

How to Show Parent Page Title regardless of what Subpage you are on

Now this is only for the people who are using wordpress as their content management system. Simply paste the code 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(”);
}
?>

<?phpif($post->post_parent) {    $parent_title = get_the_title($post->post_parent);    echo $parent_title;} else {    wp_title(”);}?>

How to Automatically insert Author Bio on each post

This is best used on a blog owned by multiple authors. If you write a post, you would definitely want to take the credit for it and thus display your bio on the post. If you have guest bloggers then this is a great method to give them credit for their contribution

Simply insert the following code into your functions.php file and it is good to go. 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’);

Speed up Your Blog’s Loading Speed

There are so many web hosting that it is difficult to find out which one is best for blogging. Your host might be slow and it can annoy your readers. Do you know that you can lose tons of readers if your blog lags or loads really slow.

You need zlib php extension enabled by your hosting provider. Once that is taken care of, simply place the following code in your header (above the DOCTYPE). Save the file and enjoy the speed.

<?php
ini_set(‘zlib.output_compression’, ‘On’);
ini_set(‘zlib.output_compression_level’, ‘1′);
?>

How to Display registered users comment count on your Blog

If your blog has a lot of registered users, then you may want to display the number of comments posted by them. Paste the code below wherever you want to display the count.

<?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 . ‘
‘;
}
?>

How to Create a Tweetmeme “Retweet” shortcode

Without any doubt, twitter is one of the best ways to get quality traffic to your blog. Of course, you can use several plugins to display this but if you want to do it the pro way then you might want to consider this. The code will display how many times your post has been retweeted.

Just paste the code below into your functions.php file and bingo!

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 the file has been saved, you can display the “retweet” button almost anywhere on your posts. When writing a post, simply insert the below code (make sure you are in html mode when inserting the code).

[tweet]


When you publish the post, the shortcode [tweet] will be replaced by the TweetMeme button.

How to get tags related to category

This is one of my favorite hack and I am sure you will love it too. This will allow you to get tags related to one or more specific category.

Paste the code 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 done that, use the below code in your theme

$args = array(‘categories’ => ‘12,13,14′);
$tags = get_category_tags($args);

How to Get all Custom fields from a Page or a Post

This code will allow you to get all the custom fields from a specific post or page. Paste the code below 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;

}


You can use the function with the following code

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



I hope you find this useful. Make sure you know your stuff well before applying all these. I have pretty good knowledge of photoshop as well as coding to some extent so if you need any assistance just reply through comments and i will try to get back to you.

Check Out Related Posts:

Related Posts with Thumbnails
  • S Ahsan,

    I'm curious as to where exactly you past the code in the functions.php file. I've seen this kind of thing around and have never had luck with it.

    I like the idea as guest posts would make this a handy thing to use.
  • Hey Eric,

    I will be working on several video tutorials where i will show how you can do that and similar other stuffs, step by step :), hope it will be helpful... stay tuned!
  • All these WP "tricks" are cool, but I'm kind of afraid to use too many since it'll gone once I re-install the template, or select a new one, am I wrong?
  • Well if you can setup a default template, code it and save it on you hard drive, you will have it everytime you setup this template. You are reinstalling wordpress but when you use a theme, you can keep the codes intact. :)
blog comments powered by Disqus

Previous post:

Next post: