Use WordPress's Content Filter to Insert Anything into Post & Page Content

WordPress has a bunch of built-in filters that work in any properly-coded theme. Ever wonder how many of those fancy plugins you use work? The ones that add share buttons to the beginning of your posts, or at the end – right after the content?

Entering from Stage Left: The Content Filter!

You can use WordPress’s content filter to add anything to the start or end of your post/page content. In fact, you can use it to insert stuff into the middle of content, but I won’t get into that today. We’re going to keep it simple.

Note: This tutorial requires that you modify your custom_functions.php file. It assumes that you know how to copy and paste from this tutorial into that file, that you know how to access & edit the file via FTP in case you make an error, and that you know how to write the HTML / PHP to create the string you want added to your content. You can ask for help under Advanced Customization on the forums at Headway, but remember it’s not our top priority. You might have more luck looking for help on Stack Overflow, or the WordPress.org forums!

My ultimate goal here is a function that will insert an ad block on WordPress posts, but not Pages – and only if there is not a custom field present designed to hide the adblock on that page. This will give me a lot of flexibility in my posting while also making it brain-dead simple to monetize. You could substitute an adblock for social media icons or share buttons, quotes, graphics, thumbnails – whatever you want here.

Here’s the basic structure. This function won’t really do anything, since we’re passing in the $content variable, then returning it as-is – without doing anything to it. I’m calling my function ‘add_adblock’ so that it makes sense later on, and I’ve added some comments to make the purpose clear.

{code type=PHP}
//Insert an Adblock to the beginning of post content
function add_adblock( $content ) {
return $content;
}
//Insert function using a filter
add_filter(‘the_content’,’add_adblock’);
{/code}

Breaking it down:
function add_adblock() is where we’re declaring our function. In the parenthesis, I’ve passed $content, a global WordPress variable that represents – you guessed it – the content of a post or page.

add_filter(‘the_content’,’add_adblock’); is where we define a few critical pieces. First, we’re adding a filter. Second, the filter we’re adding is WordPress’s the_content filter. This article on the WordPress codex is a bit vague, but it does provide us an example and brief description of the_content filter. Finally, we define which function is being used in the filter – namely, our add_adblock function.

Everything in the middle – between the first { and last } – is where we define what happens to $content. We’ll build a string with PHP/HTML after return that includes $content to add our adblock, like so:
{code type=PHP}
//Insert an Adblock to the beginning of post content
function add_adblock( $content ) {
if( !is_single() ) {
// Do not show ads on anything other than single post pages
// Just leave content alone
return $content;
} else {
return ‘[adrotate group="4"]‘ . $content;
// This string combines a shortcode for a popular ad plugin-AdRotate
// and the content variable.
}
}
add_filter(‘the_content’,’add_adblock’);
{/code}

Now we have a function that inserts the adrotate shortcode at the beginning of post content – but not page content, thanks to our handy conditional. I could insert content at the end of the content by changing the string to put $content first – return $content . ‘[adrotate group="4"]‘;

Finally, we’ll add the logic to check for a custom field value, and allow users to insert a custom field called “hideads” and value to override the default display of the adblock. If the key is present and has a value “on”, we’ll return $content untouched, but if it’s not present or is set to anything other than “on”, we’ll display the filtered content with ads.
{code type=PHP}
//Insert an Adblock to the beginning of post content
function add_adblock( $content ) {
global $post;
// Get custom field data
$key = ‘hideads';
$hideads = get_post_meta($post->ID,$key, true);
if($hideads == ‘on’ || !is_single() ) {
// If custom field is empty/not set to on, display ads
// If custom field is is set to on, do not display ads
// Do not show ads on anything other than single post pages
// Just leave content alone
return $content;
} else {
return ‘[adrotate group="4"]‘ . $content;
// This string combines a shortcode for a popular ad plugin-AdRotate
// and the content variable.
}
}
add_filter(‘the_content’,’add_adblock’);
{/code}

Voila! An adblock inserted into the content. My adblock happens to be wrapped in a DIV, so now I can float my ad to the right or left or my content and let the text flow around it.

Spread the word!

7 Responses

  1. I was trying to put social media sharing buttons to the right side of excerpts on home page and archive pages like Mashable has. I am confused what will be the code to target only excerpts leaving the search results page.

    And where we are supposed to put the actual code of either Ad or social icons html, JS?

    • If you aren’t familiar with PHP this can be a difficult tip. You’d need some additional conditional PHP to target only the excerpts. I’d recommend that you hire a developer or look for a plugin to achieve this.

  2. Very cool. Is there a way to block all plugins from page post content? What I’m after is a way clients can use WordPress more like a CRM system where they can make page posts without having their Social Media plugins popping up everywhere.

  3. Hello Caitlin,

    Thank you for this post. Could you explain how to add content at the end of post content. Also how to arrange it at end if there are more than one type of things to be added?

    Thanks in advance!

    • Hey there,

      You can add content after a post on the Single layout by using a Text block or Custom Code block.

      However, if you want to display content after posts in the loop (Blog Index, archives, etc), then your best bet is to use a plugin such as http://wordpress.org/plugins/add-to-content/

      Please let us know if you have any other questions.

Leave a reply

Current ye@r *

Headway Newsletter

Get the latest updates from our blog directly in your inbox! Are you ready to learn the latest from Headway Themes?

Copyright © 2014 Vesped Inc. All Rights Reserved. Proudly Powered by Headway and WordPress