An "on this day" plugin for WordPress


Just me scratching my own itch. I want to create an RSS feed of all the blog posts which I'd published on this day in the past.

For example, if today is 21st of November 2016 then this plugin will show blog posts written on

  • 2015-11-21
  • 2014-11-21
  • 2013-11-21

And so on.

You can view a demo at https://shkspr.mobi/blog/?on_this_day

Screenshot of an RSS feed

Code

The code is pretty simple. The WordPress API is fairly easy to get on with.

<?php
/*
Plugin Name: OnThisDay
Description: RSS feed of posts which occurred on this day in the past
*/
//  RSS of today's historic blog posts
//  Visible at example.com/?on_this_day
add_action( 'init', 'edent_on_this_day' );
function edent_on_this_day()
{
      if( isset( $_GET['on_this_day'] ) ) {
        $today = getdate();
        $args = array(
           'date_query' => array(
              array(
                 'month' => $today['mon'],
                 'day'   => $today['mday'],
              ),
           ),
        );
        $query = new WP_Query( $args );
        rss_encode($query);
        die();
    }
}
function rss_encode($data) {
    $today = getdate();
    $pubDate =  date("D, d M Y") . " 00:00:00 GMT";
    $rss = '<?xml version="1.0" encoding="UTF-8" ?>
            <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
            <channel>
                   <title>On This Day</title>
                   <description></description>
                   <link>' . site_url() . '/?on_this_day</link>
                   <language>en-gb</language>';
    $posts = $data->get_posts();
    foreach($posts as $post) {
        // Do your stuff, e.g.
        $title= $post->post_title;
        $id  = $post->ID;
        $link = get_permalink($id);
        $date = $post->post_date;
        $postDate = date("D, d M Y") . " " . date("h:i:s O", strtotime($date));
        $thumb = get_the_post_thumbnail($id, 'full');
        $archive = "From the " . date("F Y", strtotime($date)) . " archives: ";
        //  Only add an item if it is before *this year*
        //  AND before the current hour (prevents suddenly adding loads of posts)
        if (
              ( intval(date("Y", strtotime($date))) <  intval($today['year'] ) ) &&
              ( intval(date("H", strtotime($date))) <= intval($today['hours']) )
        ) {
                $rss .= '<item>
                   <title>'  .html_entity_decode($archive . $title).'</title>
                   <link>'   .htmlspecialchars($link).'</link>
                   <description>
                      <![CDATA['.$thumb.']]>
                   </description>
                   <pubDate>'.$postDate.'</pubDate>
                   <guid>'   .htmlspecialchars($link).'</guid>
                </item>';
        }
    }
    $rss .= '</channel>
    </rss>';
    header('Content-Type: application/rss+xml');
    echo $rss;
}

The latest versions of the source code is at https://github.com/edent/WordPress-On-This-Day-Plugin

Limitations

Times

The entries in the RSS feed are generated on an hourly basis.

For example, if the time is 1300 the RSS feed will only show blog posts published on or before 1300 on their original publication day.

This allows services like IFTTT to post the feed to Twitter throughout the day rather than all at once.

Location

This plugin lives at https://example.com/?on_this_day. The results are not cached.

Images

The thumbnail from the post is also included. If there is no thumbnail, services like IFTTT might misbehave:

Times and Locale

The RSS feed is hardcoded to GMT and en-GB - this may change in the future.

Posting

I'm using IFTTT to post to Twitter. Ideally, I'd use JetPack Publicize to syndicate it to Facebook, LinkedIn, and others.

Using

I'm not releasing this into the WordPress plugin ecosystem because their system confuses me. You can install it in /wp-content/plugins/onthisday/ and then activate it as normal.

You can download the plugin from GitHub


Share this post on…

What are your reckons?

All comments are moderated and may not be published immediately. Your email address will not be published.Allowed HTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <p> <pre> <br> <img src="" alt="" title="" srcset="">