Like the blog? Get the book »

How to Add Custom Content to WordPress Feeds

How to Add Custom Content to WordPress Feeds

There are numerous ways to add custom content to your WordPress feeds. If you're not using a plugin, it's possible to just add a code snippet to your theme's functions.php file. For most cases, I think probably going the plugin route is the easiest way to add custom content to your WordPress RSS/feeds. Just install, activate, add your content and done. But for WordPress developers and designers who want more fine-grained control, this article explains how to add custom feed content programmatically using the WP API. So whether you need to add copyright text, advertisements, hyperlinks, or virtually anything at all, this post explains how to make it happen.

Add custom content to feed content

Often the easiest way to explain something is to just show it:

// add custom content to all feeds
function shapeSpace_add_content_to_all_feeds($content) {

	$before = '<p>Custom content displayed before content.</p>';
	$after = '<p>Custom content displayed after content.</p>';

	if (is_feed()) {

		return $before . $content . $after;

	} else {

		return $content;

	}

}
add_filter('the_content_feed', 'shapeSpace_add_content_to_all_feeds');

This snippet adds the specified custom content to each item in your WordPress feeds. You can add this code to your WordPress site by including it in your theme, or you can make a simple plugin and go that route. Either way is fine, totally up to you.

How it works

In the previous code, our function shapeSpace_add_content_to_all_feeds() first defines the custom content that we want to add $before the feed content and $after the feed content. Using either/both of these variables, you can define virtually any custom content, text, markup, or whatever you wish.

After defining the custom content, the function continues with a conditional check via the is_feed() tag. If the request is for any of WordPress’ feeds, then the custom content will be included with the feed content. Otherwise, if the request is not for any feed, then the original content is returned unmodified.

FYI: Technically the conditional is_feed() check is not necessary, but it's useful to show how a conditional tag might be used in this context.

By itself, our shapeSpace_add_content_to_all_feeds() function won't do anything. To get it to work, we need to call it. And because this is WordPress, we can use add_filter() to choose exactly when and where the function should be called. For WP feeds, we can hook our function into the_content_feed, so our custom content will be added to the <content> tag of every WordPress feed.

Example of feed output

To help visualize what happens when custom content is added to WordPress feeds, here is an example of what the feed markup (XML) will look like after adding our custom $before and $after content using the method above:

<item>
	<title>Securing the WP REST API</title>
	<link>https://digwp.com/2018/08/secure-wp-rest-api/</link>
	<comments>https://digwp.com/2018/08/secure-wp-rest-api/#comments</comments>
	<pubDate>Mon, 27 Aug 2018 19:40:33 +0000</pubDate>
	<dc:creator>Jeff Starr</dc:creator>
	<category><![CDATA[ Security ]]></category>
	<guid isPermaLink="false">https://digwp.com/?p=8446</guid>
	<description>
		<![CDATA[ I think many WordPress users probably underestimate the amount of data that is made available via the REST API. Just about everything is available to anyone or anything that asks for it: posts, pages, categories, tags, comments, taxonomies, media, users, settings, and more. [&#8230;] ]]>
	</description>
	<content:encoded>
		<![CDATA[
		<p>Custom content displayed before content.</p>

		<p>I think many WordPress users probably underestimate the amount of data that is made available via the <a href="https://developer.wordpress.org/rest-api/" title="REST API Handbook">REST API</a>. Just about everything is available to anyone or anything that <strong>asks for it</strong>: posts, pages, categories, tags, comments, taxonomies, media, users, settings, and more. ...</p>
		<p>Post content continues here...</p>

		<p>Custom content displayed after content.</p>
		]]>
	</content:encoded>
	<wfw:commentRss>https://digwp.com/2018/08/secure-wp-rest-api/</wfw:commentRss>
	<slash:comments>1</slash:comments>
</item>

Here you can see the "before" and "after" custom content added to the <content> tag. And that's pretty much all there is to it. Now let's change it up and add our custom content to the feed description (i.e., excerpt).

Add custom content to feed description

In the previous section, we cover how to add our custom content to the the <content> tag. To instead display the custom content in the <description> tag, we simply need to change the hook that is used by our add-content function. So instead of doing this:

add_filter('the_content_feed', 'shapeSpace_add_content_to_all_feeds');

..we hook our function into the_excerpt_rss, like so:

add_filter('the_excerpt_rss', 'shapeSpace_add_content_to_all_feeds');

Here is the result of using this new tag:

<item>
	<title>Securing the WP REST API</title>
	<link>https://digwp.com/2018/08/secure-wp-rest-api/</link>
	<comments>https://digwp.com/2018/08/secure-wp-rest-api/#comments</comments>
	<pubDate>Mon, 27 Aug 2018 19:40:33 +0000</pubDate>
	<dc:creator>Jeff Starr</dc:creator>
	<category><![CDATA[ Security ]]></category>
	<guid isPermaLink="false">https://digwp.com/?p=8446</guid>
	<description>
		<![CDATA[
		<p>Custom content displayed before content.</p>
		
		I think many WordPress users probably underestimate the amount of data that is made available via the REST API. Just about everything is available to anyone or anything that asks for it: posts, pages, categories, tags, comments, taxonomies, media, users, settings, and more.  [&#8230;] 
		
		<p>Custom content displayed after content.</p>
		]]>
	</description>
	<content:encoded>
		<![CDATA[
		
		<p>I think many WordPress users probably underestimate the amount of data that is made available via the <a href="https://developer.wordpress.org/rest-api/" title="REST API Handbook">REST API</a>. Just about everything is available to anyone or anything that <strong>asks for it</strong>: posts, pages, categories, tags, comments, taxonomies, media, users, settings, and more. ...</p>
		<p>Post content continues here...</p>
		
		]]>
	</content:encoded>
	<wfw:commentRss>https://digwp.com/2018/08/secure-wp-rest-api/</wfw:commentRss>
	<slash:comments>1</slash:comments>
</item>

By hooking our function into the_excerpt_rss, our "before" and "after" content is added to the <description> instead of the <content>. So it follows that we can add to BOTH description AND content by calling our function with BOTH hooks, something like this:

add_filter('the_content_feed', 'shapeSpace_add_content_to_all_feeds');
add_filter('the_excerpt_rss', 'shapeSpace_add_content_to_all_feeds');

Take home message

Basic thing to remember:

  • the_excerpt_rss — filters feed <description>
  • the_content_feed — filters feed <content>

You can call your "content-adding" function using either or both of these hooks. Then you can configure just about any sort of custom feed content that you can imagine. BTW, I find myself using this technique somewhat frequently, so I wrote a plugin to help automate the process of adding custom content to feeds, posts, pages, or any combination — gives you lots of flexibility.

Leave a Reply

Required fields marked *. Email kept private. Basic markup allowed. By entering your data and submitting your comment, you agree to our Privacy Policy.

Advertise Here

Sign up for the newsletter!

Never spam, only good stuff

Recent posts

Loading...

Random posts

Loading...
© 2009–2019 Digging Into WordPress Powered by WordPress Monzilla Media shapeSpace