SergeyBiryukov

Add Last Modified Date To Post Columns

Apr 23rd, 2020
507
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.78 KB | None | 0 0
  1. <?php
  2. class Add_Last_Modified_Date_To_Post_Columns {
  3.  
  4.     function __construct() {
  5.         add_filter( 'manage_posts_columns',              array( $this, 'add_column' ), 10, 2 );
  6.         add_filter( 'manage_edit-post_sortable_columns', array( $this, 'add_sortable_column' ), 10, 2 );
  7.         add_action( 'manage_posts_custom_column',        array( $this, 'display_column' ), 10, 2 );
  8.         add_action( 'admin_enqueue_scripts',             array( $this, 'add_inline_style' ) );
  9.     }
  10.  
  11.     function add_column( $posts_columns, $post_type ) {
  12.         $posts_columns['modified'] = __( 'Last Modified' );
  13.  
  14.         return $posts_columns;
  15.     }
  16.  
  17.     function add_sortable_column( $sortable_columns ) {
  18.         $sortable_columns['modified'] = array( 'modified', true );
  19.  
  20.         return $sortable_columns;
  21.     }
  22.  
  23.     function display_column( $column_name, $post_id ) {
  24.         global $mode;
  25.  
  26.         if ( 'modified' !== $column_name ) {
  27.             return;
  28.         }
  29.  
  30.         $post = get_post( $post_id );
  31.  
  32.         if ( '0000-00-00 00:00:00' == $post->post_date ) {
  33.             $t_time = $h_time = __( 'Unpublished' );
  34.         } else {
  35.             $t_time = get_the_modified_time( __( 'Y/m/d g:i:s A' ) );
  36.             $m_time = $post->post_modified;
  37.             $time = get_post_modified_time( 'G', true, $post );
  38.  
  39.             $time_diff = time() - $time;
  40.  
  41.             if ( $time_diff > 0 && $time_diff < DAY_IN_SECONDS ) {
  42.                 $h_time = sprintf( __( '%s ago' ), human_time_diff( $time ) );
  43.             } else {
  44.                 $h_time = mysql2date( __( 'Y/m/d' ), $m_time );
  45.             }
  46.         }
  47.  
  48.         if ( 'excerpt' === $mode ) {
  49.             echo $t_time;
  50.         } else {
  51.             echo '<abbr title="' . $t_time . '">' . $h_time . '</abbr>';
  52.         }
  53.     }
  54.  
  55.     function add_inline_style( $hook_suffix ) {
  56.         if ( 'edit.php' !== $hook_suffix ) {
  57.             return;
  58.         }
  59.  
  60.         wp_add_inline_style( 'wp-admin', '.fixed .column-modified { width: 10% }' );
  61.     }
  62.  
  63. }
  64.  
  65. new Add_Last_Modified_Date_To_Post_Columns;
  66. ?>
Advertisement
Add Comment
Please, Sign In to add comment