What’s better than an RSS Feed? Custom RSS Feeds of course! A lot of people have predicted the death of this sharing technology for eons now, but it is still going strong.
Statistics show that over 2000 of the world’s top 10,000 websites publish RSS feeds religiously. Feedly, a popular RSS feed aggregation service, grew its subscriber base by 900% in two years and revenue by more than 400%.
You might be interested in giving your visitors a customized user experience beyond your default WordPress RSS feed. Let’s explore how to customize your feeds to suit your website and audience.
Back Up Your Website
You’ll be going to your website’s back end, so back up your site. If anything breaks, you’ll still have your website, content, and settings intact. Don’t neglect this step, no matter what!
Also, we will be creating a new template file for your WordPress theme, so you might want to switch to/create a child theme so as not to mess up your existing theme.
Creating Custom RSS Feed in WordPress
To get started, we will be working with the “functions.php” file in your theme folder. Open it and paste the code below:
/* This code initializes the custom RSS Feeds for your website*/ add_action( 'init', 'MyCustomRSS' ); function MyCustomRSS(){ add_feed( 'mycustomfeedname', 'MyCustomFeedCallback' ); } /* This code seeks the template for your RSS feed */ function MyCustomFeedCallback(){ get_template_part( 'rss', 'mycustomfeedname' ); // need to be in small case. }
Note the section that asks you to type in your custom RSS feed’s name. Keep this name simple so that you can recall it easily as it forms your website feed URL. e.g., “https://yoursite.com/feed/mycustomfeedname.”
Note: Your custom RSS feed’s name can only be in small letters.
Create a Custom RSS Feeds Template
The custom RSS feed’s template is the information your feed will need to serve your feed. Think of it as an HTML/CSS kind of feature for your feed. To do this, you have to create another file with a unique name. This name MUST follow the convention: “rss-mycustomfeedname.php.”
Save this file in your child theme folder. Now open this file and paste the following code:
<?php /** * Template Name: Custom RSS Template - YourCustomFeedName */ $postCount = 10; // The number of posts to show in the feed $postType = 'post'; // post type to display in the feed query_posts( array( 'post_type' => $postType, 'showposts' => $postCount ) ); $charset = get_option( 'blog_charset' ); header( 'Content-Type: ' . feed_content_type( 'rss-http' ) . '; charset=' . $charset, true ); ?><?xml version="1.0" encoding="<?php echo $charset; ?>"?> <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'); ?>> <channel> <title><?php bloginfo_rss( 'name' ); ?> - Feed</title> <atom:link href="<?php self_link(); ?>" rel="self" type="application/rss+xml" /> <link><?php bloginfo_rss('url'); ?></link> <description><?php bloginfo_rss('description'); ?></description> <lastBuildDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_lastpostmodified('GMT'), false); ?></lastBuildDate> <language><?php echo get_option('rss_language'); ?></language> <sy:updatePeriod><?php echo apply_filters( 'rss_update_period', 'hourly' ); ?></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('D, d M Y H:i:s +0000', 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> <?php // Reset Query wp_reset_query();
You can modify this file to show as many posts as you want. Use the $postCount
variable to change the number of posts to display. The default is showing “Posts,” but you can change it to show a different post type by changing the $postType
variable. The excerpts feature will also show excerpts for your posts. If your post doesn’t have excerpts it will show the first 120 words in the article.
Adding Functionality to the Custom RSS Feeds
Now that you have a functional template, you can add additional stuff to it.
Show Post Thumbnail in Feeds
If you want to include post thumbnails in your feed, copy and paste the following code to the bottom of your “functions.php” file.
If your post has a thumbnail image, this function will search for it and serve it together with your feed text. If it doesn’t have any image, then it does nothing.
/*This code adds thumbnail feature to your custom feed*/ add_action( 'rss2_item', 'custom_thumbnail_tag' ); function custom_thumbnail_tag() { global $post; if ( has_post_thumbnail( $post->ID ) ) { $thumbnail = get_attachment_link( get_post_thumbnail_id( $post->ID ) ); echo("<image>{$thumbnail}</image>"); } }
Set Feed Language
Sometimes you may need to declare the language for your custom feed for it to work.
Do this by using this code in your “functions.php” file:
function myFeedLanguage(){ update_option( 'rss_language', 'en' ); } add_action( 'admin_init', 'myFeedLanguage' );
This will configure your language to English. If you intend to change it to another language, then edit the en
in the function code to your preferred language. Get a list of all language codes from this link.
Get Your Custom Feed Working
To get your custom RSS feed to work, you need to apply a rewrite flush to your WordPress system. All you need to do is go to “Settings -> Permalinks” and click “Save Changes.”
Wrapping Up
A Custom RSS feed is a great way to serve your content to people who may not have enough connectivity to access your full website. It also helps new audiences discover you through RSS feed apps on Android and iOS. You should absolutely give it a try.
Image credit: United Colors of Feeds
Nicholas Godwin is a technology researcher who helps businesses tell profitable brand stories that their audiences love. He’s worked on projects for Fortune 500 companies, global tech corporations and top consulting firms, from Bloomberg Beta, Accenture, PwC, and Deloitte to HP, Shell, and AT&T. You may follow his work on Twitter or simply say hello. His website is Tech Write Researcher.
Subscribe to our newsletter!
Our latest tutorials delivered straight to your inbox
Sign up for all newsletters.
By signing up, you agree to our Privacy Policy and European users agree to the data transfer policy. We will not share your data and you can unsubscribe at any time. Subscribe