Filter hook 'rest_allow_anonymous_comments'

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

View Source

rest_allow_anonymous_comments

Filter Hook
Description
Filters whether comments can be created via the REST API without authentication. Enables creating comments for anonymous users.

Hook Information

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

Hook Parameters

Type Name Description
bool $allow_anonymous Whether to allow anonymous comments to be created. Default `false`.
WP_REST_Request $request Request used to generate the response.

Usage Examples

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

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

Source Code Context

wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php:476 - How this hook is used in WordPress core
<?php
 471  			 * @param bool $allow_anonymous Whether to allow anonymous comments to
 472  			 *                              be created. Default `false`.
 473  			 * @param WP_REST_Request $request Request used to generate the
 474  			 *                                 response.
 475  			 */
 476  			$allow_anonymous = apply_filters( 'rest_allow_anonymous_comments', false, $request );
 477  
 478  			if ( ! $allow_anonymous ) {
 479  				return new WP_Error(
 480  					'rest_comment_login_required',
 481  					__( 'Sorry, you must be logged in to comment.' ),

PHP Documentation

<?php
/**
			 * Filters whether comments can be created via the REST API without authentication.
			 *
			 * Enables creating comments for anonymous users.
			 *
			 * @since 4.7.0
			 *
			 * @param bool $allow_anonymous Whether to allow anonymous comments to
			 *                              be created. Default `false`.
			 * @param WP_REST_Request $request Request used to generate the
			 *                                 response.
			 */
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.