RBA Cash Rate: 4.35% · 1AUD = 0.67 USD · Inflation: 4.1%  
Leading Digital Marketing Experts | 1300 235 433 | Aggregation Enquires Welcome | Book Appointment
Example Interest Rates: Home Loan Variable: 5.20% (5.24%*) • Home Loan Fixed: 5.48% (6.24%*) • Fixed: 5.48% (6.24%*) • Variable: 5.20% (5.24%*) • Investment IO: 5.78% (6.81%*) • Investment PI: 5.49% (6.32%*)

Add Anything to the Beginning and End of WordPress Post Content

Add Anything to the Beginning and End of WordPress Post Content

First published on Internoetics years ago, and then migrated to the Belief website in 2014 as a draft, this article has lived with dozens of others in a state of uncertain purgatory. We expected to include a library of functions and shortcodes that would render various social icons and widgets into your WordPress post or page (which we have effectively done) before showing how they all might be rendered at the top or bottom of each page on your website. With the introduction of Gutenberg, our shift towards Elementor for clients, our own sharebar code, and the inclusion of a fully integrated mortgage broker website, the feature we intended to package up as a plugin a number of years ago is now a little less relevant now than it once was.

Hooks and Filters: It's not entirely uncommon to include content in the header or footer (such as analytics code). A relatively recent version of WordPress (V5.2) added a wp_body_open() hook directly after the opening body tag that every theme developer will or already has invariably incorporate into their themes (negating the need for our pre V5.2 hack). In fact, there are numerous filters and hooks that permit the inclusion of content into various areas of your website... although it's really just the the_content() filter we're most interested in for the purpose of including content before and/or after a post on your WordPress website.

A short time ago we introduced how Yabber Tag: yabber will render content above or below your content on the basis of an inbound email campaign link, and we also introduced how you might show time-limited content above or below your website articles to defined audiences. If you're familiar with our core marketing philosophy you'll also appreciate how we espouse that a a subscription form be shown on every single page of your website. This article introduces the code necessary to implement the features yourself.

One of the many filters made available within WordPress is the_content filter. What it essentially does it take your post content, does something with it, then spits it out at the other end with the appropriate modifications applied. You might do something nutty such as apply Pirate Day text, or use the filter to evaluate text for more usable replacements, such as geographic placeholders. In our case, we'll ignore the text completely and simply bolt on some additional HTML to either side of our post content.

The WordPress Function

Copy and paste the WordPress function into your theme's functions.php file or, if you sensibly have one installed, your custom functions plugin.

 Add Anything to the Beginning and End of WordPress Post Content
function beliefmedia_before_after_content($content) {
 $b = '';
 $a = '';
 /* No custom content */
 if ($a == '' && $b == '') return $content;
 /* The before and after stuff.. */
 if ($b != '') $content = $b . $content;
 if ($a != '') $content =  $content . $a;
 return $content;
add_filter('the_content', 'beliefmedia_before_after_content');

If you require shortcode to work in a sidebar widget, you'll have to enable the functionality with a filter. If you're using our custom functions plugin, you'll have that feature enabled by default.

If you were to use the above function you would simply assign some text to the $a or $b variable (inside the quotes) making sure to escape single quotes with a \slash. Depending on how your system is structured you may have to wrap your content into paragraph tags. Any shortcodes in yoru content should run, but if a later priority was set you can still wrap the do_shortcode() function around your custom content variables.

Your content can come from anywhere, such as an external API, anywhere in your database, or external code (such as embed code).


At some point we'll create a WordPress plugin to enable ease in assigning the content, and we'll introduce some conditional features so the content only returns on certain pages. In the meantime the custom functions options is normally more than suitable.


Title: Include Content Before or After Post
Description: Include Content Before or After Post. Basic filter function only.
  Download • Version 0.1, 464.0B, zip, Category: WordPress Shortcodes
  Featured Image: First train leaving new (third) Sydney Station 1906. Now Central Station. [ View Image ]

Download our 650-page guide on Finance Marketing. We'll show you exactly how we generate Billions in volume for our clients.

  E. Australia Standard Time [ UTC+10, Default ] [ CHECK TO CHANGE ]

  Want to have a chat?


Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest

Leave a comment