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

written by aext on October 24, 2009 in WordPress with 80 comments

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

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.