WordPress.org

Ready to get started?Download WordPress

Codex

Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Plugin API/Filter Reference/pre comment approved

Description

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.

Usage

<?php function filter_handler$approved $commentdata ){ ...... }

add_filter'pre_comment_approved' 'filter_handler' '99'); ?>

Parameters

$approved
(mixed) (required) Preliminary comment approval status: 0, 1, or 'spam'.
Default: None
$commentdata
(array) (required) Comment data array (see below)
Default: None

The $commentdata array contains the same indices as the array returned by 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"

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.

Examples

<?php
function filter_handler$approved $commentdata )
{
  
// inspect $commentdata to determine approval, disapproval, or spam status
  
return $approved;
}

add_filter'pre_comment_approved' 'filter_handler' '99');
?>

Notes

wp_allow_comment() handles the preliminary approval checking, and that approval status is passed through this filter before it returns.


Change Log

(wp_allow_comment) Since: 2.0.0

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: http://core.trac.wordpress.org/ticket/14802 )

Source File

wp_allow_comment is located in wp-includes/comment.php.

Related

comment_save_pre, pre_comment_approved, pre_comment_content, preprocess_comment, wp_allow_comment()

See Also

Plugin_API/Filter_Reference