Title: WP_REST_Comments_Controller::prepare_links
Published: December 6, 2016
Last modified: February 24, 2026

---

# WP_REST_Comments_Controller::prepare_links( WP_Comment $comment ): array

## In this article

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

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

Prepares links for the request.

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

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

Comment object.

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

 array Links for the given comment.

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

    ```php
    protected function prepare_links( $comment ) {
    	$links = array(
    		'self'       => array(
    			'href' => rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $comment->comment_ID ) ),
    		),
    		'collection' => array(
    			'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
    		),
    	);

    	if ( 0 !== (int) $comment->user_id ) {
    		$links['author'] = array(
    			'href'       => rest_url( 'wp/v2/users/' . $comment->user_id ),
    			'embeddable' => true,
    		);
    	}

    	if ( 0 !== (int) $comment->comment_post_ID ) {
    		$post       = get_post( $comment->comment_post_ID );
    		$post_route = rest_get_route_for_post( $post );

    		if ( ! empty( $post->ID ) && $post_route ) {
    			$links['up'] = array(
    				'href'       => rest_url( $post_route ),
    				'embeddable' => true,
    				'post_type'  => $post->post_type,
    			);
    		}
    	}

    	if ( 0 !== (int) $comment->comment_parent ) {
    		$links['in-reply-to'] = array(
    			'href'       => rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $comment->comment_parent ) ),
    			'embeddable' => true,
    		);
    	}

    	// Only grab one comment to verify the comment has children.
    	$comment_children = $comment->get_children(
    		array(
    			'count'   => true,
    			'orderby' => 'none',
    			'type'    => 'all',
    		)
    	);

    	if ( ! empty( $comment_children ) ) {
    		$args = array(
    			'parent' => $comment->comment_ID,
    		);

    		$rest_url = add_query_arg( $args, rest_url( $this->namespace . '/' . $this->rest_base ) );

    		$links['children'] = array(
    			'href'       => $rest_url,
    			'embeddable' => true,
    		);
    	}

    	// Embedding children for notes requires `type` and `status` inheritance.
    	if ( isset( $links['children'] ) && 'note' === $comment->comment_type ) {
    		$args = array(
    			'parent' => $comment->comment_ID,
    			'type'   => $comment->comment_type,
    			'status' => 'all',
    		);

    		$rest_url = add_query_arg( $args, rest_url( $this->namespace . '/' . $this->rest_base ) );

    		$links['children'] = array(
    			'href'       => $rest_url,
    			'embeddable' => true,
    		);
    	}

    	return $links;
    }
    ```

[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/6.9.4/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php#L1240)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php#L1240-L1316)

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

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

Gets the REST API route for a post.

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

Retrieves the URL to a REST endpoint.

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

Retrieves a modified URL query string.

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

Retrieves post data given a post ID or post object.

  |

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

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

Prepares a single comment output for response.

  |

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

| Version | Description | 
| [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_links%2F)
before being able to contribute a note or feedback.