So here’s something odd that I’ve just discovered over the past couple days.
(TL;DR: A WordPress plugin had a bug in the code. I successfully debugged it, just to realize that the author had done the same (in a different way) a few days ago. But I still get to feel good about what I did! Plus, I made another small change.)
Since my blog turns 20 in November of this year, I’m running daily “on this day” posts that include a list of all posts made on that date in previous years. To do this, I’ve installed the posted today plugin, and I pre-schedule the posts with a template that includes the correct shortcode (this is the body text for the Jan 2 post, using curly brackets instead of straight to avoid triggering the shortcode):
Since I'll hit 20 years of blogging this November, this year I'm posting a daily list of anything I published on this day in the past. Here are my past posts for January 2…
{postedtoday month="1" day="2"}
The posts have been showing up on my blog just fine (Jan. 1, Jan. 2)…but I noticed that they weren’t showing up in my feed in NetNewsWire, nor were they getting captured by IFTTT and pushed through to my Twitter feed, though they were showing up in my micro.blog feed (which only parses titles and links, not body content).
I verified that the post information existed in my site’s RSS feed, and included the full text of the post with all past posts from the proper date rather than just the text as shown above (so the shortcode was being processed as the RSS feed was generated). Here’s a somewhat redacted (to include only one past post entry) version of the item entry for the Jan 2 post:
<item>
<title>On This Day: Jan 2</title>
<link>https://michaelhans.com/eclecticism/2020/01/02/on-this-day-jan-2/</link>
<comments>https://michaelhans.com/eclecticism/2020/01/02/on-this-day-jan-2/#respond</comments>
<pubDate>Thu, 02 Jan 2020 16:00:47 +0000</pubDate>
<dc:creator><![CDATA[Michael Hanscom]]></dc:creator>
<category><![CDATA[blog]]></category>
<guid isPermaLink="false">https://michaelhans.com/eclecticism/?p=16448</guid>
<description><![CDATA[Recognizing 20 years of blogging, here are my past posts from January 2]]></description>
<content:encoded><![CDATA[Since I’ll hit 20 years of blogging this November, this year I’m posting a daily list of anything I published on this day in the past. Here are my past posts for January 2…
<p>There are <strong> </strong> posts previously published on January 2nd</p><ul class="todaypost"><li><strong>2019</strong><ul><li><a href="https://michaelhans.com/eclecticism/2019/01/02/12723/"></a> <span class="today_excerpt">“Sen. Romney’s statement is not a profile in courage. Rather it is another example of the emptiness of the #nevertrump movement — all talk and no action.”</span> (<a href="https://michaelhans.com/eclecticism/2019/01/02/12723/">➡</a>)</li></ul></li></ul>
]]></content:encoded>
<wfw:commentRss>https://michaelhans.com/eclecticism/2020/01/02/on-this-day-jan-2/feed/</wfw:commentRss>
<slash:comments>0</slash:comments>
</item>
I had a guess as to what was going on, but wasn’t sure…yet.
I’d noticed earlier that the “There are X posts previously published on [DATE]” line that Posted Today includes wasn’t working properly, and was omitting the number that’s supposed to be where the X is, so the line just read, “There are posts previously published on [DATE]”. I hadn’t worried too much about that just yet, filing it away into the “figure out later” bucket.
However, when looking at the XML data for my RSS feed, I noticed that there appeared to be a garbage character at that point in the data:
This made me think that there was something in the plugin that wasn’t working properly, and the garbage character may have been choking the RSS parsers that NetNewsWire and Twitter use, so they ended up discarding those items.
I took a look at the PHP code for the plugin to see if this was something I could tweak myself, and the answer seemed to be…kind of.
Here’s the plugin code that generated that line:
// get the grammar right for a result of 1
$singular = sprintf(
_x('There is <strong>1</strong> post previously published on %s', 'Single post found', 'postedtoday'),
$the_date
);
$multiple = sprintf(
_x('There are <strong>%c</strong> posts previously published on %s', 'Multiple posts found', 'postedtoday'),
$posts_from_today->found_posts,
$the_date
);
I don’t really know PHP, but it was pretty obvious that the %c
argument should be replaced by the integer for however many posts Posted Today found to display, but for some reason, was instead outputting a garbage character.
I figured I’d just tweak that line to not output a post count, and changed the code to this:
// get the grammar right for a result of 1
$singular = sprintf(
_x('There is <strong>1</strong> post previously published on %s', 'Single post found', 'postedtoday'),
$the_date
);
$multiple = sprintf(
_x('These are all the posts previously published on %s', 'Multiple posts found', 'postedtoday'),
$posts_from_today->found_posts,
$the_date
);
Unfortunately, while that fixed that error, it then resulted in the %s
argument outputting an integer instead of a date; instead of the expected “These are all the posts previously published on January 2nd”, I got, “These are all the posts published on 27”. Well, that wasn’t right…however, the “27” was the correct number of past posts for Jan. 2, and was what should have been showing up instead of the garbage character. Which got me thinking…
As it turned out, according to the PHP manual, the %c
argument “is treated as an integer and presented as the character with that ASCII” — and ASCII 27 is the ‘escape’ character! So the plugin was checking to see how many posts were made, coming up with 27, and dropping the number in the right place–but was using the incorrect argument for PHP’s sprintf
function, so that instead of outputting the integer, it was outputting an escape character. Similarly, for my Jan. 1 entry, it came up with 26 posts, and ASCII 26 is the ‘substitute’ character, so that also acted as a garbage character in the XML RSS feed.
With that in mind, I tweaked the code to be this:
// get the grammar right for a result of 1
$singular = sprintf(
_x('There is <strong>1</strong> post previously published on %s', 'Single post found', 'postedtoday'),
$the_date
);
$multiple = sprintf(
_x('There are <strong>%s</strong> posts previously published on %s', 'Multiple posts found', 'postedtoday'),
$posts_from_today->found_posts,
$the_date
);
And just like magic, it worked!
Well, at least, I now get the correct phrase in my post: “There are 27 posts previously published on January 2nd”. I’ll find out the next time NetNewsWire refreshes as to whether this fixes my RSS feed so that those posts aren’t mysteriously disappearing. Fingers crossed!
Update: The mysteriously missing posts have appeared in my feed in NetNewsWire. Success!
And then, after going through all this and writing it up, I realized that the plugin’s author had updated the plugin to correct for this error three days ago, and all I really needed to do was upload the latest version of the plugin. So I’m amused, but I also get to feel accomplished for successfully debugging and solving the problem, so yay!
Plus, I’ve made one other tweak to the plugin, so that it adds a link to the end of the excerpt to better handle “microblog” style entries that don’t have titles, so I still get to feel good about that part, as well. :) My coding skills may be underdeveloped and rusty from lack of regular use, but they’re not entirely atrophied!
Here’s the change I made to accomplish that:
Original code:
// display excerpt if we want it
if ( $excerpt ) $output .= ' <span class="today_excerpt">' . get_the_excerpt() . '</span>';
$output .= '</li>';
My code:
// display excerpt if we want it
if ( $excerpt ) $output .= ' <span class="today_excerpt">' . get_the_excerpt() . '</span>';
$output .= ' (<a href="' . get_permalink() . '">➡</a>)</li>';
Mischief managed!