Tips on how to Add Customized Content material to WordPress Feeds

Tips on how to Add Customized Content material to WordPress Feeds

All

WordPress / All 29 Views comments

There are numerous ways to add custom content to your WordPress feeds. When you're not using a plugin, it is attainable to only add a code snippet to your theme's functions.php file. For many instances, I feel in all probability going the plugin route is the simplest method so as to add customized content material to your WordPress RSS/feeds. Simply set up, activate, add your content material and finished. However for WordPress builders and designers who need extra fine-grained management, this text explains learn how to add customized feed content material programmatically utilizing the WP API. So whether or not you have to add copyright textual content, ads, hyperlinks, or nearly something in any respect, this publish explains the way to make it occur.

Add customized content material to feed content material

Typically the simplest option to clarify one thing is to only present it:

// add customized content material to all feeds
perform shapeSpace_add_content_to_all_feeds($content material) 

	$earlier than = '<p>Customized content material displayed earlier than content material.</p>';
	$after = '<p>Customized content material displayed after content material.</p>';

	if (is_feed()) 

		return $earlier than . $content material . $after;

	 else 

		return $content material;

	


add_filter('the_content_feed', 'shapeSpace_add_content_to_all_feeds');

This snippet provides the required customized content material to every merchandise in your WordPress feeds. You'll be able to add this code to your WordPress website by together with it in your theme, or you can also make a easy plugin and go that route. Both approach is ok, completely as much as you.

The way it works

Within the earlier code, our perform shapeSpace_add_content_to_all_feeds() first defines the customized content material that we need to add $earlier than the feed content material and $after the feed content material. Utilizing both/each of those variables, you'll be able to outline nearly any customized content material, textual content, markup, or no matter you would like.

After defining the customized content material, the perform continues with a conditional verify by way of the is_feed() tag. If the request is for any of WordPress’ feeds, then the customized content material might be included with the feed content material. In any other case, if the request is just not for any feed, then the unique content material is returned unmodified.

FYI: Technically the conditional is_feed() examine is just not mandatory, however it's helpful to point out how a conditional tag may be used on this context.

By itself, our shapeSpace_add_content_to_all_feeds() perform will not do something. To get it to work, we have to name it. And since that is WordPress, we will use add_filter() to decide on precisely when and the place the perform ought to be referred to as. For WP feeds, we will hook our perform into the_content_feed, so our customized content material will probably be added to the <content material> tag of each WordPress feed.

Instance of feed output

To assist visualize what occurs when customized content material is added to WordPress feeds, right here is an instance of what the feed markup (XML) will seem like after including our customized $earlier than and $after content material utilizing the tactic above:

<merchandise>
	<title>Securing the WP REST API</title>
	<hyperlink>https://digwp.com/2018/08/secure-wp-rest-api/</hyperlink>
	<feedback>https://digwp.com/2018/08/secure-wp-rest-api/#feedback</feedback>
	<pubDate>Mon, 27 Aug 2018 19:40:33 +0000</pubDate>
	<dc:creator>Jeff Starr</dc:creator>
	<class><![CDATA[ Security ]]></class>
	<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 material: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 material:encoded>
	<wfw:commentRss>https://digwp.com/2018/08/secure-wp-rest-api/</wfw:commentRss>
	<slash:feedback>1</slash:feedback>
</merchandise>

Right here you possibly can see the "earlier than" and "after" customized content material added to the <content material> tag. And that is just about all there's to it. Now let's change it up and add our customized content material to the feed description (i.e., excerpt).

Add customized content material to feed description

Within the earlier part, we cowl tips on how to add our customized content material to the the <content material> tag. To as an alternative show the customized content material within the <description> tag, we merely want to vary the hook that's utilized by our add-content perform. So as an alternative of doing this:

add_filter('the_content_feed', 'shapeSpace_add_content_to_all_feeds');

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

add_filter('the_excerpt_rss', 'shapeSpace_add_content_to_all_feeds');

Right here is the results of utilizing this new tag:

<merchandise>
	<title>Securing the WP REST API</title>
	<hyperlink>https://digwp.com/2018/08/secure-wp-rest-api/</hyperlink>
	<feedback>https://digwp.com/2018/08/secure-wp-rest-api/#feedback</feedback>
	<pubDate>Mon, 27 Aug 2018 19:40:33 +0000</pubDate>
	<dc:creator>Jeff Starr</dc:creator>
	<class><![CDATA[ Security ]]></class>
	<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>Customized content material displayed after content material.</p>
		]]>
	</description>
	<content material: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 material:encoded>
	<wfw:commentRss>https://digwp.com/2018/08/secure-wp-rest-api/</wfw:commentRss>
	<slash:feedback>1</slash:feedback>
</merchandise>

By hooking our perform into the_excerpt_rss, our "earlier than" and "after" content material is added to the <description> as an alternative of the <content material>. So it follows that we will add to BOTH description AND content material by calling our perform with BOTH hooks, one thing 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 house message

Primary factor to recollect:

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

You'll be able to name your "content-adding" perform utilizing both or each of those hooks. Then you'll be able to configure nearly any type of customized feed content material conceivable. BTW, I discover myself utilizing this system considerably often, so I wrote a plugin to assist automate the method of including customized content material to feeds, posts, pages, or any mixture — provides you plenty of flexibility.

Comments