Skip to content

Instantly share code, notes, and snippets.

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 johnlewisdesign/783f37a433dcba6fdbc1e735a37c30c3 to your computer and use it in GitHub Desktop.
Save johnlewisdesign/783f37a433dcba6fdbc1e735a37c30c3 to your computer and use it in GitHub Desktop.
Log post history in comments - WordPress
function add_comment_on_post_update( $post_id, $post_after, $post_before ) {
// If this is just a revision, don't send the email.
if ( wp_is_post_revision( $post_id ) )
return;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return;
if (isset($post_before->post_status) && 'auto-draft' == $post_before->post_status) {
return;
}
if ('post' == $_POST['post_type']) {
$current_user = wp_get_current_user();
$comment = 'Edited by '.$current_user->user_login;
$time = current_time('mysql');
$data = array(
'comment_post_ID' => $post_id,
'comment_author' => 'admin',
'comment_author_email' => 'admin@admin.com',
'comment_author_url' => 'http://www.xyz.com',
'comment_content' => $comment,
'user_id' => $current_user->ID,
'comment_date' => $time,
'comment_approved' => 1,
'comment_type' => 'custom-comment-class'
);
wp_insert_comment($data);
}
}
add_action( 'post_updated', 'add_comment_on_post_update', 10 , 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment