Title: WP_REST_URL_Details_Controller::get_document_head
Published: February 3, 2022
Last modified: August 20, 2026

---

# WP_REST_URL_Details_Controller::get_document_head( string $html ): string

## In this article

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

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

This function’s access is marked private. This means it is not intended for use 
by plugin or theme developers, only by core. It is listed here for completeness.

Retrieves the head element section.

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

 `$html`stringrequired

The string of HTML to parse.

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

 string The `<head>..</head>` section on success. Given `$html` if not found.

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

    ```php
    private function get_document_head( $html ) {
    	$head_html = $html;

    	// Find the opening `<head>` tag.
    	$head_start = strpos( $html, '<head' );
    	if ( false === $head_start ) {
    		// Didn't find it. Return the original HTML.
    		return $html;
    	}

    	// Find the closing `</head>` tag.
    	$head_end = strpos( $head_html, '</head>' );
    	if ( false === $head_end ) {
    		// Didn't find it. Find the opening `<body>` tag.
    		$head_end = strpos( $head_html, '<body' );

    		// Didn't find it. Return the original HTML.
    		if ( false === $head_end ) {
    			return $html;
    		}
    	}

    	// Extract the HTML from opening tag to the closing tag. Then add the closing tag.
    	$head_html  = substr( $head_html, $head_start, $head_end );
    	$head_html .= '</head>';

    	return $head_html;
    }
    ```

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

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

| Used by | Description | 
| [WP_REST_URL_Details_Controller::parse_url_details()](https://developer.wordpress.org/reference/classes/wp_rest_url_details_controller/parse_url_details/)`wp-includes/rest-api/endpoints/class-wp-rest-url-details-controller.php` |

Retrieves the contents of the title tag from the HTML response.

  |

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

| Version | Description | 
| [5.9.0](https://developer.wordpress.org/reference/since/5.9.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_url_details_controller%2Fget_document_head%2F)
before being able to contribute a note or feedback.