Skip to content

Instantly share code, notes, and snippets.

@Dan0sz
Last active March 11, 2020 08:59
Show Gist options
  • Save Dan0sz/53d7aa1f2d0e9a9b4bce12e45d082d04 to your computer and use it in GitHub Desktop.
Save Dan0sz/53d7aa1f2d0e9a9b4bce12e45d082d04 to your computer and use it in GitHub Desktop.
Add Date Last Modified next to Published Date in WordPress Posts.
<?php
/**
* Plugin Name: Date Last Modified for WordPress Posts
* Plugin URI: https://daan.dev/wordpress/date-last-updated-modified/
* Description: Display 'Last Updated' date along with 'Date Published' in WordPress.
* Version: 1.0.0
* Author: Daan van den Bergh
* Author URI: https://daan.dev
* License: GPL2v2 or later
* Text Domain: daan-date-last-modified
*/
function daan_add_date_last_modified($date_published)
{
global $post;
$post_id = get_queried_object_id();
// Make sure to only apply this filter on the current post, and not 'recent posts' widgets, archive pages, etc.
if ($post_id != $post->ID) {
return $date_published;
}
/**
* get_post_datetime() return datetime-object which can be compared, formatted, modified, etc.
*/
$modified_datetime = get_post_datetime($post->ID, 'modified');
$published_datetime = get_post_datetime($post->ID);
/**
* Exit if Date Last Modified is equal to or smaller than Date Published.
*/
if ($modified_datetime <= $published_datetime) {
return $date_published;
}
$text_before_date = __('Updated', 'daan-date-last-modified');
$before = '(' . $text_before_date . ': ';
$after = ')';
$modified_ymd = $modified_datetime->format('Y-m-d');
$modified_human_readable = $modified_datetime->format(get_option('date_format'));
$updated_html = $before . sprintf(
'<time class="entry-date" datetime="%s" itemprop="dateModified">%s</time>',
esc_attr($modified_ymd),
esc_attr($modified_human_readable)
) . $after;
return $date_published . ' ' . $updated_html;
}
// ATTENTION: The first parameter in add_filter() is the filter's name. This might differ per theme.
add_filter('the_time', 'daan_add_date_last_modified', null, 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment