WordPress

WordPress: Custom RSS Feed

There’s three main steps to create your custom RSS Feed.

  1. Register your feed in functions.php

    add_action( 'after_setup_theme', 'custom_rss' );
    /**
     * Register custom RSS template.
     */
    function custom_rss() {
    	add_feed( 'custom', 'render_custom_css' );
    }
    /**
     * Custom RSS template callback.
     */
    function render_custom_css() {
    	get_template_part( 'feed', 'custom' );
    }
    
  2. Create your template file feed-custom.php in your theme folder.
    You can copy and paste from wp-includes/feed-rss2.php to make things easier and make changes something like the following – I fetch post with post_type is ajde_events and is it a featured event:

    <?php
    /**
     * RSS2 Feed Template for displaying RSS2 Posts feed.
     *
     * @package WordPress
     */
    
    header('Content-Type: ' . feed_content_type('rss-http') . '; charset=' . get_option('blog_charset'), true);
    $more = 1;
    
    echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'>';
    
    /**
     * Fires between the xml and rss tags in a feed.
     *
     * @since 4.0.0
     *
     * @param string $context Type of feed. Possible values include 'rss2', 'rss2-comments',
     *                        'rdf', 'atom', and 'atom-comments'.
     */
    do_action( 'rss_tag_pre', 'rss2' );
    ?>
    <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
    	/**
    	 * Fires at the end of the RSS root to add namespaces.
    	 *
    	 * @since 2.0.0
    	 */
    	do_action( 'rss2_ns' );
    	?>
    >
    
    <channel>
    	<title><?php bloginfo_rss('name'); wp_title_rss(); ?></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 bloginfo_rss( 'language' ); ?></language>
    	<sy:updatePeriod><?php
    		$duration = 'hourly';
    
    		/**
    		 * Filter how often to update the RSS feed.
    		 *
    		 * @since 2.1.0
    		 *
    		 * @param string $duration The update period. Accepts 'hourly', 'daily', 'weekly', 'monthly',
    		 *                         'yearly'. Default 'hourly'.
    		 */
    		echo apply_filters( 'rss_update_period', $duration );
    	?></sy:updatePeriod>
    	<sy:updateFrequency><?php
    		$frequency = '1';
    
    		/**
    		 * Filter the RSS update frequency.
    		 *
    		 * @since 2.1.0
    		 *
    		 * @param string $frequency An integer passed as a string representing the frequency
    		 *                          of RSS updates within the update period. Default '1'.
    		 */
    		echo apply_filters( 'rss_update_frequency', $frequency );
    	?></sy:updateFrequency>
    
    	<?php 
    	$args = array(
    			'meta_key' => '_featured',
    			'meta_value' => 'yes',
    			'post_type' => 'ajde_events',
    			'post_status' => 'publish'
    		);
    		$the_query = new WP_Query( $args );
    	?>
    	<?php do_action( 'rss2_head'); ?>
    	<?php if ( $the_query->have_posts() ) : ?>
    		<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    			<item>
    				<title><?php the_title(); ?></title>
    				<link><?php the_permalink_rss() ?></link>
    				<comments><?php comments_link_feed(); ?></comments>
    				<pubDate><?php echo mysql2date('D, d M Y H:i:s +0800', get_post_time('Y-m-d H:i:s', true), false); ?></pubDate>
    				<dc:creator><![CDATA[<?php the_author() ?>]]></dc:creator>
    				<?php the_category_rss('rss2') ?>
    
    				<guid isPermaLink="false"><?php the_guid(); ?></guid>
    				<?php if (get_option('rss_use_excerpt')) : ?>
    						<description><![CDATA[<?php the_excerpt_rss(); ?>]]></description>
    				<?php else : ?>
    						<description><![CDATA[<?php the_excerpt_rss(); ?>]]></description>
    					<?php $content = get_the_content_feed('rss2'); ?>
    					<?php if ( strlen( $content ) > 0 ) : ?>
    						<content:encoded><![CDATA[<?php echo $content; ?>]]></content:encoded>
    					<?php else : ?>
    						<content:encoded><![CDATA[<?php the_excerpt_rss(); ?>]]></content:encoded>
    					<?php endif; ?>
    				<?php endif; ?>
    				<wfw:commentRss><?php echo esc_url( get_post_comments_feed_link(null, 'rss2') ); ?></wfw:commentRss>
    				<slash:comments><?php echo get_comments_number(); ?></slash:comments>
    				<?php rss_enclosure(); ?>
    				<?php do_action( 'rss2_item' ); ?>
    			</item>
    		<?php endwhile; ?>
    
    		<?php wp_reset_postdata(); ?>
    	<?php else : ?>
    		
    	<?php endif; ?>
    </channel>
    </rss>
    
  3. Basically you are done and you can go to your WordPress site http://yourblog.com?feed=custom. You may need add the following in order to flush your RSS Feed Cache – it’s a headache to wait for an hour to get your RSS Feed updated – change it to a second!
    /**
     * Setting a new cache time for feeds in WordPress
     */
    function prefix_set_feed_cache_time( $seconds ) {
    	return 1;
    }
    add_filter( 'wp_feed_cache_transient_lifetime' , 'prefix_set_feed_cache_time' );
    

P/S: You can customize your output by adding a bit of styling if you wanted to!

Leave a Reply

Your email address will not be published. Required fields are marked *

twenty − 17 =