Last active
May 20, 2018 03:28
-
-
Save phlbnks/d71101eee4d33678e3e4a8c44adf1dbe to your computer and use it in GitHub Desktop.
Filter the *displayed* time of posts in WordPress to move them into the past/future.
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
/** | |
* Filter the_time function to show posts as being from another date/time. | |
* | |
* @param string $formatted The formatted time. | |
* @param string $format The time format used. | |
* @return string Modified formatted time. | |
*/ | |
function cc_time_machine( $formatted, $format ) { | |
$offset = get_option( 'cc_time_offset' ); // Offset in days, can be negative. | |
if ( $offset ) { | |
global $post; | |
$timestamp = get_post_time( 'U', true, $post ); // UTC timestamp with GMT offset included. | |
$adjusted = $timestamp + ( (int) $offset * DAY_IN_SECONDS ); | |
return date( $format, $adjusted ); | |
} | |
// If option doesn't exist, then just return it unmodified. | |
return $formatted; | |
} | |
add_filter( 'the_time', 'cc_time_machine', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Wrap this into a plugin or just drop it into your functions.php
Remember you need to create / manage the option for
$offset
, or removeget_option()
and replace it with a value, e.g.'-15'
.