Skip to content

Instantly share code, notes, and snippets.

@seezee
Last active November 25, 2022 22:28
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seezee/606b015926643d861918407a31f04756 to your computer and use it in GitHub Desktop.
Save seezee/606b015926643d861918407a31f04756 to your computer and use it in GitHub Desktop.
Customized WordPress RSS Feed. REPLACE the value of the variable $date before employing!
<?php
/**
* RSS2 Feed Template for displaying RSS2 Posts feed.
* Adds an offset of "1" to display all but most recent
*
* Full details at:
* https://wordimpress.com/anatomy-advanced-wordpress-blog-notification-email
*
* @package Your Package Name
*/
/**
* TO USE:
* 1. Update the package name
* 2. Update the value for the $date variable
* 3. Save changes and place this file in the your theme's root folder
* 4. Create a new page; this will be the custom feed page
* 5. On the new page, go to Page Attribute > Template & use the dropdown to select
* the Feed Offset template (this template)
* 6. Publish the new page
*/
// Security.
if ( ! defined( 'ABSPATH' ) ) {
die( 'Sorry, you are not allowed to access this page directly.' );
}
header( 'Content-Type: ' . feed_content_type( 'rss-http' ) . '; charset=' . get_option( 'blog_charset' ), true );
$arr = array(); // For wp_kses.
$date = "PUT YOUR COPYRIGHT START DATE HERE";
echo '<?xml version="1.0" encoding="' . wp_kses( get_option( 'blog_charset' ), $arr ) . '"?' . '>';
/**
* 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>
<language>en-us</language>
<title>
<?php
bloginfo_rss( 'name' );
echo ' ';
wp_title_rss();
?>
</title>
<copyright>Copyright ©<?php echo $date; ?> — <?php echo date('Y'); ?> <?php bloginfo( 'name' ); ?>. All rights reserved.</copyright>
<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 wp_kses( mysql2date( 'D, d M Y H:i:s +0000', get_lastpostmodified( 'GMT' ), false ), $arr ); ?></lastBuildDate>
<language><?php bloginfo_rss( 'language' ); ?></language>
<?php
$duration = 'hourly';
/**
* Filter how often to update the RSS feed.
*
* @since 2.1.0
*
* @param string $duration The update period.
* Default 'hourly'. Accepts 'hourly', 'daily', 'weekly', 'monthly', 'yearly'.
*/
?>
<sy:updatePeriod><?php echo wp_kses( apply_filters( 'rss_update_period', $duration ), $arr ); ?></sy:updatePeriod>
<?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'.
*/
?>
<sy:updateFrequency><?php echo wp_kses( apply_filters( 'rss_update_frequency', $frequency ), $arr ); ?></sy:updateFrequency>
<?php
/**
* Fires at the end of the RSS2 Feed Header.
*
* @since 2.0.0
*/
do_action( 'rss2_head' );
$args = array(
'offset' => 1,
);
// Custom query.
$query = new WP_Query( $args );
// Check that we have query results.
if ( $query->have_posts() ) {
// Start looping over the query results.
while ( $query->have_posts() ) :
$query->the_post();
?>
<item>
<title><?php the_title_rss(); ?></title>
<link><?php the_permalink_rss(); ?></link>
<comments><?php comments_link_feed(); ?></comments>
<pubDate><?php echo wp_kses( mysql2date( 'D, d M Y H:i:s +0000', get_post_time( 'Y-m-d H:i:s', true ), false ), $arr ); ?></pubDate>
<dc:creator>
<![CDATA[<?php the_author(); ?>]]>
</dc:creator>
<?php the_category_rss( 'rss2' ); ?>
<guid isPermaLink="false"><?php the_guid(); ?></guid>
<description>
<![CDATA[<?php wp_strip_all_tags( the_excerpt() ); ?>]]>
</description>
<content:encoded>
<![CDATA[<?php wp_strip_all_tags( the_excerpt() ); ?>]]>
</content:encoded>
<?php rss_enclosure(); ?>
<?php
/**
* Fires at the end of each RSS2 feed item.
*
* @since 2.0.0
*/
do_action( 'rss2_item' );
?>
</item>
<?php
endwhile;
}
// Restore original post data.
wp_reset_postdata();
?>
</channel>
</rss>
<?php
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment