Creative Problem Solver. Programmer. Bodysurfing. Sometime Comics.
Blogger since 2001.

own yr www rn! #IndieWeb

Have I ever posted on Leap Day?

I genuinely don’t know. There have been 5 since I started the site. 2004. 2008. 2012. 2016. 2020.

And today is Leap Day and I already posted today.

Have I ever posted on Leap Day?

Let’s write a WordPress shortcode and find out!

add_shortcode('on_this_day',
/**
 * Expect [on_this_day month="February" day="29"]
 * Both month and day are required
 * And return an ordered list of posts from that day grouped by year
 * @param $atts
 * @param $content
 * @param $tag
 */ function ( $atts, $content, $tag ) {
    $atts = shortcode_atts([
       'month' => '',
       'day' => '',
    ], $atts, $tag);
    $month = $atts['month'];
    $day = $atts['day'];
    if (empty($month) || empty($day)) {
       return;
    }
    // if month is non-numeric, convert it to a number
    if (!is_numeric($month)) {
       $month = date('m', strtotime($month));
    }
    // and the same with the day...
    if (!is_numeric($day)) {
       $day = date('d', strtotime($day));
    }
    $args = [
       'post_type' => 'post',
       'post_status' => 'publish',
       'posts_per_page' => -1,
       'date_query' => [
          'month' => $month,
          'day' => $day,
       ],
       'orderby' => 'date',
       'order' => 'ASC',
    ];
    $posts = get_posts($args);
    $posts_by_year = [];
    foreach ($posts as $post) {
       $year = get_the_date('Y', $post);
       $posts_by_year[$year][] = $post;
    }
    $output = '';
    foreach ($posts_by_year as $year => $year_posts) {
       $output .= sprintf('<dt>%s</dt>', $year);
       foreach ($year_posts as $post) {
          $output .= sprintf('<dd><a href="%s">%s</a></dd>',
             get_permalink($post->ID),
             get_the_title($post)
          );
       }
    }
    return '<dl class="on-this-day" >' . $output . '</dl>';

});

I add that code to my theme. And now, when I add to a page or post with a shortcode that specifies a month and a day, like so…

[on_this_day month="February" day="29"]

…I get output like this:

2008
Mr. Bird
2024
Gravatar WordPress verification for self-hosted WordPress
Have I ever posted on Leap Day?

So before today, I posted once on 29 February. In 2008. I’m surprised! That was fun.


Since it’s Leap Day it’s also Delete Your Drafts Day. I deleted 3 lingering drafts I will not miss. I turned 1 into a post which will publish tomorrow. So there are 4 drafts, which is good enough for me. I wrote about my odd schedule for checking on drafts last week if you care to read it.


Happy Leap Day!

Unrelated goofiness: ‘30 Rock’ Co-Creator Tells The Story Behind Leap Day Williams Watch the weirdness and check it out on Nestflix.


P.S. is there a WordPress shortcode you wish existed? What would it be?

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This post accepts webmentions. If you link to and write about this on your website, enter the web address of your post below: