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

---

# WP_REST_URL_Details_Controller::get_metadata_from_meta_element( array $meta_elements, string $attr, string $attr_value ): string

## In this article

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

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.

Gets the metadata from a target meta element.

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

 `$meta_elements`arrayrequired

A multi-dimensional indexed array on success, else empty array.

 * `0` string[]
 * Meta elements with a content attribute.
 * `1` string[]
 * Content attribute’s opening quotation mark.
 * `2` string[]
 * Content attribute’s value for each meta element.

`$attr`stringrequired

Attribute that identifies the element with the target metadata.

`$attr_value`stringrequired

The attribute’s value that identifies the element with the target metadata.

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

 string The metadata on success. Empty string if not found.

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

    ```php
    private function get_metadata_from_meta_element( $meta_elements, $attr, $attr_value ) {
    	// Bail out if there are no meta elements.
    	if ( empty( $meta_elements[0] ) ) {
    		return '';
    	}

    	$metadata = '';
    	$pattern  = '#' .
    			/*
    			 * Target this attribute and value to find the metadata element.
    			 *
    			 * Allows for (a) no, single, double quotes and (b) whitespace in the value.
    			 *
    			 * Why capture the opening quotation mark, i.e. (["\']), and then backreference,
    			 * i.e \1, for the closing quotation mark?
    			 * To ensure the closing quotation mark matches the opening one. Why? Attribute values
    			 * can contain quotation marks, such as an apostrophe in the content.
    			 */
    			$attr . '=([\"\']??)\s*' . $attr_value . '\s*\1' .

    			/*
    			 * These are the options:
    			 * - i : case-insensitive
    			 * - s : allows newline characters for the . match (needed for multiline elements)
    			 * - U means non-greedy matching
    			 */
    			'#isU';

    	// Find the metadata element.
    	foreach ( $meta_elements[0] as $index => $element ) {
    		preg_match( $pattern, $element, $match );

    		// This is not the metadata element. Skip it.
    		if ( empty( $match ) ) {
    			continue;
    		}

    		/*
    		 * Found the metadata element.
    		 * Get the metadata from its matching content array.
    		 */
    		if ( isset( $meta_elements[2][ $index ] ) && is_string( $meta_elements[2][ $index ] ) ) {
    			$metadata = trim( $meta_elements[2][ $index ] );
    		}

    		break;
    	}

    	return $metadata;
    }
    ```

[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#L618)
[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#L618-L667)

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

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

Parses the meta description from the provided HTML.

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

Parses the Open Graph (OG) Image from the provided HTML.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wp_rest_url_details_controller/get_metadata_from_meta_element/?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_metadata_from_meta_element%2F)
before being able to contribute a note or feedback.