How to Create Custom RSS Feeds in WordPress | Web Development
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:
/** * 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="$charset; ?>"?>do_action('rss2_ns'); ?>> // Reset Query wp_reset_query();bloginfo_rss( 'name' ); ?> - Feed
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("{$thumbnail} "); } }
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