apply_filters( 'pre_comment_approved', int|string|WP_Error $approved, array $commentdata )

Filters a comment’s approval status before it is set.

Parameters Parameters


(int|string|WP_Error) The approval status. Accepts 1, 0, 'spam', 'trash', or WP_Error.


(array) Comment data.

Top ↑

More Information More Information

  • A filter hook called by the wp_allow_comment() function prior to inserting a comment into the database. The filter is applied to the proposed comment’s approval status, allowing a plugin to override.
  • wp_allow_comment() handles the preliminary approval checking, and that approval status is passed through this filter before it returns.
  • The $commentdata array contains the same indices as the array returned by WP_Comment_Query::get_comments(), including:

    'comment_post_ID' - The post to which the comment will apply
    'comment_author' - (may be empty)
    'comment_author_email' - (may be empty)
    'comment_author_url' - (may be empty)
    'comment_author_IP' - IP address
    'comment_agent' - e.g., "Mozilla/5.0..."
    'comment_content' - The text of the proposed comment
    'comment_type' - 'pingback', 'trackback', or empty for regular comments
    'user_ID' - (empty if not logged in)
  • Return Values:

    0 (int) comment is marked for moderation as "Pending"
    1 (int) comment is marked for immediate publication as "Approved"
    'spam' (string) comment is marked as "Spam"
    'trash' (string) comment is to be put in the Trash

    In all cases the comment is added to the database, even spam. Comments marked as spam will never be visible on the front end. Spam comments are kept for possible analysis by plugins.
  • Prior to WP 3.1, the filter was not passed $comment_data and instead was expected to use global variables such as $comment_ID to access information about the comment. (see: )

Top ↑

Source Source

File: wp-includes/comment.php

View on Trac

Top ↑

Changelog Changelog

Version Description
4.9.0 Returning a WP_Error value from the filter will short-circuit comment insertion and allow skipping further processing.
2.1.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by stevenlinx

    Example migrated from Codex:

    The following code examines comment content and modifies approval status if needed

    add_filter( 'pre_comment_approved' , 'filter_handler' , '99', 2 );
    function filter_handler( $approved , $commentdata )
      // insert code here to inspect $commentdata and determine 'approval', 'disapproval', 'trash', or 'spam' status
      return $approved;

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