Summery Summery
Adds a new comment to the database.
Syntax Syntax
Description Description
Filters new comment to ensure that the fields are sanitized and valid before inserting comment into database. Calls ‘comment_post’ action with comment ID and whether comment is approved by WordPress. Also has ‘preprocess_comment’ filter for processing the comment data before the function handles it.
We use REMOTE_ADDR here directly. If you are behind a proxy, you should ensure that it is properly set, such as in wp-config.php, for your environment.
Parameters Parameters
- $commentdata
-
(Required) Comment data.
- 'comment_author'
(string) The name of the comment author. - 'comment_author_email'
(string) The comment author email address. - 'comment_author_url'
(string) The comment author URL. - 'comment_content'
(string) The content of the comment. - 'comment_date'
(string) The date the comment was submitted. Default is the current time. - 'comment_date_gmt'
(string) The date the comment was submitted in the GMT timezone. Default is$comment_datein the GMT timezone. - 'comment_parent'
(int) The ID of this comment's parent, if any. Default 0. - 'comment_post_ID'
(int) The ID of the post that relates to the comment. - 'user_id'
(int) The ID of the user who submitted the comment. Default 0. - 'user_ID'
(int) Kept for backward-compatibility. Use$user_idinstead. - 'comment_agent'
(string) Comment author user agent. Default is the value of 'HTTP_USER_AGENT' in the$_SERVERsuperglobal sent in the original request. - 'comment_author_IP'
(string) Comment author IP address in IPv4 format. Default is the value of 'REMOTE_ADDR' in the$_SERVERsuperglobal sent in the original request.
- 'comment_author'
- $avoid_die
-
(Optional) Should errors be returned as WP_Error objects instead of executing wp_die()?
Default value: false
Return Return
(int|false|WP_Error) The ID of the comment on success, false or WP_Error on failure.
Source Source
File: wp-includes/comment.php
/** This filter is documented in wp-includes/comment.php */
$commentdata['user_id'] = apply_filters( 'pre_user_id', $commentdata['user_id'] );
}
/**
* Filters the comment author's browser user agent before it is set.
*
* @since 1.5.0
*
* @param string $comment_agent The comment author's browser user agent.
*/
$commentdata['comment_agent'] = apply_filters( 'pre_comment_user_agent', ( isset( $commentdata['comment_agent'] ) ? $commentdata['comment_agent'] : '' ) );
/** This filter is documented in wp-includes/comment.php */
$commentdata['comment_author'] = apply_filters( 'pre_comment_author_name', $commentdata['comment_author'] );
/**
* Filters the comment content before it is set.
*
* @since 1.5.0
*
* @param string $comment_content The comment content.
*/
$commentdata['comment_content'] = apply_filters( 'pre_comment_content', $commentdata['comment_content'] );
/**
* Filters the comment author's IP address before it is set.
*
* @since 1.5.0
*
* @param string $comment_author_ip The comment author's IP address.
*/
$commentdata['comment_author_IP'] = apply_filters( 'pre_comment_user_ip', $commentdata['comment_author_IP'] );
/** This filter is documented in wp-includes/comment.php */
$commentdata['comment_author_url'] = apply_filters( 'pre_comment_author_url', $commentdata['comment_author_url'] );
/** This filter is documented in wp-includes/comment.php */
$commentdata['comment_author_email'] = apply_filters( 'pre_comment_author_email', $commentdata['comment_author_email'] );
$commentdata['filtered'] = true;
return $commentdata;
}
/**
* Whether a comment should be blocked because of comment flood.
*
* @since 2.1.0
*
* @param bool $block Whether plugin has already blocked comment.
* @param int $time_lastcomment Timestamp for last comment.
* @param int $time_newcomment Timestamp for new comment.
* @return bool Whether comment should be blocked.
*/
function wp_throttle_comment_flood( $block, $time_lastcomment, $time_newcomment ) {
if ( $block ) { // A plugin has already blocked... we'll let that decision stand.
return $block;
}
if ( ( $time_newcomment - $time_lastcomment ) < 15 ) {
return true;
}
return false;
}
/**
* Adds a new comment to the database.
*
* Filters new comment to ensure that the fields are sanitized and valid before
* inserting comment into database. Calls {@see 'comment_post'} action with comment ID
* and whether comment is approved by WordPress. Also has {@see 'preprocess_comment'}
* filter for processing the comment data before the function handles it.
*
* We use `REMOTE_ADDR` here directly. If you are behind a proxy, you should ensure
* that it is properly set, such as in wp-config.php, for your environment.
*
* See {@link https://core.trac.wordpress.org/ticket/9235}
*
* @since 1.5.0
* @since 4.3.0 Introduced the `comment_agent` and `comment_author_IP` arguments.
* @since 4.7.0 The `$avoid_die` parameter was added, allowing the function
* to return a WP_Error object instead of dying.
* @since 5.5.0 The `$avoid_die` parameter was renamed to `$wp_error`.
* @since 5.5.0 Introduced the `comment_type` argument.
*
* @see wp_insert_comment()
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param array $commentdata {
* Comment data.
*
* @type string $comment_author The name of the comment author.
* @type string $comment_author_email The comment author email address.
* @type string $comment_author_url The comment author URL.
* @type string $comment_content The content of the comment.
* @type string $comment_date The date the comment was submitted. Default is the current time.
* @type string $comment_date_gmt The date the comment was submitted in the GMT timezone.
* Default is `$comment_date` in the GMT timezone.
* @type string $comment_type Comment type. Default 'comment'.
* @type int $comment_parent The ID of this comment's parent, if any. Default 0.
Advertisement
Changelog Changelog
| Version | Description |
|---|---|
| 4.7.0 | The $avoid_die parameter was added, allowing the function to return a WP_Error object instead of dying. |
| 4.3.0 | 'comment_agent' and 'comment_author_IP' can be set via $commentdata. |
| 1.5.0 | Introduced. |