How to Create Rss Feed for My Wordpress Website
WordPress comes with built-in default RSS feeds. You can tweak the default feeds by adding custom content to your RSS Feeds, or even adding post thumbnail to your RSS Feeds. The default RSS and Atom feeds are enough for most users, but you may wish to create a custom RSS feed for delivering specific type of content. In this article, we will show you how to create custom RSS feeds in WordPress.
Please note that this tutorial is not intended for beginner level WordPress users. If you are a beginner, and still want to try it, then please do so on a local install.
As always, you must create a complete backup of your WordPress website before making any major changes to a live website.
Having said that, let's get started with your first custom RSS feed in WordPress.
Let's assume you want to create a new RSS feed which displays just the following information:
- Title
- Link
- Published Date
- Author
- Excerpt
First thing you need to do is create the new RSS feed in your theme's functions.php
file or in a site-specific plugin:
add_action('init', 'customRSS'); function customRSS(){ add_feed('feedname', 'customRSSFunc'); }
The above code triggers the customRSS
function, which adds the feed. 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. Make a note of the feedname, as you'll need this later on.
Once you have initialized the feed, you'll need to create the callback function to produce the required feed, using the following code in your theme's functions.php
file or in a site specific plugin:
function customRSSFunc(){ get_template_part('rss', 'feedname'); }
The code above is using the get_template_part
function to link to a separate template file, however you can also place the RSS code directly into the function. By using get_template_part
, we can keep the functionality separate to the layout. The get_template_part
function has two arguments, slug and name, that will look for a template file with the name in the following format, starting with the file at the top (if it doesn't find the first, it will move on to the second, and so on):
-
wp-content/themes/child/rss-feedname.php
-
wp-content/themes/parent/rss-feedname.php
-
wp-content/themes/child/rss.php
-
wp-content/themes/parent/rss.php
For the purposes of this tutorial, it is best to set the slug to the type of feed you're creating (in this case: rss), and the name to the feedname configured earlier on.
Once you've told WordPress to look for the feed template, you'll need to create it. The below code will produce the layout for the feed with the information we listed earlier. Save this file in your theme folder as the slug-name.php template file configured in the get_template_part
function.
<?php /** * Template Name: Custom RSS Template - Feedname */ $postCount = 5; // The number of posts to show in the feed $posts = query_posts('showposts=' . $postCount); header('Content-Type: '.feed_content_type('rss-http').'; charset='.get_option('blog_charset'), true); echo '<?xml version="1.0" encoding="'.get_option('blog_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>
This template code will generate an RSS feed following the above layout. The postCount
variable allows you to control the number of posts to display in your feed. The template can be amended as required to display whatever information you require (e.g. post images, comments, etc).
The the_excerpt_rss
function will display the excerpt of each post, and for posts that do not have excerpts, it will display the first 120 words of the post's content.
Finally, to display your feed, you'll first need to flush your WordPress rewrite rules. The easiest way to do this is by logging in to the WordPress admin, and clicking Settings -> Permalinks. Once here, just click Save Changes, which will flush the rewrite rules.
You can now access your new feed at yourdomain.com/feed/feedname
, where feedname was the feedname you gave in the add_feed
function earlier on.
The W3C offers a feed validation service, allowing you to validate the resulting feed.
Troubleshooting
- I'm getting a 404 error when trying to view my feed!
- Check to see if you are using the correct feedname in your URL. It has to be the one you supplied in the
add_feed
function - If you have the correct feedname, your rewrite rules may not have flushed correctly. Re-save your permalinks just to be sure.
- If you've re-saved your permalinks, you can force a rewrite flush via your theme's functions.php file. Add the following code to the customRSS function we created earlier. Make sure you add the code after the
add_feed
function.
global $wp_rewrite; $wp_rewrite->flush_rules();
- Check to see if you are using the correct feedname in your URL. It has to be the one you supplied in the
- Once you've added this, reload your WordPress site. NOTE: This should be removed immediately after use. Once is enough for the rules to be flushed.
- Using the W3C feed validator, specific details should be given where your feed isn't validating. Edit the feed template file to resolve these issues
- This is common where the RSS language has not been configured on your WordPress installation. To do this, you can add the following code to your theme's
functions.php
file, to update the language option.
function rssLanguage(){ update_option('rss_language', 'en'); } add_action('admin_init', 'rssLanguage');
We hope this article helped you create your own custom RSS Feeds in WordPress. Let us know how and why you will be using custom RSS feeds on your WordPress site by leaving a comment below.
Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us.
bailyfroultan1957.blogspot.com
Source: https://www.wpbeginner.com/wp-tutorials/how-to-create-custom-rss-feeds-in-wordpress/
0 Response to "How to Create Rss Feed for My Wordpress Website"
Post a Comment