Hack WP-RelativeDate to Show ONLY Relative Date and Time

I real­ly dig Lester Chan’s very pop­u­lar WordPress plu­g­in WP-RelativeDate. WordPress itself, through the <?php the_date() ?> and <?php the_time() ?> tags, restricts posts to show­ing only the exact date and time of pub­li­ca­tion (e.g. “Jan 30, 2009 at 9:15 AM PST”). WP-RelativeDate, how­ev­er, can replace “Jan 30, 2009 at 9:15 AM PST” with “Jan 30, 2009 at 9:15 AM PST, 10 min­utes ago” or “Jan 30, 2009 at 9:15 AM PST, yes­ter­day”. It’s won­der­ful to have rel­a­tive dates that let vis­i­tors know not only when a post was pub­lished, but also how long ago.

Unfortunately, WP-RelativeDate always lists the entire date and rel­a­tive date or time. You can’t, for instance, dis­play only “10 min­utes ago” or “yes­ter­day” as with more recent­ly built blog­ging and micro-blogging systems–Twitter, for instance. WP-RelativeDate is hard­cod­ed to insert both. Also, after a few hours since the post was pub­lished, WP-RelativeDate begins dis­play­ing “Today” instead of “X hours ago”. Neither of those char­ac­ter­is­tics fit with my sites’ needs, so I hacked Lester’s plu­g­in. With just two lines of mod­i­fi­ca­tion I stripped its out­put down to only the rel­a­tive date or time. You can, too. Here’s how:

  1. Download the lat­est ver­sion of WP-RelativeDate
  2. Unzip the archive and open wp-relativedate.php in your HTML or text edi­tor (Notepad or TextEdit will work just fine).
  3. Scroll down to the line that reads: ### Alternative To WordPress the_date().. The line below that, which begins with function relative_post_the_date, is what we need to change.
  4. 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 orig­i­nal code line is still there, just com­ment­ed out. I encour­age hack­ing oth­ers’ plu­g­ins this way so that, when trou­bleshoot­ing the hack or when the orig­i­nal plu­g­in is updat­ed, you can eas­i­ly find what was mod­i­fied. Of course, you might want to replace my ini­tials with your own.

  5. About a bak­er’s dozen lines beneath that is the sec­ond mod­i­fi­ca­tion to make. Replace:

    $output = $before.__('Today', 'wp-relativedate').$after;

    With:

    $output = '';
    /* PSB Hack: replaced below line with above */
    /* $output = $before.__('Today', 'wp-relativedate').$after;*/

  6. That’s it for the code hack­ing, but as an added pre­cau­tion I rec­om­mend you scroll back to the top of the file and add HACKED Remember to reapply hacks if upgraded. right after Description: . That will add the help­ful reminder–in bold–to the WP-RelativeDate descrip­tion box on your WordPress Plugin Management page. Thus, when Lester releas­es a new ver­sion of his plu­g­in and the Plugin Management asks you to upgrade, you know that you’ll have to make the same changes in the new ver­sion (unless Lester changes it to include my hacks).
  7. Save wp-relativedate.php and install it into your WordPress site as usual–upload the entire wp-relativedate fold­er to /wp-content/plugins/ and acti­vate the plugin.

In your tem­plates use the <?php relative_post_the_date(); ?> to insert just the rel­a­tive date in place of, or in com­bi­na­tion with, the stan­dard <?php the_date() ?> and <?php the_time() ?> tags.

1 thought on “Hack WP-RelativeDate to Show ONLY Relative Date and Time

  1. iamcracks

    awe­some hack Pariah. thanks alot! putting it to good use.

Comments are closed.