Using WordPress ‘comment_on_draft’ PHP action

The comment_on_draft WordPress action fires when a comment is attempted on a post in draft mode.

Usage

add_action('comment_on_draft', 'your_custom_function');
function your_custom_function($comment_post_id) {
    // your custom code here
}

Parameters

  • $comment_post_id: int – Post ID.

More information

See WordPress Developer Resources: comment_on_draft

Examples

Send an email notification when someone comments on a draft post

Send an email to the post author when a comment is attempted on their draft post.

add_action('comment_on_draft', 'notify_author_about_comment');
function notify_author_about_comment($comment_post_id) {
    // Get post author's email
    $post = get_post($comment_post_id);
    $author_email = get_the_author_meta('user_email', $post->post_author);

    // Set email subject and message
    $subject = 'New comment on your draft post';
    $message = 'Hi, a new comment was attempted on your draft post: ' . get_the_title($comment_post_id);

    // Send email
    wp_mail($author_email, $subject, $message);
}