With the abundance of news pieces and the number of blog sites continuously growing, keeping track of whatβs coming round can be tiring. Fortunately, there is such a standard as RSS that makes it super easy. It provides a simple yet an effective way to keep up with the latest news and what matters to you most. In other words, RSS grabs content from a particular website and aggregates it into a news reader application.
When implemented well, this automation solution saves readers time greatly. Really Simple Syndication monitors the websites that youβre interested in and synchronises all their new content updates as they come out. It takes the daunting search routine on its part, leaving you the time just to glimpse and enjoy the latest. Β
You can use these feeds in a number of ways. For very basic implementation, WordPress has its built-in default RSS feeds. But there are also more specialized purposes for delivering a more specific type of content, for which you may need to create a custom RSS. In this article, weβll show you how to do this.
Please note that this tutorial is not targeted for beginner level of WordPress users.
As always, before making any major changes to a live website, remember to complete your WordPress website backup.
The first thing you need to do is initialize your custom RSS action in your themeβs functions.php file.
add_action( 'init', 'myRSS' ); function myRSS() { add_feed( 'feedname', 'myRSSFeed' ); }
The function above adds the feed to the template through the add_feed function. The add_feed function has two arguments, feedname, and a callback function. The feedname will make up your new feed URLΒ yourdomain.com/feed/feedname, and the callback function will be called to actually create the feed. So, we need to create the myRSSFeed function in themeβs functions.php file.
You can place the RSS code directly in the callback function. By using get_template_path, we can keep the functionality separate to the layout.
function myRSSFeed() { get_template_path( 'slug', 'name' ); }
The get_template_path function takes the slug and name arguments to a template file like wp-content/themes/themename/slug-name.php or wp-content/themes/themename/slug.php.
Take a look on the template file.
<?php /** * RSS Feed Template by Julia N. */ $limitCount = 7; // The posts limit to show $posts = query_posts('showposts=' . $limitCount); // Setting up content type and charset headers header('Content-Type: '.feed_content_type('rss-http').';charset='.get_option('blog_charset'), true); // Setting up valid XML encoding echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'>'; ?> <!-- Declaring XML Validators namespaces --> <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" <?php do_action('rss2_ns'); ?>> <!-- Declaring channel with articles data --> <?php $dateTimeFormat = 'D, M d Y H:i:s'; ?> <channel> <title><?php bloginfo_rss('name'); ?> - Feed</title> <link><?php bloginfo_rss('url') ?></link> <description><?php bloginfo_rss('description') ?></description> <lastBuildDate><?php echo mysql2date($dateTimeFormat, get_lastpostmodified(), false); ?></lastBuildDate> <language><?php echo get_option('rss_language'); ?></language> <sy:updatePeriod><?php echo apply_filters( 'rss_update_period', 'daily' ); ?></sy:updatePeriod> <sy:updateFrequency><?php echo apply_filters( 'rss_update_frequency', '1' ); ?></sy:updateFrequency> <?php do_action('rss2_head'); ?> <?php while (have_posts()) : the_post(); ?> <item> <title><?php the_title_rss(); ?></title> <link><?php the_permalink_rss(); ?></link> <pubDate><?php echo mysql2date($dateTimeFormat, get_post_time('Y-m-d H:i:s', true), false); ?></pubDate> <dc:creator><?php the_author(); ?></dc:creator> <guid isPermaLink="false"><?php the_guid(); ?></guid> <description><![CDATA[<?php the_excerpt_rss() ?>]]></description> <content:encoded><![CDATA[<?php the_excerpt_rss() ?>]]></content:encoded> <?php rss_enclosure(); ?> <?php do_action('rss2_item'); ?> </item> <?php endwhile; ?> </channel> </rss>
The final thing that is left to do is flush your WordPress rewrite rules. To proceed with this action easily, you need to log in to the WordPress admin panel, and go Settings -> Permalinks. Once there, just click Save Changes, this will flush the rewrite rules. Now you can access your feed at yourdomain.com/feed/feedname.
The last thing to mention is that you can aggregate as many RSS feeds from as many sources as youβd like if you apply some ingenuity. The easiest way to do so is to make a use of WP RSS Aggregator. It is a very neat plugin for importing, merging and displaying RSS and Atom feeds on your WordPress site. This solution is very comprehensive and elegant for WordPress, so it does not take much effort and time to get started.
16 Responses
thanks.. nice info..
Anybody can help me to get the total likes in feed
currently i am getting the total commnet
i am using the plugin kodes post likes
which is not generating the total likes in the rss feeed
thanks
How to control custom feed type with custom rewrite rule ?
Thank you for the post. I will definitely visit again.
Hi,
Thanks for your post!
Any idea how to add a custom post type to this rss feed ?
Regards
Hey Julia, I have a question, can WP RSS Aggregator plugin let me customize my feed title?
Hey Jivan, if you mean the title of the whole RSS feed, then yes. It’s explained here:
https://kb.wprssaggregator.com/article/219-how-to-create-custom-rss-feeds
I have tried three dif types of code and they come up without a 404 but the files are blank. Any ideas?
get_template_path( ‘slug’, ‘name’ );
That is not a function it is
get_template_part( ‘slug’, ‘name’ );
this helped me to get the RSS functioning. Thanks Matt!
Great tutorial – would be nice to expand on getting set categories or even Custom post types or attributes / tags
I’m glad you like it, Scott. Thanks for your recommendations, I’ll consider them in my further posts π
Good tutorial (y)
but, i prefer to use WP RSS Aggregator because its very easy and simple
regards
Thanks, Alwib. WP RSS Aggregator is awesome too π
Im just agree with alwib.
In case WP RSS more easy to use
yeah i think so… it can more easy to use