Filter hook 'rest_pre_insert_comment'

in WP Core File wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php at line 707

View Source

rest_pre_insert_comment

Filter Hook
Description
Filters a comment before it is inserted via the REST API. Allows modification of the comment right before it is inserted via wp_insert_comment(). Returning a WP_Error value from the filter will short-circuit insertion and allow skipping further processing.

Hook Information

File Location wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php View on GitHub
Hook Type Filter
Line Number 707

Hook Parameters

Type Name Description
array|WP_Error $prepared_comment The prepared comment data for wp_insert_comment().
WP_REST_Request $request Request used to insert the comment.

Usage Examples

Basic Usage
<?php
// Hook into rest_pre_insert_comment
add_filter('rest_pre_insert_comment', 'my_custom_filter', 10, 2);

function my_custom_filter($prepared_comment, $request) {
    // Your custom filtering logic here
    return $prepared_comment;
}

Source Code Context

wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php:707 - How this hook is used in WordPress core
<?php
 702  		 * @since 4.8.0 `$prepared_comment` can now be a WP_Error to short-circuit insertion.
 703  		 *
 704  		 * @param array|WP_Error  $prepared_comment The prepared comment data for wp_insert_comment().
 705  		 * @param WP_REST_Request $request          Request used to insert the comment.
 706  		 */
 707  		$prepared_comment = apply_filters( 'rest_pre_insert_comment', $prepared_comment, $request );
 708  		if ( is_wp_error( $prepared_comment ) ) {
 709  			return $prepared_comment;
 710  		}
 711  
 712  		$comment_id = wp_insert_comment( wp_filter_comment( wp_slash( (array) $prepared_comment ) ) );

PHP Documentation

<?php
/**
		 * Filters a comment before it is inserted via the REST API.
		 *
		 * Allows modification of the comment right before it is inserted via wp_insert_comment().
		 * Returning a WP_Error value from the filter will short-circuit insertion and allow
		 * skipping further processing.
		 *
		 * @since 4.7.0
		 * @since 4.8.0 `$prepared_comment` can now be a WP_Error to short-circuit insertion.
		 *
		 * @param array|WP_Error  $prepared_comment The prepared comment data for wp_insert_comment().
		 * @param WP_REST_Request $request          Request used to insert the comment.
		 */
Quick Info
  • Hook Type: Filter
  • Parameters: 2
  • File: wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php
Related Hooks

Related hooks will be displayed here in future updates.