Skip to content

Instantly share code, notes, and snippets.

@bpmore
Created May 26, 2017 15:00
Show Gist options
  • Save bpmore/7820d45c44af4adb46d09e9a55e3b080 to your computer and use it in GitHub Desktop.
Save bpmore/7820d45c44af4adb46d09e9a55e3b080 to your computer and use it in GitHub Desktop.
Add last modified to WordPress admin for page and post listings
/** Andrew Norcross - Date Modified Display - http://andrewnorcross.com/tutorials/modified-date-display/ */
// Post Columns
add_action ( 'manage_posts_custom_column', 'rkv_post_columns_data', 10, 2 );
add_filter ( 'manage_edit-post_columns', 'rkv_post_columns_display' );
function rkv_post_columns_data( $column, $post_id ) {
switch ( $column ) {
case 'modified':
$m_orig = get_post_field( 'post_modified', $post_id, 'raw' );
$m_stamp = strtotime( $m_orig );
$modified = date('n/j/y @ g:i a', $m_stamp );
$modr_id = get_post_meta( $post_id, '_edit_last', true );
$auth_id = get_post_field( 'post_author', $post_id, 'raw' );
$user_id = !empty( $modr_id ) ? $modr_id : $auth_id;
$user_info = get_userdata( $user_id );
echo '<p class="mod-date">';
echo '<em>'.$modified.'</em><br />';
echo 'by <strong>'.$user_info->display_name.'<strong>';
echo '</p>';
break;
// end all case breaks
}
}
function rkv_post_columns_display( $columns ) {
$columns['modified'] = 'Last Modified';
return $columns;
}
// Page Columns
add_action ( 'manage_pages_custom_column', 'rkv_heirch_columns', 10, 2 );
add_filter ( 'manage_edit-page_columns', 'rkv_page_columns' );
function rkv_heirch_columns( $column, $post_id ) {
switch ( $column ) {
case 'modified':
$m_orig = get_post_field( 'post_modified', $post_id, 'raw' );
$m_stamp = strtotime( $m_orig );
$modified = date('n/j/y @ g:i a', $m_stamp );
$modr_id = get_post_meta( $post_id, '_edit_last', true );
$auth_id = get_post_field( 'post_author', $post_id, 'raw' );
$user_id = !empty( $modr_id ) ? $modr_id : $auth_id;
$user_info = get_userdata( $user_id );
echo '<p class="mod-date">';
echo '<em>'.$modified.'</em><br />';
echo 'by <strong>'.$user_info->display_name.'<strong>';
echo '</p>';
break;
// end all case breaks
}
}
function rkv_page_columns( $columns ) {
$columns['modified'] = 'Last Modified';
return $columns;
}
// Do the sort!
function last_modified_column_register_sortable( $columns ) {
$columns["modified"] = "last_modified";
return $columns;
}
add_filter( "manage_edit-post_sortable_columns", "last_modified_column_register_sortable" );
add_filter( "manage_edit-page_sortable_columns", "last_modified_column_register_sortable" );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment