Created
June 18, 2020 19:29
-
-
Save johnlewisdesign/783f37a433dcba6fdbc1e735a37c30c3 to your computer and use it in GitHub Desktop.
Log post history in comments - WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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