Using WordPress ‘check_comment()’ PHP function

The check_comment() WordPress PHP function checks if a comment meets certain internal conditions to be accepted. For instance, if manual moderation is active, the check will fail. Similarly, if the number of links in the comment surpasses the limit or if the comment contains any disallowed words, the function will return false. If the comment author was approved before, the comment gets automatically approved. If all checks pass, the function returns true.

Usage

Here’s a simple way to use the check_comment() function:

$author = "John Charles Smith";
$email = "[email protected]";
$url = "http://example.com";
$comment = "Excellent...";
$user_ip = "12.34.56.78";
$user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.79 Safari/535.11";
$comment_type = "comment";

if ( check_comment( $author, $email, $url, $comment, $user_ip, $user_agent, $comment_type ) ) {
    echo "The Comment robot says: Thank you for your comment.";
} else {
    echo "The Comment robot says: This comment is NOT valid!";
}

Parameters

  • $author (string) – Required. The name of the comment author.
  • $email (string) – Required. The email of the comment author.
  • $url (string) – Required. The URL of the comment author.
  • $comment (string) – Required. The content of the comment.
  • $user_ip (string) – Required. The IP address of the comment author.
  • $user_agent (string) – Required. The User-Agent of the comment author.
  • $comment_type (string) – Required. The type of comment, either user-submitted comment, trackback, or pingback.

More information

See WordPress Developer Resources: check_comment()

Examples

Checking a valid comment

This example checks a valid comment. If the comment passes the check, it returns a thank you message.

$author = "Jane Doe";
$email = "[email protected]";
$url = "http://example.com";
$comment = "Great post!";
$user_ip = "192.168.1.1";
$user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36";
$comment_type = "comment";

if ( check_comment( $author, $email, $url, $comment, $user_ip, $user_agent, $comment_type ) ) {
    echo "Thank you for your comment!";
} else {
    echo "This comment is NOT valid!";
}

This example checks a comment with too many links. It will return a message stating that the comment is not valid.

$author = "John Doe";
$email = "[email protected]";
$url = "http://example.com";
$comment = "Check out my sites http://site1.com, http://site2.com, http://site3.com";
$user_ip = "192.168.1.2";
$user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36";
$comment_type = "comment";

if ( check_comment( $author, $email, $url, $comment, $user_ip, $user_agent, $comment_type ) ) {
    echo "Thank you for your comment!";
} else {
    echo "This comment is NOT valid!";
}

Checking a comment with disallowed words

This example checks a comment that contains disallowed words. The function will return a message stating the comment is not valid.

$author = "John Doe";
$email = "[email protected]";
$url = "http://example.com";
$comment = "This post is terrible!";
$user_ip = "192.168.1.2";
$user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36";
$comment_type = "comment";

if ( check_comment( $author, $email, $url, $comment, $user_ip, $user_agent, $comment_type ) ) {
    echo "Thank you for your comment!";
} else {
    echo "This comment is NOT valid!";
}

Checking a previously approved author’s comment

This example checks a comment from a previously approved author. The function will automatically approve the comment and return a thank you message.

$author = "Jane Doe";
$email = "[email protected]";
$url = "http://example.com";
$comment = "Another great post!";
$user_ip = "192.168.1.1";
$user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36";
$comment_type = "comment";

if ( check_comment( $author, $email, $url, $comment, $user_ip, $user_agent, $comment_type ) ) {
    echo "Thank you for your comment!";
} else {
    echo "This comment is NOT valid!";
}

Checking a trackback comment

This example checks a trackback comment. The function will validate the comment based on the internal conditions and return an appropriate message.

$author = "John Doe";
$email = "[email protected]";
$url = "http://example.com";
$comment = "Here is a trackback to a post on my site.";
$user_ip = "192.168.1.2";
$user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36";
$comment_type = "trackback";

if ( check_comment( $author, $email, $url, $comment, $user_ip, $user_agent, $comment_type ) ) {
    echo "Thank you for your trackback!";
} else {
    echo "This trackback is NOT valid!";
}