Title: WP_REST_Comments_Controller::prepare_item_for_response
Published: December 6, 2016
Last modified: May 20, 2026

---

# WP_REST_Comments_Controller::prepare_item_for_response( WP_Comment $item, WP_REST_Request $request ): 󠀁[WP_REST_Response](https://developer.wordpress.org/reference/classes/wp_rest_response/)󠁿

## In this article

 * [Parameters](https://developer.wordpress.org/reference/classes/wp_rest_comments_controller/prepare_item_for_response/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/classes/wp_rest_comments_controller/prepare_item_for_response/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/classes/wp_rest_comments_controller/prepare_item_for_response/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/classes/wp_rest_comments_controller/prepare_item_for_response/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/classes/wp_rest_comments_controller/prepare_item_for_response/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/wp_rest_comments_controller/prepare_item_for_response/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/classes/wp_rest_comments_controller/prepare_item_for_response/?output_format=md#wp--skip-link--target)

Prepares a single comment output for response.

## 󠀁[Parameters](https://developer.wordpress.org/reference/classes/wp_rest_comments_controller/prepare_item_for_response/?output_format=md#parameters)󠁿

 `$item`[WP_Comment](https://developer.wordpress.org/reference/classes/wp_comment/)
required

Comment object.

`$request`[WP_REST_Request](https://developer.wordpress.org/reference/classes/wp_rest_request/)
required

Request object.

## 󠀁[Return](https://developer.wordpress.org/reference/classes/wp_rest_comments_controller/prepare_item_for_response/?output_format=md#return)󠁿

 [WP_REST_Response](https://developer.wordpress.org/reference/classes/wp_rest_response/)
Response object.

## 󠀁[Source](https://developer.wordpress.org/reference/classes/wp_rest_comments_controller/prepare_item_for_response/?output_format=md#source)󠁿

    ```php
    public function prepare_item_for_response( $item, $request ) {
    	// Restores the more descriptive, specific name for use within this method.
    	$comment = $item;

    	// Don't prepare the response body for HEAD requests.
    	if ( $request->is_method( 'HEAD' ) ) {
    		/** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php */
    		return apply_filters( 'rest_prepare_comment', new WP_REST_Response( array() ), $comment, $request );
    	}

    	$fields = $this->get_fields_for_response( $request );
    	$data   = array();

    	if ( in_array( 'id', $fields, true ) ) {
    		$data['id'] = (int) $comment->comment_ID;
    	}

    	if ( in_array( 'post', $fields, true ) ) {
    		$data['post'] = (int) $comment->comment_post_ID;
    	}

    	if ( in_array( 'parent', $fields, true ) ) {
    		$data['parent'] = (int) $comment->comment_parent;
    	}

    	if ( in_array( 'author', $fields, true ) ) {
    		$data['author'] = (int) $comment->user_id;
    	}

    	if ( in_array( 'author_name', $fields, true ) ) {
    		$data['author_name'] = $comment->comment_author;
    	}

    	if ( in_array( 'author_email', $fields, true ) ) {
    		$data['author_email'] = $comment->comment_author_email;
    	}

    	if ( in_array( 'author_url', $fields, true ) ) {
    		$data['author_url'] = $comment->comment_author_url;
    	}

    	if ( in_array( 'author_ip', $fields, true ) ) {
    		$data['author_ip'] = $comment->comment_author_IP;
    	}

    	if ( in_array( 'author_user_agent', $fields, true ) ) {
    		$data['author_user_agent'] = $comment->comment_agent;
    	}

    	if ( in_array( 'date', $fields, true ) ) {
    		$data['date'] = mysql_to_rfc3339( $comment->comment_date );
    	}

    	if ( in_array( 'date_gmt', $fields, true ) ) {
    		$data['date_gmt'] = mysql_to_rfc3339( $comment->comment_date_gmt );
    	}

    	if ( in_array( 'content', $fields, true ) ) {
    		$data['content'] = array(
    			/** This filter is documented in wp-includes/comment-template.php */
    			'rendered' => apply_filters( 'comment_text', $comment->comment_content, $comment, array() ),
    			'raw'      => $comment->comment_content,
    		);
    	}

    	if ( in_array( 'link', $fields, true ) ) {
    		$data['link'] = get_comment_link( $comment );
    	}

    	if ( in_array( 'status', $fields, true ) ) {
    		$data['status'] = $this->prepare_status_response( $comment->comment_approved );
    	}

    	if ( in_array( 'type', $fields, true ) ) {
    		$data['type'] = get_comment_type( $comment->comment_ID );
    	}

    	if ( in_array( 'author_avatar_urls', $fields, true ) ) {
    		$data['author_avatar_urls'] = rest_get_avatar_urls( $comment );
    	}

    	if ( in_array( 'meta', $fields, true ) ) {
    		$data['meta'] = $this->meta->get_value( $comment->comment_ID, $request );
    	}

    	$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
    	$data    = $this->add_additional_fields_to_object( $data, $request );
    	$data    = $this->filter_response_by_context( $data, $context );

    	// Wrap the data in a response object.
    	$response = rest_ensure_response( $data );

    	if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
    		$response->add_links( $this->prepare_links( $comment ) );
    	}

    	/**
    	 * Filters a comment returned from the REST API.
    	 *
    	 * Allows modification of the comment right before it is returned.
    	 *
    	 * @since 4.7.0
    	 *
    	 * @param WP_REST_Response  $response The response object.
    	 * @param WP_Comment        $comment  The original comment object.
    	 * @param WP_REST_Request   $request  Request used to generate the response.
    	 */
    	return apply_filters( 'rest_prepare_comment', $response, $comment, $request );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.0/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php#L1130)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.0/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php#L1130-L1238)

## 󠀁[Hooks](https://developer.wordpress.org/reference/classes/wp_rest_comments_controller/prepare_item_for_response/?output_format=md#hooks)󠁿

 [apply_filters( ‘comment_text’, string $comment_text, WP_Comment|null $comment, array $args )](https://developer.wordpress.org/reference/hooks/comment_text/)

Filters the text of a comment to be displayed.

 [apply_filters( ‘rest_prepare_comment’, WP_REST_Response $response, WP_Comment $comment, WP_REST_Request $request )](https://developer.wordpress.org/reference/hooks/rest_prepare_comment/)

Filters a comment returned from the REST API.

## 󠀁[Related](https://developer.wordpress.org/reference/classes/wp_rest_comments_controller/prepare_item_for_response/?output_format=md#related)󠁿

| Uses | Description | 
| [rest_is_field_included()](https://developer.wordpress.org/reference/functions/rest_is_field_included/)`wp-includes/rest-api.php` |

Given an array of fields to include in a response, some of which may be `nested.fields`, determine whether the provided field should be included in the response body.

  | 
| [rest_get_avatar_urls()](https://developer.wordpress.org/reference/functions/rest_get_avatar_urls/)`wp-includes/rest-api.php` |

Retrieves the avatar URLs in various sizes.

  | 
| [WP_REST_Comments_Controller::prepare_status_response()](https://developer.wordpress.org/reference/classes/wp_rest_comments_controller/prepare_status_response/)`wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php` |

Checks comment_approved to set comment status for single comment output.

  | 
| [WP_REST_Comments_Controller::prepare_links()](https://developer.wordpress.org/reference/classes/wp_rest_comments_controller/prepare_links/)`wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php` |

Prepares links for the request.

  | 
| [mysql_to_rfc3339()](https://developer.wordpress.org/reference/functions/mysql_to_rfc3339/)`wp-includes/functions.php` |

Parses and formats a MySQL datetime (Y-m-d H:i:s) for ISO8601 (Y-m-d\TH:i:s).

  | 
| [get_comment_link()](https://developer.wordpress.org/reference/functions/get_comment_link/)`wp-includes/comment-template.php` |

Retrieves the link to a given comment.

  | 
| [get_comment_type()](https://developer.wordpress.org/reference/functions/get_comment_type/)`wp-includes/comment-template.php` |

Retrieves the comment type of the current comment.

  | 
| [rest_ensure_response()](https://developer.wordpress.org/reference/functions/rest_ensure_response/)`wp-includes/rest-api.php` |

Ensures a REST response is a response object (for consistency).

  | 
| [apply_filters()](https://developer.wordpress.org/reference/functions/apply_filters/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to a filter hook.

  |

[Show 4 more](https://developer.wordpress.org/reference/classes/wp_rest_comments_controller/prepare_item_for_response/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/classes/wp_rest_comments_controller/prepare_item_for_response/?output_format=md#)

| Used by | Description | 
| [WP_REST_Comments_Controller::update_item()](https://developer.wordpress.org/reference/classes/wp_rest_comments_controller/update_item/)`wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php` |

Updates a comment.

  | 
| [WP_REST_Comments_Controller::delete_item()](https://developer.wordpress.org/reference/classes/wp_rest_comments_controller/delete_item/)`wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php` |

Deletes a comment.

  | 
| [WP_REST_Comments_Controller::get_item()](https://developer.wordpress.org/reference/classes/wp_rest_comments_controller/get_item/)`wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php` |

Retrieves a comment.

  | 
| [WP_REST_Comments_Controller::create_item()](https://developer.wordpress.org/reference/classes/wp_rest_comments_controller/create_item/)`wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php` |

Creates a comment.

  | 
| [WP_REST_Comments_Controller::get_items()](https://developer.wordpress.org/reference/classes/wp_rest_comments_controller/get_items/)`wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php` |

Retrieves a list of comment items.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wp_rest_comments_controller/prepare_item_for_response/?output_format=md#changelog)󠁿

| Version | Description | 
| [5.9.0](https://developer.wordpress.org/reference/since/5.9.0/) | Renamed `$comment` to `$item` to match parent class for PHP 8 named parameter support. | 
| [4.7.0](https://developer.wordpress.org/reference/since/4.7.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_rest_comments_controller%2Fprepare_item_for_response%2F)
before being able to contribute a note or feedback.