I really dig Lester Chan’s very popular WordPress plugin WP-RelativeDate. WordPress itself, through the <?php the_date() ?>
and <?php the_time() ?>
tags, restricts posts to showing only the exact date and time of publication (e.g. “Jan 30, 2009 at 9:15 AM PST”). WP-RelativeDate, however, can replace “Jan 30, 2009 at 9:15 AM PST” with “Jan 30, 2009 at 9:15 AM PST, 10 minutes ago” or “Jan 30, 2009 at 9:15 AM PST, yesterday”. It’s wonderful to have relative dates that let visitors know not only when a post was published, but also how long ago.
Unfortunately, WP-RelativeDate always lists the entire date and relative date or time. You can’t, for instance, display only “10 minutes ago” or “yesterday” as with more recently built blogging and micro-blogging systems–Twitter, for instance. WP-RelativeDate is hardcoded to insert both. Also, after a few hours since the post was published, WP-RelativeDate begins displaying “Today” instead of “X hours ago”. Neither of those characteristics fit with my sites’ needs, so I hacked Lester’s plugin. With just two lines of modification I stripped its output down to only the relative date or time. You can, too. Here’s how:
- Download the latest version of WP-RelativeDate
- Unzip the archive and open wp-relativedate.php in your HTML or text editor (Notepad or TextEdit will work just fine).
- Scroll down to the line that reads:
### Alternative To WordPress the_date().
. The line below that, which begins withfunction relative_post_the_date
, is what we need to change. - Replace:
function relative_post_the_date($d = '', $before = '', $after = '', $display_ago_only = false, $display = true)
With these three lines:
function relative_post_the_date($d = '', $before = '(', $after = ')', $display_ago_only = true, $display = true) {
/* PSB Hack: replaced below line with above */
/*function relative_post_the_date($d = '', $before = '', $after = '', $display_ago_only = false, $display = true) {*/Note that the original code line is still there, just commented out. I encourage hacking others’ plugins this way so that, when troubleshooting the hack or when the original plugin is updated, you can easily find what was modified. Of course, you might want to replace my initials with your own.
- About a baker’s dozen lines beneath that is the second modification to make. Replace:
$output = $before.__('Today', 'wp-relativedate').$after;
With:
$output = '';
/* PSB Hack: replaced below line with above */
/* $output = $before.__('Today', 'wp-relativedate').$after;*/
- That’s it for the code hacking, but as an added precaution I recommend you scroll back to the top of the file and add
HACKED Remember to reapply hacks if upgraded.
right afterDescription:
. That will add the helpful reminder–in bold–to the WP-RelativeDate description box on your WordPress Plugin Management page. Thus, when Lester releases a new version of his plugin and the Plugin Management asks you to upgrade, you know that you’ll have to make the same changes in the new version (unless Lester changes it to include my hacks). - Save wp-relativedate.php and install it into your WordPress site as usual–upload the entire wp-relativedate folder to /wp-content/plugins/ and activate the plugin.
In your templates use the <?php relative_post_the_date(); ?>
to insert just the relative date in place of, or in combination with, the standard <?php the_date() ?>
and <?php the_time() ?>
tags.
awesome hack Pariah. thanks alot! putting it to good use.