Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Filter the *displayed* time of posts in WordPress to move them into the past/future.
/**
* 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 );
@emirpprime

This comment has been minimized.

Show comment
Hide comment
@emirpprime

emirpprime Apr 25, 2017

Wrap this into a plugin or just drop it into your functions.php
Remember you need to create / manage the option for $offset, or remove get_option() and replace it with a value, e.g. '-15'.

Owner

emirpprime commented Apr 25, 2017

Wrap this into a plugin or just drop it into your functions.php
Remember you need to create / manage the option for $offset, or remove get_option() and replace it with a value, e.g. '-15'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment