Title: comment_post
Published: April 25, 2014
Last modified: August 20, 2026

---

# do_action( ‘comment_post’, int $comment_id, int|string $comment_approved, array $commentdata )

## In this article

 * [Parameters](https://developer.wordpress.org/reference/hooks/comment_post/?output_format=md#parameters)
 * [Source](https://developer.wordpress.org/reference/hooks/comment_post/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/hooks/comment_post/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/hooks/comment_post/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/hooks/comment_post/?output_format=md#user-contributed-notes)

Fires immediately after a comment is inserted into the database.

## 󠀁[Parameters](https://developer.wordpress.org/reference/hooks/comment_post/?output_format=md#parameters)󠁿

 `$comment_id`int

The comment ID.

`$comment_approved`int|string

1 if the comment is approved, 0 if not, `'spam'` if spam.

`$commentdata`array

Comment data.

## 󠀁[Source](https://developer.wordpress.org/reference/hooks/comment_post/?output_format=md#source)󠁿

    ```php
    do_action( 'comment_post', $comment_id, $commentdata['comment_approved'], $commentdata );
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/comment.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/comment.php#L2491)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/comment.php#L2491-L2491)

## 󠀁[Related](https://developer.wordpress.org/reference/hooks/comment_post/?output_format=md#related)󠁿

| Used by | Description | 
| [wp_new_comment()](https://developer.wordpress.org/reference/functions/wp_new_comment/)`wp-includes/comment.php` |

Adds a new comment to the database.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/hooks/comment_post/?output_format=md#changelog)󠁿

| Version | Description | 
| [4.5.0](https://developer.wordpress.org/reference/since/4.5.0/) | The `$commentdata` parameter was added. | 
| [1.2.0](https://developer.wordpress.org/reference/since/1.2.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/hooks/comment_post/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 5 content](https://developer.wordpress.org/reference/hooks/comment_post/?output_format=md#comment-content-840)
 2.    [Stephen Cronin](https://profiles.wordpress.org/stephencronin/)  [  11 years ago  ](https://developer.wordpress.org/reference/hooks/comment_post/#comment-840)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fround-lake.dustinice.workers.dev%3A443%2Fhttps%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fcomment_post%2F%23comment-840)
     Vote results for this note: 2[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fround-lake.dustinice.workers.dev%3A443%2Fhttps%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fcomment_post%2F%23comment-840)
 4.  The following example uses the comment_post hook to run a function immediately
     after a comment is posted. The function checks whether the comment is approved
     and, if so, executes the code specified.
 5.      ```php
         function show_message_function( $comment_ID, $comment_approved ) {
         	if( 1 === $comment_approved ){
         		//function logic goes here
         	}
         }
         add_action( 'comment_post', 'show_message_function', 10, 2 );
         ```
     
 6.  Note that the add_action line includes the priority and the number of parameters(`,
     10, 2`). If we leave the number of parameters out, we will only be able to access
     to the first parameter ($comment_ID) in our function. We will not be able to access
     the second parameter ($comment_approved).
 7.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fround-lake.dustinice.workers.dev%3A443%2Fhttps%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fcomment_post%2F%3Freplytocom%3D840%23feedback-editor-840)
 8.   [Skip to note 6 content](https://developer.wordpress.org/reference/hooks/comment_post/?output_format=md#comment-content-4879)
 9.    [Andrew Wilder](https://profiles.wordpress.org/eatingrules/)  [  6 years ago  ](https://developer.wordpress.org/reference/hooks/comment_post/#comment-4879)
 10. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fround-lake.dustinice.workers.dev%3A443%2Fhttps%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fcomment_post%2F%23comment-4879)
     Vote results for this note: 1[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fround-lake.dustinice.workers.dev%3A443%2Fhttps%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fcomment_post%2F%23comment-4879)
 11. `$comment_approved` can also be ‘trash’ if the new comment is trashed automatically
     based on Settings > Discussion > Disallowed Comment Keys.
 12.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fround-lake.dustinice.workers.dev%3A443%2Fhttps%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fcomment_post%2F%3Freplytocom%3D4879%23feedback-editor-4879)
 13.  [Skip to note 7 content](https://developer.wordpress.org/reference/hooks/comment_post/?output_format=md#comment-content-5362)
 14.   [Chigozie Orunta](https://profiles.wordpress.org/chigozieorunta/)  [  5 years ago  ](https://developer.wordpress.org/reference/hooks/comment_post/#comment-5362)
 15. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fround-lake.dustinice.workers.dev%3A443%2Fhttps%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fcomment_post%2F%23comment-5362)
     Vote results for this note: 1[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fround-lake.dustinice.workers.dev%3A443%2Fhttps%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fcomment_post%2F%23comment-5362)
 16. Get notified via email when a user posts a comment on your site’s blog.
 17.     ```php
         function wpdocs_notify_my_mail( $comment_id, $comment_approved ) {
         	if ( ! $comment_approved ) {
         		$comment = get_comment( $comment_id );
         		$mail = 'test@example.org';
         		$subject = sprintf( 'New Comment by: %s', $comment->comment_author );
         		$message = $comment->comment_content;
     
         		wp_mail( $mail, $subject, $message );
         	}
         }
         add_action( 'comment_post', 'wpdocs_notify_my_mail', 10, 2 );
         ```
     
 18.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fround-lake.dustinice.workers.dev%3A443%2Fhttps%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fcomment_post%2F%3Freplytocom%3D5362%23feedback-editor-5362)
 19.  [Skip to note 8 content](https://developer.wordpress.org/reference/hooks/comment_post/?output_format=md#comment-content-7478)
 20.   [mangofanfanw](https://profiles.wordpress.org/mangofanfanw/)  [  8 months ago  ](https://developer.wordpress.org/reference/hooks/comment_post/#comment-7478)
 21. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fround-lake.dustinice.workers.dev%3A443%2Fhttps%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fcomment_post%2F%23comment-7478)
     Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fround-lake.dustinice.workers.dev%3A443%2Fhttps%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fcomment_post%2F%23comment-7478)
 22. If you use rest api to submit comments, hook **comment_post** may not work.
      Then
     you can use hook **wp_insert_comment** instand.
 23.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fround-lake.dustinice.workers.dev%3A443%2Fhttps%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fcomment_post%2F%3Freplytocom%3D7478%23feedback-editor-7478)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fround-lake.dustinice.workers.dev%3A443%2Fhttps%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fcomment_post%2F)
before being able to contribute a note or feedback.