k-isabelle
5/31/2018 - 4:29 PM

Wordpress - Create Custom RSS Feeds - Display RSS feeds - Set RSS Cache Expiry / Cache Lifetime

Wordpress - Create Custom RSS Feeds - Display RSS feeds - Set RSS Cache Expiry / Cache Lifetime

/*----------------------------------------------*/
/*   Feed Cache Refresh Rate
/*----------------------------------------------*/
// http://www.village-idiot.org/archives/2009/08/04/fix-the-rss-update-time-on-the-wordpress-rss-widget/

// By default Wordpress checks for updates in RSS feeds only once every 12 hours.
// This code will cause the feeds to be checked every 30 minutes. Add to functions.php
// 1800 seconds = 30 min.

add_filter( 'wp_feed_cache_transient_lifetime', create_function('$a', 'return 1800;') );




/*----------------------------------------------*/
/*   Create A Custom Feed
/*----------------------------------------------*/
// http://www.wpbeginner.com/wp-tutorials/how-to-create-custom-rss-feeds-in-wordpress/

// 1. Declare the new feed in functions.php + indicate which template file to use

add_action('init', 'customRSS');
function customRSS(){
        add_feed('feedname', 'customRSSFunc');
}

function customRSSFunc(){
        get_template_part('rss', 'feedname');
}


// 2. Create template file 'rss-feedname.php'

<?php
/**
 * Template Name: Custom RSS Template - Feedname
 */
$postCount = 5; // The number of posts to show in the feed
// $posts = query_posts('showposts=' . $postCount);
$my_query = new WP_query( 
                    array(
                      'posts_per_page'   =>    -1,
                      'meta_query'       => array(
                          array(
                              'key' => 'featured_on_bunt',
                              'value' => '"in_cat"', 
                              'compare' => 'LIKE'
                            )
                        )
                      )
                    );


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 if ($my_query->have_posts()): ?>
          <?php while ($my_query->have_posts()) : $my_query->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; ?>
        <?php endif; ?>
</channel>
</rss>




/*----------------------------------------------*/
/*   Display a Feed in Wordpress
/*----------------------------------------------*/
// http://www.wpbeginner.com/wp-tutorials/how-to-display-any-rss-feed-on-your-wordpress-blog/

<h2><?php _e( 'Recent news from Some-Other Blog:', 'my-text-domain' ); ?></h2>

<?php // Get RSS Feed(s)
include_once( ABSPATH . WPINC . '/feed.php' );

// Get a SimplePie feed object from the specified feed source.
$rss = fetch_feed( 'http://www.wpbeginner.com/feed/' );

if ( ! is_wp_error( $rss ) ) : // Checks that the object is created correctly

    // Figure out how many total items there are, but limit it to 5. 
    $maxitems = $rss->get_item_quantity( 5 ); 

    // Build an array of all the items, starting with element 0 (first element).
    $rss_items = $rss->get_items( 0, $maxitems );

endif;
?>

<ul>
    <?php if ( $maxitems == 0 ) : ?>
        <li><?php _e( 'No items', 'my-text-domain' ); ?></li>
    <?php else : ?>
        <?php // Loop through each feed item and display each item as a hyperlink. ?>
        <?php foreach ( $rss_items as $item ) : ?>
            <li>
                <a href="<?php echo esc_url( $item->get_permalink() ); ?>"
                    title="<?php printf( __( 'Posted %s', 'my-text-domain' ), $item->get_date('j F Y | g:i a') ); ?>">
                    <?php echo esc_html( $item->get_title() ); ?>
                </a>
            </li>
        <?php endforeach; ?>
    <?php endif; ?>
</ul>