Australia's Leading Digital Marketing Experts. T. 1300 235 433  |  Aggregation Enquires Welcome

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.

1
<?php 
2
/*
3
 Add Anything to the Beginning and End of WordPress Post Content
4
 https://www.beliefmedia.com.au/wordpress-post-content
5
*/
6
 
7
function beliefmedia_before_after_content($content) {
8
 
9
 $b = '';
10
 $a = '';
11
 
12
 /* No custom content */
13
 if ($a == '' && $b == '') return $content;
14
 
15
 /* The before and after stuff.. */
16
 if ($b != '') $content = $b . $content;
17
 if ($a != '') $content =  $content . $a;
18
 
19
 return $content;
20
}
21
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).

Conclusion

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.

Download


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.

  AUS Eastern Standard Time (Virginia)

  Want to have a chat?
 

Like this article?

Share on facebook
Share on Facebook
Share on twitter
Share on Twitter
Share on linkedin
Share on Linkdin
Share on pinterest
Share on Pinterest

Leave a comment