Categories
Posted in: How to, Wordpress

Add Last Edit by to WP Post/Page List Table

There are some situations where you may want to know who was the last person to edit a page or post. WordPress doesn’t provide us with this information out of the box.. yet.

There are a few WordPress actions and filters we are going to be utilizing in our implementation those are as follows:

  • manage_posts_custom_column (action – for posts)
  • manage_edit-post_columns (filter – for posts)
  • manage_pages_custom_column (action – for pages)
  • manage_edit-page_columns (filter – for pages)

Our end results will look like this.
Adding a column to the wordpress post table list to show a Last Modified by: UserName column.

Let’s get coding

For our Post List table column, we are going to be using the first 2 filters mentioned above.

// Adds a last modified by ---user column to wordpress post list
// ie /wp-admin/edit.php
add_action ( 'manage_posts_custom_column', 'jematrix_post_columns_data', 10, 2	);
add_filter ( 'manage_edit-post_columns', 'jematrix_post_columns_display' );

function jematrix_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 jematrix_post_columns_display( $columns ) {

	$columns['modified']	= 'Last Modified';
	return $columns;
}

The jematrix_post_columns_data() function sets our new column to the post. If the post hasn’t been updated yet it will be empty until the next time it is updated.

In the next few lines, we get the time it was modified and format it to our liking. You may format it, however, you would like.

The next thing we do is make a new meta value for our post to save the information to our database.

Finally, we output the HTML to display when and who was the last user to update the post. We use display_name but you may use something else like last_name first_name user_login etc.

Tip: An easy way to find the field (name) you want to show go to your profile page, go to the one you want ie “Nickname” right-click and inspect the element. Look for the <input type=”text” name=”nickname” nickname is what we are looking for. More info here

The function jematrix_post_columns_display() simply sets our new column in the post list table and sets the title of “Last Modified”.

Now on to the Pages table list.

To do the same for Pages as we did for posts is pretty much the exact same thing except for the actions and filters. But, the structure and concept are the same.

// and for pages
add_action ( 'manage_pages_custom_column',	'jematrix_heirch_columns',	10,	2	);
add_filter ( 'manage_edit-page_columns',	'jematrix_page_columns'				);

function jematrix_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 jematrix_page_columns( $columns ) {

	$columns['modified']	= 'Last Modified';
	return $columns;
}

We could take this a bit further and make the column sortable. However, we didn’t see the need for it this time. If you would like us to show you how, let us know in the comments below.

Happy Coding!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.