do_action( 'comment_post', int $comment_ID, int|string $comment_approved, array $commentdata )

Fires immediately after a comment is inserted into the database.


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.


Top ↑

Source

File: wp-includes/comment.php

View on Trac



Top ↑

Changelog

Changelog
Version Description
4.5.0 The $commentdata parameter was added.
1.2.0 Introduced.

Top ↑

User Contributed Notes

  1. Skip to note 1 content
    Contributed by Stephen Cronin

    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.

    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 );
    

    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).

  2. Skip to note 3 content
    Contributed by Chigozie Orunta

    Get notified via email when a user posts a comment on your site’s blog.

    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 );
    

You must log in before being able to contribute a note or feedback.