8 most important tricks for WordPress developers
0WordPress trick to display Adsense on any page Easily
For this trick to be successful you require a self hosted wordpress and an adsense account of course.
Insert the below code into function.php file.
All or majority of the themes come with a function.php file.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
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 done save the file and with that you can use your adsense code where ever you wish it should display. To do that, simply paste the following code in your post (make sure you are in the HTML mode)
|
1 |
[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.
<?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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
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='80px' height='80px' alt="wordpress trick for adding author bio" 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; } |
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.
|
1 2 3 4 5 6 |
<div> <div><?php</div> <div>ini_set('zlib.output_compression', 'On');</div> <div>ini_set('zlib.output_compression_level', '1?);</div> <div>?></div> </div> |
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?phpglobal $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!
|
1 2 3 4 5 |
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).
|
1 |
[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
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
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
|
1 2 |
$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
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
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->get_queried_object(); $id = $content_array->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 => $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 => $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
|
1 2 |
$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.