Skip to content

Instantly share code, notes, and snippets.

@maheshwaghmare
Last active December 4, 2019 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maheshwaghmare/88c6c036cca90e46b03b4d74fffe58df to your computer and use it in GitHub Desktop.
Save maheshwaghmare/88c6c036cca90e46b03b4d74fffe58df to your computer and use it in GitHub Desktop.
Highlight the dates column with readable date format. E.g. Format "Scheduled 2 weeks in advance" is more readable than "Scheduled 2019/12/18"
<?php
/**
* Highlight the dates column with readable date format.
*
* @since 1.0.0
*
* @todo Change the `prefix_` and with your own unique prefix.
*
* @param string $t_time The published time.
* @param WP_Post $post Post object.
* @param string $column_name The column name.
* @param string $mode The list display mode ('excerpt' or 'list').
*
* @return mixed
*/
if( ! function_exists( 'prefix_highlight_post_date_with_readable_format' ) ) :
function prefix_highlight_post_date_with_readable_format( $t_time = '', $post = object, $date = '', $mode = '' ) {
// If current list post type is not a `post`?
// Then return default date format.
if( 'post' !== $post->post_type ) {
return $t_time;
}
$readable_date = human_time_diff( strtotime( $post->post_date ) );
// Empty variable.
$color = '';
// If post status is `draft` then assign `red` color.
if( 'future' === $post->post_status ) {
$color = 'red';
$readable_date = $readable_date . ' in advance'; // E.g. 2 weeks in advance
}
// // If post status is `publish` then assign `blue` color.
if( 'publish' === $post->post_status ) {
$color = 'blue';
$readable_date = $readable_date . ' ago'; // E.g. 1 month ago
}
return '<span style="color: '.$color.'">' . $readable_date . '</span>';
}
add_filter( 'post_date_column_time', 'prefix_highlight_post_date_with_readable_format', 10, 4 );
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment