How to Create Custom RSS Feeds in WordPress

multiple-rss-feeds-featured

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:

 $postType, 'showposts' => $postCount ) ); 
$charset = get_option( 'blog_charset' );
header( 'Content-Type: ' . feed_content_type( 'rss-http' ) . '; charset=' . $charset, true );
?>"?>
>

<?php bloginfo_rss( 'name' ); ?> - Feed










<?php the_title_rss(); ?>




]]>
]]>








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

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Nicholas Godwin Avatar

Read next

Suzanne Simard sealed paper birch and Douglas fir seedlings inside plastic bags, fed them carbon-14 and carbon-13 dioxide, and nine days later found carbon had crossed between species through fungal threads in the British Columbia soil beneath her boots
A species of jellyfish called Turritopsis dohrnii can revert its adult cells back to a juvenile polyp stage when injured or starving, effectively restarting its life cycle, and biologists have so far failed to identify any natural limit to how many times it can do this.
A Japanese man named Jiroemon Kimura, who lived to 116, was born in 1897 when Queen Victoria still ruled and died in 2013, meaning a single human life personally overlapped with the invention of the airplane, the atomic bomb, the internet, and Instagram
The Hollywood sign originally read HOLLYWOODLAND when it was built in 1923 as a real estate advertisement for a housing development, and it was only meant to stand for 18 months, but nobody ever got around to taking it down and the city eventually adopted it as a landmark
Almost all of the world’s internet traffic does not travel by satellite but through fibre-optic cables lying on the ocean floor, a hidden web of wires crossing the deepest parts of the sea to connect the continents.
People who flip their phone face down on every table aren’t being secretive. They figured out that staying interruptible meant handing their time to whoever rang first
Twitch vs. Facebook Gaming vs. YouTube Gaming: What’s the Best Live Game Streaming Platform?
Chrome Extensions Ownership Transfer is a Direct Threat to You: How to Stay Safe