Last active
January 4, 2022 11:32
-
-
Save geoffreycrofte/50f65c6f2f77cd9e574f3eb6552c4f5e to your computer and use it in GitHub Desktop.
Save and display the last modified post date in WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Adds a timestamp in the first blog option. | |
* @return void | |
*/ | |
function juiz_update_last_edit_date() { | |
if ( is_multisite() ) { | |
add_blog_option( 1, 'last_post_modified_timestamp', time() ); | |
} else { | |
add_option( 'last_post_modified_timestamp', time() ); | |
} | |
} | |
add_action('save_post', 'juiz_update_last_edit_date' ); | |
/** | |
* Echo the formatted date based on the $format param and the last modified post date. | |
* @param (string) $format The formatted date based on datetime format. | |
* @see https://www.php.net/manual/en/datetime.format.php | |
* @return (string) The formatted date. | |
*/ | |
function juiz_last_edit_date( $format ) { | |
if ( is_multisite() ) { | |
$timestamp = get_blog_option( 1, 'last_post_modified_timestamp', 0 ); | |
} else { | |
$timestamp = get_option( 'last_post_modified_timestamp', 0 ); | |
} | |
echo date( $format, $timestamp ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Use this wherever you want to display the date | |
*/ | |
juiz_last_edit_date('m.d.Y'); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment