Automatically add last modified date and time to your blog posts

Posted by Paul Braren on Sep 16 2011 (updated on Nov 22 2011) in
  • WebDesign
  • addlastmodifiedtoyourblog

    In Wordpress, there's no simple WordPress Plugin for newbies like me, and my chosen/purchased template doesn't have that function built in.  I just want my reader to be able to have an easy way to see when I've last made any changes to any of my posts.  I identified the need for such function over here:
    TinkerTry.com/ubsod

    So, after a bit of poking around and some edits to my postinfo.php, I believe I nailed it.  Here is my story...

    It all began with my  failed attempt at getting this 2008-vintage Addin called "Last Modified" to work the way I wanted it to:
    wordpress.org/extend/plugins/last-modified

    A bit of Googling around, and discovered a user's comments basically saying that's the old school way:
    www.blogging-girl.com/2008/08/21/wordpress-plugin-last-modified

    so I deactivated and deleted the "Last Modified" Plugin and moved onward, reading this related WordPress articles:
    codex.wordpress.org/Template_Tags/the_modified_date
    I didn't quite catch on to the tip about avoiding repeating the date/time twice, but that's OK, I like the way it is, actually, always showing the last edited time, even if no changes were made.

    I then headed over here for the date syntax:
    us3.php.net/manual/en/function.date.php

    and read about "The Loop" here:
    codex.wordpress.org/The_Loop_in_Action

    Finally, ready to break things code!

    1) Told WordPress to change my default date format

    I wanted to add the time to everywhere the date shows, adding 2 digit hours to keep columns aligned, see screenshot.

    This is done through the /wp-admin Dashboard:
    Appearance, Theme Options, General Settings, General Tab, Date format
    changing from

    M j, Y
    to
    D M d, Y h:i

    2) Next, I edited postinfo.php

    adding this 1 line (in green) to the end of postinfo.php, just before these existing last 2 lines:
    <p>Last Edited: <?php the_modified_date('M j, Y'); ?> at <?php the_modified_date('g:i a'); ?></p>
              <?php }; ?>
    </div>

    3)  Try try again, tweak until it's right!
    I then tweaked and tweaked (and broke and unbroke) my site, until I settled on this new one line, achieving decent column alignment,
    <p>-Edited <?php _e('by','Polished'); ?> <?php the_author_posts_link(); ?> on <?php the_modified_date('D M d,'); ?> <?php the_modified_date('Y h:i a'); ?></p>
              <?php }; ?>
    </div> 

    That's it, new row lined up nicely under the default theme's post details row, only there for readers who have actually clicked on an article they wish to review.  Not sure yet whether I'd want this clutter on main site, we'll see.

    PS:  Wed is much wider than Fri, and Aug is wider than Jul, so it doesn't always line up perfectly, seen below:

    addlastmodifiedtoyourblogmisaligned