Wordpress Hack: Anything Can Be Added Anywhere In The Post Content

Wordpress Hack: Anything Can Be Added Anywhere In The Post Content


Spread it!

  • Share

Some of wordpress plugins automatically add their display below your post content which is such as: YARPP (Related Posts) or Sociable …. They added their displays into your post content, but sometime, you want to add your own code before them, even between the introduction and the main content. This article will tell you how to add anything to your wordpress theme just right before others plugin do.

This is the first tutorial that I don’t offer the download link. Just follow these simple steps and you’ll get the nice results.

Hack your theme functions.php file


The only thing you want to do is just hack your theme functions.php file.

Open this file.

In this article, I will add the author bio box into wordpress theme before the Related Posts displays. Write a function that return the author bio box, you can write anywhere, but at the end of your functions.php is avoid messing up your file:

function get_author_bio ($content=''){  

    if(!in_category("Community News")) {
    $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";
    $html.="</div>";
    $content .= $html;

    }

    return $content;

}

In Wordpress 2.8 and above, the_author_description(); has been deprecated and replaced with another function: the_author_meta(‘description’);. However, you have to use get_the_author_meta() if you need to return (not display) the information. Take a look at this function’s information at Wordpress Codex.

In this function, I set the condition that the bio box will not display if this post is in Community News category. I’m using the same as above. This function is not return the bio box, it joins the post content and bio box content into one.

Next, you need to hook this function to the_content with code:

add_filter('the_content', 'get_author_bio');

For the example, before you use this hack, your bio box will display after the Related Posts, likes this:

wordpress hack bio box

But after you did some hacks in functions.php file, you will get a result likes mine at the end of this post.

To display something at the begin, easy as adding it normally into your post theme file: single.php

Sometime, you want to display your ads right below your post’s introduction. We will need to split the content into two parts, then insert ads code at the middle. Let’s write a function likes this:

function put_middle_content($content='') {

    if ( preg_match('/<span id="(.*?)?"><\/span>/', $content, $matches) ) {

        $content = explode($matches[0], $content, 2);

        $content = $content[0] . $matches[0] . '[YOUR ADS ARE HERE]' . $content[1];
    }

    return $content;

}

If your post has More tag, this function will split the content into 2 parts with delimiter is the More tag. Then, It will combine the content array again with the adding between 2 array strings.

Adding the code below to complete:

add_filter('the_content', 'put_middle_content');

That’s all


Yup, that’s all. Now, you can add anything you want to anywhere, such as: top, middle, and right after the post content that you could not do with the template display file. Your adding’s priority will be always the first.

  • Digg This Post
  • Tweet This Post
  • Stumble This Post
  • Submit This Post To Delicious
  • Submit This Post To Reddit
  • Submit This Post To Mixx
  • Share on your Facebook
  • Submit this post to Dzone
  • Submit this post to Designbump
  • Submit this post to TheWebBlend

Author: Lam Nguyen

I'm Lam Nguyen, a 21 year old web developer writing about everything related to web design. I am owner of AEXT.NET and WhoFreelance.com Web Community News. You can catch me on twitter.


66 User Comments

  1. designfollow 24. Oct, 2009 at 6:52 am #

    thanks for thes info.

  2. Gopal Raju 25. Oct, 2009 at 4:55 am #

    Thanks for the hack!

  3. Brandon Cox 25. Oct, 2009 at 2:17 pm #

    How did you know I was looking for this exact hack today? Wow – very cool timing!

    • Lam Nguyen 25. Oct, 2009 at 2:21 pm #

      Because some of my clients want to add some ads code inside post content. :D Thanks for enjoy reading!

  4. Tutorial City 25. Oct, 2009 at 5:57 pm #

    Very clever and useful!

  5. Mal Milligan 26. Oct, 2009 at 6:31 pm #

    Nice hack Dude !! Just got your link from WP Recipes. This is really a beautiful to look at site… lots of eye candy and great title selection. I’m impressed. Now all you need is some PR on your homepage.

  6. Delighted 26. Oct, 2009 at 6:40 pm #

    This is awesome Lam! Very cool.. Thank you Sir

  7. Lam Nguyen 26. Oct, 2009 at 7:02 pm #

    @Mal Milligan: Thank you :D , but I’m not good at page rank increasing. You’re correct, and I’ll try to increase my page rank.

  8. Jitendra 28. Oct, 2009 at 12:40 am #

    Hi Lam,

    I would like to know if we can get the total no. of posts by a specific user?

  9. Elena 30. Oct, 2009 at 3:01 am #

    I recently came across your blog and have been reading along. I thought I would leave my first comment. Nice post!

  10. sandy 31. Oct, 2009 at 10:07 am #

    I was finding from so many days finally found it. Thanks very much.Please tell me how to give style to it.It is coming plain text so i want to add some background.

    And aslo a thing like visit website and follow on twitter.

  11. sandy 31. Oct, 2009 at 10:11 am #

    Never mind thanks got it.

  12. Mia 03. Nov, 2009 at 1:09 pm #

    Lam, your site simply ROCKS ! most informative & clearly defined, easy to understand for even a beginner like me :) Thank you so much for sharing all your knowledge . Wishing you much continued future success within your career endeavors .

    Best wishes , M. 

  13. Mauro 09. Nov, 2009 at 2:16 pm #

    Really nice code, thanks for sharing ;D

  14. Arun 17. Nov, 2009 at 4:31 am #

    How do I customize the box via CSS that is what I am looking at. It worked like a charm…but no way I can customize this ?

    • Lam Nguyen 17. Nov, 2009 at 5:52 pm #

      It’s easy that you can do by add css style by the id and class name of the elements.

  15. Pavan Somu 18. Dec, 2009 at 1:08 am #

    Thanks for the hack. Its very obvious even for fellow bloggers.

  16. Pen 2 Net 24. Dec, 2009 at 8:34 am #

    This is amazing. I learnt it works wven with WPMU. Does it work with buddypressed WPMU?

    • Lam Nguyen 24. Dec, 2009 at 12:07 pm #

      I’ve never used WPMU before, so I’m not sure :)

  17. radiaku 27. Dec, 2009 at 8:53 am #

    Its work in 2.9.

    Its seem not working in me :(

  18. krushna 09. Feb, 2010 at 4:32 am #

    hi Lam, thanks for the information about wp. can you pls do more post related to wp hacks. it will very helpful for the beginner like me.

  19. insic 16. Feb, 2010 at 5:59 am #

    This will also appears in pages. what is the workaround to make it appear only in post?
    insic´s last blog ..WPBuzzer Wordpress Plugin Workaround My ComLuv Profile

Trackbacks/Pingbacks

  1. Wordpress Hack: Anything Can Be Added Anywhere In The Post Content | ShareFavorite - 24. Oct, 2009

    [...] the rest here: Wordpress Hack: Anything Can Be Added Anywhere In The Post Content Related [...]

  2. Tweets that mention Wordpress Hack: Anything Can Be Added Anywhere In The Post Content | AEXT.NET -- Topsy.com - 24. Oct, 2009

    [...] This post was mentioned on Twitter by Shurandy Thode and Lam Nguyen, Web Design News. Web Design News said: Wordpress Hack: Anything Can Be Added Anywhere In The Post Content http://bit.ly/4lUFbA [...]

  3. Wordpress Hack: Anything Can Be Added Anywhere In The Post Content | Web Design Updates - 25. Oct, 2009

    [...] Wordpress Hack: Anything Can Be Added Anywhere In The Post Content [...]

  4. 30+ Stunning Community Links for Designers and Developers | Programming Blog - 25. Oct, 2009

    [...] Wordpress Hack: Anything Can Be Added Anywhere In The Post Content [...]

  5. Wordpress Hack: Anything Can Be Added Anywhere In The Post Content | AEXT.NET - 25. Oct, 2009

    [...] is the original post: Wordpress Hack: Anything Can Be Added Anywhere In The Post Content | AEXT.NET Comments0 Leave a Reply Click here to cancel [...]

  6. Wordpress Hack: Anything Can Be Added Anywhere In The Post Content | AEXT.NET » Web Design - 26. Oct, 2009

    [...] Wordpress Hack: Anything Can Be Added Anywhere In The Post Content | AEXT.NET [...]

  7. WordPress hack: Automatically insert author bio on each post - 26. Oct, 2009

    [...] to Lam Nguyen for this cool little trick! If you enjoyed this article, please consider sharing it! [...]

  8. CSS Brigit | Wordpress Hack: Anything Can Be Added Anywhere In The Post Content - 26. Oct, 2009

    Wordpress Hack: Anything Can Be Added Anywhere In The Post Content…

    Learn how to add anything to anywhere in the wordpress post content with a small wordpress hack….

  9. Wordpress Hack: Anything Can Be Added Anywhere In The Post Content | Design Newz - 27. Oct, 2009

    [...] Wordpress Hack: Anything Can Be Added Anywhere In The Post Content [...]

  10. 100+ Mix of Great Community Links! | Admix Web - 27. Oct, 2009

    [...] Wordpress Hack: Anything Can Be Added Anywhere In The Post Content [...]

  11. 30+ Stunning Community Links for Designers and Developers | Master Design - 28. Oct, 2009

    [...] Wordpress Hack: Anything Can Be Added Anywhere In The Post Content [...]

  12. 100+ Mix of Great Community Links! | Master Design - 30. Oct, 2009

    [...] Wordpress Hack: Anything Can Be Added Anywhere In The Post Content [...]

  13. Top 10 WordPress Hacks from October 2009 | Programming Blog - 02. Nov, 2009

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

  14. Top 10 WordPress Hacks from October 2009 | meshdairy - 02. Nov, 2009

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

  15. Top 10 WordPress Hacks from October 2009 | ART HACKER - 04. Nov, 2009

    [...] $content; }   add_filter('the_content', 'get_author_bio');</div> 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 speedIs your host slow? Althought on [...]

  16. Top 10 WordPress Hacks | Cosmos Blog - 04. Nov, 2009

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

  17. Ten Tips for Becoming a Better WordPress Developer | Web Design Ledger - 04. Nov, 2009

    [...] building an author bio box for each post [...]

  18. Ten Tips for Becoming a Better WordPress Developer | meshdairy - 05. Nov, 2009

    [...] building an author bio box for each post [...]

  19. Ten Tips for Becoming a Better WordPress Developer | Programming Blog - 06. Nov, 2009

    [...] building an author bio box for each post [...]

  20. Ten #Tips for Becoming a Better #WordPress #Developer | Master Design - 06. Nov, 2009

    [...] building an author bio box for each post [...]

  21. 93 liens sur PHP, Wordpress, Photoshop, jQuery, CSS3, … - 07. Nov, 2009

    [...] Wordpress Hack: Anything Can Be Added Anywhere In The Post Content Wordpress: Ajouté la fiche de l'auteur dans un article et insérez de la pub après l'intro [...]

  22. Automatically insert author bio on each post | Dhrobonil - 13. Nov, 2009

    [...] $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/ Share and [...]

  23. 100 top Wordpress tips | FAQPAL Blog - 15. Nov, 2009

    [...] Source… [...]

  24. wordpress hack anything can be added anywherre in the post content | Squico - 17. Nov, 2009

    [...] In: Wordpress plugins 17 Nov 2009 Go to Source [...]

  25. How To Add Author Avatar and Bio to Posts on WordPress and WPMU - WordPress MU and BuddyPress plugins, themes, support, tips and how to's - 20. Nov, 2009

    [...] want to credit Lam Nguyen for this helpful little hack that I found in his post on this [...]

  26. Top 10 WordPress tips for optimization | Dhrobonil - 23. Nov, 2009

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

  27. 10 Useful Code Snippets And Plugins To Spice Up Wordpress Avatar | AEXT.NET - 01. Dec, 2009

    [...] Source: AEXT.NET [...]

  28. 22 Latest Exceptional WordPress Hacks | Programming Blog - 08. Dec, 2009

    [...] Source Link [...]

  29. 22 Latest Exceptional WordPress Hacks | EzTips | Wordpress Tips - Tutorials - Make Money Online - 08. Dec, 2009

    [...] Source Link [...]

  30. 22 Latest Exceptional WordPress Hacks | meshdairy - 10. Dec, 2009

    [...] Source Link [...]

  31. Wordpress Hack: Anything Can Be Added Anywhere In The Post Content - 11. Dec, 2009

    [...] your post content which is such as: YARPP (Related Posts) or Sociable …. Continued here: Click Here…Read Other Interesting Posts in PHP TutorialsThe ways to create thumbnail image by php with [...]

  32. 10 Useful Code Snippets And Plugins To Spice Up Wordpress Avatar | Master Design - 16. Dec, 2009

    [...] Source: AEXT.NET [...]

  33. Top 10 WordPress Hacks from October 2009 - 23. Dec, 2009

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

  34. 13 Useful Code Snippets for WordPress Development | Web Design Ledger - 29. Dec, 2009

    [...] Source [...]

  35. 5 Useful Tweaks for WordPress Developers : Northbound Designs - 15. Jan, 2010

    [...] Source [...]

  36. How-to Wrap Google Adsense In Wordpress Posts Correctly | AEXT.NET - 21. Jan, 2010

    [...] coupe of months ago, I published a post Wordpress Hack: Anything Can Be Added Anywhere In The Post Content. With that tutorial, you can add anything in WordPress posts on top, bottom before other plugins [...]

  37. 37 Cool Wordpress Hacks And Tutorials You Should Try | - 25. Jan, 2010

    [...] 26.How To Insert Author Bio On Each Post [...]

  38. 37 Cool Wordpress Hacks And Tutorials You Should Try | Afif Fattouh - Web Specialist - 26. Jan, 2010

    [...] 26.How To Insert Author Bio On Each Post [...]

  39. Tarqy dot Com | Menambahkan Identitas Penulis Artikel | Blogging, Wordpress Plugin, Wordpress Themes, Wordpress Tips - 26. Jan, 2010

    [...] for Lam Nguyen Share and [...]

  40. 37个Wordpress经典教程 « Booto'Blog - 26. Jan, 2010

    [...] 26.How To Insert Author Bio On Each Post [...]

  41. WP Hack: Add Anything, Anywhere To Post - Black Hat Forum Black Hat SEO - 16. Feb, 2010

    [...] Hack: Add Anything, Anywhere To Post good for adding adsense codes… http://aext.net/2009/10/wordpress-ha…-post-content/ __________________ _/_/_/_/ _/TSP_/ [...]

  42. 37个Wordpress经典教程 | 第七日 - 23. Feb, 2010

    [...] 26.How To Insert Author Bio On Each Post [...]

  43. 13 Useful Code Snippets for WordPress Development | WebsGeek - 23. Feb, 2010

    [...] Source [...]

Leave a Reply

CommentLuv Enabled