BLOG

See what we're whipping up!

I’ve come across numerous articles and forum threads on the internet teaching and discussing how to add code before the_content via a custom function. Funny enough, I’ve also seen many others on how to add code after the_content via, yet again, another custom function. Well, prepared to be amazed! Because, today I’m going to tell you how to do both, at the same time! That’s right, at the same time! Whoo! Cheer! Oh, it’s not that exciting. Well, okay then. I’ll just go on with it.

Wait. Before we get started, and if you haven’t read it yet, you might want to checkout my Sweet WordPress Functions post, because I show you how to create your own plugin for custom functions and stuff. That way, your functions are safely tucked away and won’t be written over in you functions.php file when you update your theme. Also, if you do something wrong and your code goes haywire, you can just disable the plugin and viola, you’re saved! Alright, I’m actually moving on now.

First thing is first, we have to create a function.

function wpdev_before_after($content) {
}
add_filter(‘the_content’, ‘wpdev_before_after’);

So, we have our function now and it’s hooking into the_content(). Now, we need to feed it something! First, we’re going to use a variable to define our output before the_content, and then we’re going to use a variable to define our output after the_content.

$beforecontent = ‘This goes before the content. Isn’t that awesome!’;
$aftercontent = ‘And this will come after, so that you can remind them of something, like following you on Facebook for instance.’;

Alright, now we have our variables with our content. Let’s do some reorganization! Oh, how I love the simplicity of it.

$fullcontent = $beforecontent . $content . $aftercontent;
return $fullcontent;

Seriously, that’s it! So, with it all together now!

function wpdev_before_after($content) {
$beforecontent = ‘This goes before the content. Isn’t that awesome!’;
$aftercontent = ‘And this will come after, so that you can remind them of something, like following you on Facebook for instance.’;
$fullcontent = $beforecontent . $content . $aftercontent;
return $fullcontent;
}
add_filter(‘the_content’, ‘wpdev_before_after’);

Bam! Boom! Content before AND after the_content. You can use this for all kinds of stuff! Share buttons, Reminders to check you out on Facebook, Twitter, etc. Anything! Oh, and a hint, you can add more variables and add them to the $fullcontent string. How you arrange them in the string is how they’ll be output. Let me know if you have any questions or need any help in the comments below, and as always, have a great day!

Share this article: Share on FacebookTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUponPin on PinterestEmail this to someone

4 comments

  • If my new content is in a external file php, How call it? Thank you 🙂

    Reply
    • Hello Eva,

      Thanks for the question! You can use get_template_part(‘file-name’);

      If your file is custom-single.php, for example, it would be get_template_part(‘custom-single’);

      Reply
      • Hi. Thank you by your answer but I’m tried with this method and dont’t work.
        The content of file prints twice: Before the content (always before) and at the end of all code of page (after footer) 🙁

        Reply
        • Hey Eva,

          Could you please post your code? Thank you!

          Reply