Skip to content

Instantly share code, notes, and snippets.

@urre
Created December 14, 2019 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save urre/08b0848ae0ef1015030b21e946bcee4c to your computer and use it in GitHub Desktop.
Save urre/08b0848ae0ef1015030b21e946bcee4c to your computer and use it in GitHub Desktop.
WordPress Custom RSS feed
<?php
/*
Plugin Name: Custom RSS Feed for App
Description: A plugin creating a customized rssfeed
Plugin URI: https://urre.me/
Author: Urban Sandén
Author URI: https://urre.me
*/
// Add dates for rss feed
function customfeed_rss_date( $timestamp = null ) {
$timestamp = ($timestamp==null) ? time() : $timestamp;
echo date(DATE_RSS, $timestamp);
}
// Add endpoint and specify EP mask
function customfeed_add_endpoint() {
add_rewrite_endpoint( 'rssfeed', EP_PERMALINK | EP_PAGES );
}
add_action( 'init', 'customfeed_add_endpoint' );
// Check if /rssfeed is used
function customfeed_template_redirect() {
global $wp_query;
if ( ! isset( $wp_query->query_vars['rssfeed'] ) )
return;
customfeed_output_feed();
exit;
}
add_action( 'template_redirect', 'customfeed_template_redirect' );
// Output rss feed
function customfeed_output_feed() {
$postCount = 10;
$postType = 'post';
query_posts( array( 'post_type' => $postType, '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>
<link><?php bloginfo_rss('url') ?></link>
<atom:link href="http://dallas.example.com/rss.xml" rel="self" type="application/rss+xml" />
<description>Nyheter från <?php echo get_bloginfo('name'); ?></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', '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(); ?>
<?php
$image_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'large' );
$image_alt = get_post_meta( get_post_thumbnail_id( $post->ID ), '_wp_attachment_image_alt', true );
$posttags = get_the_tags($post->ID);
?>
<item>
<title><?php echo html_entity_decode(get_the_title($post->ID)); ?></title>
<link><?php echo get_permalink($post->ID); ?></link>
<description>
<?php if ( has_post_thumbnail( $post->ID ) ) :
echo '<![CDATA[<img src="' . $image_src[0] . '" height="600" width="1024" alt="' . $image_alt . '">]]>';
endif; ?>
<?php echo '<![CDATA[<p>Etiketter: </p>]]>'; ?>
<?php if ($posttags) : foreach($posttags as $tag) : echo '<![CDATA[<a href="' . get_tag_link($tag->term_id) . '">' . $tag->name . '</a>]]>'; endforeach; endif; ?>
</description>
<content:encoded><![CDATA[<?php echo the_content() ?>]]></content:encoded>
<?php rss_enclosure(); ?>
<?php do_action('rss2_item'); ?>
<pubDate><?php customfeed_rss_date( strtotime($post->post_date_gmt) ); ?></pubDate>
<dc:creator><?php echo get_the_author($post->ID); ?></dc:creator>
<guid><?php echo get_permalink($post->ID); ?></guid>
</item>
<?php endwhile; ?>
</channel>
</rss>
<?php
}
function customfeed_activate() {
customfeed_add_endpoint();
flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'customfeed_activate' );
function customfeed_deactivate() {
flush_rewrite_rules();
}
register_deactivation_hook( __FILE__, 'customfeed_deactivate' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment