Greg Rickaby

Greg Rickaby

Full-Stack Engineer / Photographer / Author

Create a custom RSS feed for WordPress

Posted on | 2 minute read

Disney World - May 2022

Creating a custom RSS feed is pretty easy. I’ll show you have to do it in three steps. Here’s a pro-tip: browsers heavily cache RSS feeds. While developing, try using Incognito Mode.

1) Register the template

Place the following into functions.php

/**
 * Register custom RSS feed.
 * 
 * Note: This is registering a feed named, "custom".
 */
function grd_register_rss_feed() {
	add_feed( 'custom', 'grd_get_custom_rss_template' );
}
add_action( 'after_setup_theme', 'grd_rss_template' );

/**
 * Get the custom RSS template.
 *
 * Note: This is fetching a template part, named "feed-custom.php"
 */
function grd_get_custom_rss_template() {
	get_template_part( 'feed', 'custom' );
}

2) Create the template file

Create a new file, Name it feed-custom.php and place it inside your theme’s root directory.

<?php
/**
 * Customs RSS template with related posts.
 *
 * Place this file in your theme's directory.
 *
 * @package sometheme
 * @subpackage theme
 */

header( 'Content-Type: ' . feed_content_type( 'rss-http' ) . '; charset=' . get_option( 'blog_charset' ), true );
$frequency  = 1;        // Default '1'. The frequency of RSS updates within the update period.
$duration   = 'hourly'; // Default 'hourly'. Accepts 'hourly', 'daily', 'weekly', 'monthly', 'yearly'.
$postlink   = '<br /><a href="' . get_permalink() . '">See the rest of the story at mysite.com</a><br /><br />';
$email      = get_the_author_meta( 'email');
$author     = get_the_author();
$postimages = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'large' );

// Check for post image. If none, fallback to a default.
$postimage = ( $postimages ) ? $postimages[0] : get_stylesheet_directory_uri() . '/images/default.jpg';

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' ); ?>
>

	<!-- RSS feed defaults -->
	<channel>
		<title><?php bloginfo_rss( 'name' ); wp_title_rss(); ?></title>
		<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 bloginfo_rss( 'language' ); ?></language>
		<sy:updatePeriod><?php echo apply_filters( 'rss_update_period', $duration ); ?></sy:updatePeriod>
		<sy:updateFrequency><?php echo apply_filters( 'rss_update_frequency', $frequency ); ?></sy:updateFrequency>
		<atom:link href="<?php self_link(); ?>" rel="self" type="application/rss+xml" />

		<!-- Feed Logo (optional) -->
		<image>
			<url>https://mysite.com/somelogo.png</url>
			<title>
				<?php bloginfo_rss( 'description' ) ?>
			</title>
			<link><?php bloginfo_rss( 'url' ) ?></link>
		</image>

		<?php do_action( 'rss2_head' ); ?>

		<!-- Start loop -->
		<?php while( have_posts() ) : the_post(); ?>

			<item>
				<title><?php the_title_rss(); ?></title>
				<link><?php the_permalink_rss(); ?></link>
				<guid isPermaLink="false"><?php the_guid(); ?></guid>
				<author><?php echo $email ?><?php echo ' (' . $author . ')' ?></author>
				<image>
					<url><?php echo esc_url( $postimage ); ?>"/></url>
				</image>
				<pubDate><?php echo mysql2date( 'D, d M Y H:i:s +0000', get_post_time( 'Y-m-d H:i:s', true ), false ); ?></pubDate>

				<!-- Echo content and related posts -->
				<content:encoded>
					<![CDATA[<?php echo the_excerpt_rss(); echo $postlink; ?>]]>
				</content:encoded>
			</item>

		<?php endwhile; ?>
		<!-- End loop -->
	</channel>
</rss>

3) Flush Permalinks

Now your custom RSS feed can be found at
http://yoursite.com?feed=custom

Comments

No comments yet.

Creating Custom WordPress RSS Feeds - WP RSS Aggregator

[…] Greg Rickaby – Custom RSS Template in WordPress […]

Raylo (@raylonet)

Raylo (@raylonet)

simple stuff that works, thanks for the help

Said Erraoudy

Said Erraoudy

thanks alot bro (y) !

lettjacob

lettjacob

Thank you for taking the time to share this. Works great!

zaheerabbasaghani

zaheerabbasaghani

Thank u very much

Saiful

Saiful

It ‘s nice to do with custom Feeds.
Thanks a lot.

Tutorial: Help! My RSS Campaign is ugly and the preview is wrong.

[…] to Greg Rickaby for the alternative RSS template tutorial. You still have to read the code to change it to your […]

Leave a Comment